Commit | Line | Data |
---|---|---|
2d78951a MD |
1 | /* |
2 | * lttng-filter.c | |
3 | * | |
4 | * LTTng UST filter code. | |
5 | * | |
7e50015d | 6 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2d78951a | 7 | * |
7e50015d MD |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
2d78951a | 14 | * |
7e50015d MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
2d78951a | 17 | * |
7e50015d MD |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
2d78951a MD |
25 | */ |
26 | ||
3fbec7dc | 27 | #define _LGPL_SOURCE |
f488575f | 28 | #include <urcu/rculist.h> |
97b58163 | 29 | #include "lttng-filter.h" |
cd54f6d9 MD |
30 | |
31 | static const char *opnames[] = { | |
32 | [ FILTER_OP_UNKNOWN ] = "UNKNOWN", | |
33 | ||
34 | [ FILTER_OP_RETURN ] = "RETURN", | |
35 | ||
36 | /* binary */ | |
37 | [ FILTER_OP_MUL ] = "MUL", | |
38 | [ FILTER_OP_DIV ] = "DIV", | |
39 | [ FILTER_OP_MOD ] = "MOD", | |
40 | [ FILTER_OP_PLUS ] = "PLUS", | |
41 | [ FILTER_OP_MINUS ] = "MINUS", | |
0039e2d8 MD |
42 | [ FILTER_OP_BIT_RSHIFT ] = "BIT_RSHIFT", |
43 | [ FILTER_OP_BIT_LSHIFT ] = "BIT_LSHIFT", | |
47e5f13e MD |
44 | [ FILTER_OP_BIT_AND ] = "BIT_AND", |
45 | [ FILTER_OP_BIT_OR ] = "BIT_OR", | |
46 | [ FILTER_OP_BIT_XOR ] = "BIT_XOR", | |
226106c0 MD |
47 | |
48 | /* binary comparators */ | |
cd54f6d9 MD |
49 | [ FILTER_OP_EQ ] = "EQ", |
50 | [ FILTER_OP_NE ] = "NE", | |
51 | [ FILTER_OP_GT ] = "GT", | |
52 | [ FILTER_OP_LT ] = "LT", | |
53 | [ FILTER_OP_GE ] = "GE", | |
54 | [ FILTER_OP_LE ] = "LE", | |
55 | ||
226106c0 MD |
56 | /* string binary comparators */ |
57 | [ FILTER_OP_EQ_STRING ] = "EQ_STRING", | |
58 | [ FILTER_OP_NE_STRING ] = "NE_STRING", | |
59 | [ FILTER_OP_GT_STRING ] = "GT_STRING", | |
60 | [ FILTER_OP_LT_STRING ] = "LT_STRING", | |
61 | [ FILTER_OP_GE_STRING ] = "GE_STRING", | |
62 | [ FILTER_OP_LE_STRING ] = "LE_STRING", | |
63 | ||
64 | /* s64 binary comparators */ | |
65 | [ FILTER_OP_EQ_S64 ] = "EQ_S64", | |
66 | [ FILTER_OP_NE_S64 ] = "NE_S64", | |
67 | [ FILTER_OP_GT_S64 ] = "GT_S64", | |
68 | [ FILTER_OP_LT_S64 ] = "LT_S64", | |
69 | [ FILTER_OP_GE_S64 ] = "GE_S64", | |
70 | [ FILTER_OP_LE_S64 ] = "LE_S64", | |
71 | ||
72 | /* double binary comparators */ | |
73 | [ FILTER_OP_EQ_DOUBLE ] = "EQ_DOUBLE", | |
74 | [ FILTER_OP_NE_DOUBLE ] = "NE_DOUBLE", | |
75 | [ FILTER_OP_GT_DOUBLE ] = "GT_DOUBLE", | |
76 | [ FILTER_OP_LT_DOUBLE ] = "LT_DOUBLE", | |
77 | [ FILTER_OP_GE_DOUBLE ] = "GE_DOUBLE", | |
78 | [ FILTER_OP_LE_DOUBLE ] = "LE_DOUBLE", | |
79 | ||
1e5f62b4 MD |
80 | /* Mixed S64-double binary comparators */ |
81 | [ FILTER_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64", | |
82 | [ FILTER_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64", | |
83 | [ FILTER_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64", | |
84 | [ FILTER_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64", | |
85 | [ FILTER_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64", | |
86 | [ FILTER_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64", | |
87 | ||
88 | [ FILTER_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE", | |
89 | [ FILTER_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE", | |
90 | [ FILTER_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE", | |
91 | [ FILTER_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE", | |
92 | [ FILTER_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE", | |
93 | [ FILTER_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE", | |
226106c0 | 94 | |
cd54f6d9 MD |
95 | /* unary */ |
96 | [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS", | |
97 | [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS", | |
98 | [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT", | |
226106c0 MD |
99 | [ FILTER_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64", |
100 | [ FILTER_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64", | |
101 | [ FILTER_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64", | |
102 | [ FILTER_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE", | |
103 | [ FILTER_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE", | |
104 | [ FILTER_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE", | |
cd54f6d9 MD |
105 | |
106 | /* logical */ | |
107 | [ FILTER_OP_AND ] = "AND", | |
108 | [ FILTER_OP_OR ] = "OR", | |
109 | ||
77aa5901 | 110 | /* load field ref */ |
cd54f6d9 | 111 | [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF", |
2f0145d1 MD |
112 | [ FILTER_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING", |
113 | [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE", | |
114 | [ FILTER_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64", | |
115 | [ FILTER_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE", | |
116 | ||
77aa5901 | 117 | /* load from immediate operand */ |
cd54f6d9 MD |
118 | [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING", |
119 | [ FILTER_OP_LOAD_S64 ] = "LOAD_S64", | |
da6eed25 | 120 | [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE", |
49905038 MD |
121 | |
122 | /* cast */ | |
123 | [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64", | |
124 | [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64", | |
125 | [ FILTER_OP_CAST_NOP ] = "CAST_NOP", | |
77aa5901 MD |
126 | |
127 | /* get context ref */ | |
128 | [ FILTER_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF", | |
129 | [ FILTER_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING", | |
130 | [ FILTER_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64", | |
131 | [ FILTER_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE", | |
f6753f2d MD |
132 | |
133 | /* load userspace field ref */ | |
134 | [ FILTER_OP_LOAD_FIELD_REF_USER_STRING ] = "LOAD_FIELD_REF_USER_STRING", | |
135 | [ FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE ] = "LOAD_FIELD_REF_USER_SEQUENCE", | |
136 | ||
137 | /* | |
138 | * load immediate star globbing pattern (literal string) | |
139 | * from immediate. | |
140 | */ | |
141 | [ FILTER_OP_LOAD_STAR_GLOB_STRING ] = "LOAD_STAR_GLOB_STRING", | |
142 | ||
143 | /* globbing pattern binary operator: apply to */ | |
144 | [ FILTER_OP_EQ_STAR_GLOB_STRING ] = "EQ_STAR_GLOB_STRING", | |
145 | [ FILTER_OP_NE_STAR_GLOB_STRING ] = "NE_STAR_GLOB_STRING", | |
47e5f13e MD |
146 | |
147 | /* | |
148 | * Instructions for recursive traversal through composed types. | |
149 | */ | |
150 | [ FILTER_OP_GET_CONTEXT_ROOT ] = "GET_CONTEXT_ROOT", | |
151 | [ FILTER_OP_GET_APP_CONTEXT_ROOT ] = "GET_APP_CONTEXT_ROOT", | |
152 | [ FILTER_OP_GET_PAYLOAD_ROOT ] = "GET_PAYLOAD_ROOT", | |
153 | ||
154 | [ FILTER_OP_GET_SYMBOL ] = "GET_SYMBOL", | |
155 | [ FILTER_OP_GET_SYMBOL_FIELD ] = "GET_SYMBOL_FIELD", | |
156 | [ FILTER_OP_GET_INDEX_U16 ] = "GET_INDEX_U16", | |
157 | [ FILTER_OP_GET_INDEX_U64 ] = "GET_INDEX_U64", | |
158 | ||
159 | [ FILTER_OP_LOAD_FIELD ] = "LOAD_FIELD", | |
160 | [ FILTER_OP_LOAD_FIELD_S8 ] = "LOAD_FIELD_S8", | |
161 | [ FILTER_OP_LOAD_FIELD_S16 ] = "LOAD_FIELD_S16", | |
162 | [ FILTER_OP_LOAD_FIELD_S32 ] = "LOAD_FIELD_S32", | |
163 | [ FILTER_OP_LOAD_FIELD_S64 ] = "LOAD_FIELD_S64", | |
164 | [ FILTER_OP_LOAD_FIELD_U8 ] = "LOAD_FIELD_U8", | |
165 | [ FILTER_OP_LOAD_FIELD_U16 ] = "LOAD_FIELD_U16", | |
166 | [ FILTER_OP_LOAD_FIELD_U32 ] = "LOAD_FIELD_U32", | |
167 | [ FILTER_OP_LOAD_FIELD_U64 ] = "LOAD_FIELD_U64", | |
168 | [ FILTER_OP_LOAD_FIELD_STRING ] = "LOAD_FIELD_STRING", | |
169 | [ FILTER_OP_LOAD_FIELD_SEQUENCE ] = "LOAD_FIELD_SEQUENCE", | |
170 | [ FILTER_OP_LOAD_FIELD_DOUBLE ] = "LOAD_FIELD_DOUBLE", | |
0039e2d8 MD |
171 | |
172 | [ FILTER_OP_UNARY_BIT_NOT ] = "UNARY_BIT_NOT", | |
93c591bb MD |
173 | |
174 | [ FILTER_OP_RETURN_S64 ] = "RETURN_S64", | |
cd54f6d9 MD |
175 | }; |
176 | ||
cd54f6d9 MD |
177 | const char *print_op(enum filter_op op) |
178 | { | |
179 | if (op >= NR_FILTER_OPS) | |
180 | return "UNKNOWN"; | |
181 | else | |
182 | return opnames[op]; | |
183 | } | |
184 | ||
cd54f6d9 | 185 | static |
7dd08bec | 186 | int apply_field_reloc(struct lttng_event *event, |
cd54f6d9 MD |
187 | struct bytecode_runtime *runtime, |
188 | uint32_t runtime_len, | |
189 | uint32_t reloc_offset, | |
47e5f13e MD |
190 | const char *field_name, |
191 | enum filter_op filter_op) | |
cd54f6d9 MD |
192 | { |
193 | const struct lttng_event_desc *desc; | |
194 | const struct lttng_event_field *fields, *field = NULL; | |
195 | unsigned int nr_fields, i; | |
2f0145d1 | 196 | struct load_op *op; |
cd54f6d9 MD |
197 | uint32_t field_offset = 0; |
198 | ||
77aa5901 | 199 | dbg_printf("Apply field reloc: %u %s\n", reloc_offset, field_name); |
cd54f6d9 MD |
200 | |
201 | /* Lookup event by name */ | |
202 | desc = event->desc; | |
203 | if (!desc) | |
204 | return -EINVAL; | |
205 | fields = desc->fields; | |
206 | if (!fields) | |
207 | return -EINVAL; | |
208 | nr_fields = desc->nr_fields; | |
209 | for (i = 0; i < nr_fields; i++) { | |
210 | if (!strcmp(fields[i].name, field_name)) { | |
211 | field = &fields[i]; | |
212 | break; | |
213 | } | |
214 | /* compute field offset */ | |
215 | switch (fields[i].type.atype) { | |
216 | case atype_integer: | |
217 | case atype_enum: | |
218 | field_offset += sizeof(int64_t); | |
219 | break; | |
220 | case atype_array: | |
221 | case atype_sequence: | |
222 | field_offset += sizeof(unsigned long); | |
223 | field_offset += sizeof(void *); | |
224 | break; | |
225 | case atype_string: | |
226 | field_offset += sizeof(void *); | |
227 | break; | |
228 | case atype_float: | |
229 | field_offset += sizeof(double); | |
da6eed25 | 230 | break; |
cd54f6d9 MD |
231 | default: |
232 | return -EINVAL; | |
233 | } | |
234 | } | |
235 | if (!field) | |
236 | return -EINVAL; | |
237 | ||
238 | /* Check if field offset is too large for 16-bit offset */ | |
5b4839a8 | 239 | if (field_offset > FILTER_BYTECODE_MAX_LEN - 1) |
cd54f6d9 MD |
240 | return -EINVAL; |
241 | ||
242 | /* set type */ | |
47e5f13e MD |
243 | op = (struct load_op *) &runtime->code[reloc_offset]; |
244 | ||
245 | switch (filter_op) { | |
246 | case FILTER_OP_LOAD_FIELD_REF: | |
247 | { | |
248 | struct field_ref *field_ref; | |
249 | ||
250 | field_ref = (struct field_ref *) op->data; | |
251 | switch (field->type.atype) { | |
252 | case atype_integer: | |
253 | case atype_enum: | |
254 | op->op = FILTER_OP_LOAD_FIELD_REF_S64; | |
255 | break; | |
256 | case atype_array: | |
1d7f24e1 MD |
257 | { |
258 | const struct lttng_basic_type *elem_type = &field->type.u.array.elem_type; | |
259 | ||
81f083b1 | 260 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
1d7f24e1 MD |
261 | return -EINVAL; |
262 | op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE; | |
263 | break; | |
264 | } | |
47e5f13e | 265 | case atype_sequence: |
1d7f24e1 MD |
266 | { |
267 | const struct lttng_basic_type *elem_type = &field->type.u.sequence.elem_type; | |
268 | ||
81f083b1 | 269 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
1d7f24e1 | 270 | return -EINVAL; |
47e5f13e MD |
271 | op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE; |
272 | break; | |
1d7f24e1 | 273 | } |
47e5f13e MD |
274 | case atype_string: |
275 | op->op = FILTER_OP_LOAD_FIELD_REF_STRING; | |
276 | break; | |
277 | case atype_float: | |
278 | op->op = FILTER_OP_LOAD_FIELD_REF_DOUBLE; | |
279 | break; | |
280 | default: | |
281 | return -EINVAL; | |
282 | } | |
283 | /* set offset */ | |
284 | field_ref->offset = (uint16_t) field_offset; | |
da6eed25 | 285 | break; |
47e5f13e | 286 | } |
cd54f6d9 MD |
287 | default: |
288 | return -EINVAL; | |
289 | } | |
2d78951a MD |
290 | return 0; |
291 | } | |
292 | ||
77aa5901 MD |
293 | static |
294 | int apply_context_reloc(struct lttng_event *event, | |
295 | struct bytecode_runtime *runtime, | |
296 | uint32_t runtime_len, | |
297 | uint32_t reloc_offset, | |
47e5f13e MD |
298 | const char *context_name, |
299 | enum filter_op filter_op) | |
77aa5901 | 300 | { |
77aa5901 MD |
301 | struct load_op *op; |
302 | struct lttng_ctx_field *ctx_field; | |
303 | int idx; | |
53569322 | 304 | struct lttng_session *session = runtime->p.session; |
77aa5901 MD |
305 | |
306 | dbg_printf("Apply context reloc: %u %s\n", reloc_offset, context_name); | |
307 | ||
308 | /* Get context index */ | |
53569322 MD |
309 | idx = lttng_get_context_index(session->ctx, context_name); |
310 | if (idx < 0) { | |
311 | if (lttng_context_is_app(context_name)) { | |
312 | int ret; | |
313 | ||
314 | ret = lttng_ust_add_app_context_to_ctx_rcu(context_name, | |
315 | &session->ctx); | |
316 | if (ret) | |
317 | return ret; | |
318 | idx = lttng_get_context_index(session->ctx, | |
319 | context_name); | |
320 | if (idx < 0) | |
321 | return -ENOENT; | |
322 | } else { | |
323 | return -ENOENT; | |
324 | } | |
325 | } | |
77aa5901 MD |
326 | /* Check if idx is too large for 16-bit offset */ |
327 | if (idx > FILTER_BYTECODE_MAX_LEN - 1) | |
328 | return -EINVAL; | |
329 | ||
330 | /* Get context return type */ | |
53569322 | 331 | ctx_field = &session->ctx->fields[idx]; |
47e5f13e MD |
332 | op = (struct load_op *) &runtime->code[reloc_offset]; |
333 | ||
334 | switch (filter_op) { | |
335 | case FILTER_OP_GET_CONTEXT_REF: | |
336 | { | |
337 | struct field_ref *field_ref; | |
338 | ||
339 | field_ref = (struct field_ref *) op->data; | |
340 | switch (ctx_field->event_field.type.atype) { | |
341 | case atype_integer: | |
342 | case atype_enum: | |
343 | op->op = FILTER_OP_GET_CONTEXT_REF_S64; | |
344 | break; | |
345 | /* Sequence and array supported as string */ | |
47e5f13e | 346 | case atype_array: |
1d7f24e1 MD |
347 | { |
348 | const struct lttng_basic_type *elem_type = &ctx_field->event_field.type.u.array.elem_type; | |
349 | ||
81f083b1 | 350 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
1d7f24e1 MD |
351 | return -EINVAL; |
352 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; | |
353 | break; | |
354 | } | |
47e5f13e | 355 | case atype_sequence: |
1d7f24e1 MD |
356 | { |
357 | const struct lttng_basic_type *elem_type = &ctx_field->event_field.type.u.sequence.elem_type; | |
358 | ||
81f083b1 | 359 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
1d7f24e1 MD |
360 | return -EINVAL; |
361 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; | |
362 | break; | |
363 | } | |
364 | case atype_string: | |
47e5f13e MD |
365 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; |
366 | break; | |
367 | case atype_float: | |
368 | op->op = FILTER_OP_GET_CONTEXT_REF_DOUBLE; | |
369 | break; | |
370 | case atype_dynamic: | |
371 | op->op = FILTER_OP_GET_CONTEXT_REF; | |
372 | break; | |
373 | default: | |
374 | return -EINVAL; | |
375 | } | |
376 | /* set offset to context index within channel contexts */ | |
377 | field_ref->offset = (uint16_t) idx; | |
53569322 | 378 | break; |
47e5f13e | 379 | } |
77aa5901 MD |
380 | default: |
381 | return -EINVAL; | |
382 | } | |
77aa5901 MD |
383 | return 0; |
384 | } | |
385 | ||
386 | static | |
387 | int apply_reloc(struct lttng_event *event, | |
388 | struct bytecode_runtime *runtime, | |
389 | uint32_t runtime_len, | |
390 | uint32_t reloc_offset, | |
391 | const char *name) | |
392 | { | |
393 | struct load_op *op; | |
394 | ||
395 | dbg_printf("Apply reloc: %u %s\n", reloc_offset, name); | |
396 | ||
397 | /* Ensure that the reloc is within the code */ | |
398 | if (runtime_len - reloc_offset < sizeof(uint16_t)) | |
399 | return -EINVAL; | |
400 | ||
47e5f13e | 401 | op = (struct load_op *) &runtime->code[reloc_offset]; |
77aa5901 MD |
402 | switch (op->op) { |
403 | case FILTER_OP_LOAD_FIELD_REF: | |
404 | return apply_field_reloc(event, runtime, runtime_len, | |
47e5f13e | 405 | reloc_offset, name, op->op); |
77aa5901 MD |
406 | case FILTER_OP_GET_CONTEXT_REF: |
407 | return apply_context_reloc(event, runtime, runtime_len, | |
47e5f13e MD |
408 | reloc_offset, name, op->op); |
409 | case FILTER_OP_GET_SYMBOL: | |
410 | case FILTER_OP_GET_SYMBOL_FIELD: | |
411 | /* | |
412 | * Will be handled by load specialize phase or | |
413 | * dynamically by interpreter. | |
414 | */ | |
415 | return 0; | |
77aa5901 MD |
416 | default: |
417 | ERR("Unknown reloc op type %u\n", op->op); | |
418 | return -EINVAL; | |
419 | } | |
420 | return 0; | |
421 | } | |
422 | ||
f488575f MD |
423 | static |
424 | int bytecode_is_linked(struct lttng_ust_filter_bytecode_node *filter_bytecode, | |
7dd08bec | 425 | struct lttng_event *event) |
f488575f MD |
426 | { |
427 | struct lttng_bytecode_runtime *bc_runtime; | |
428 | ||
e58095ef MD |
429 | cds_list_for_each_entry(bc_runtime, |
430 | &event->bytecode_runtime_head, node) { | |
f488575f MD |
431 | if (bc_runtime->bc == filter_bytecode) |
432 | return 1; | |
433 | } | |
434 | return 0; | |
435 | } | |
436 | ||
cd54f6d9 MD |
437 | /* |
438 | * Take a bytecode with reloc table and link it to an event to create a | |
439 | * bytecode runtime. | |
440 | */ | |
2d78951a | 441 | static |
7dd08bec | 442 | int _lttng_filter_event_link_bytecode(struct lttng_event *event, |
e58095ef MD |
443 | struct lttng_ust_filter_bytecode_node *filter_bytecode, |
444 | struct cds_list_head *insert_loc) | |
2d78951a | 445 | { |
cd54f6d9 MD |
446 | int ret, offset, next_offset; |
447 | struct bytecode_runtime *runtime = NULL; | |
448 | size_t runtime_alloc_len; | |
449 | ||
2d78951a MD |
450 | if (!filter_bytecode) |
451 | return 0; | |
cd54f6d9 | 452 | /* Bytecode already linked */ |
f488575f | 453 | if (bytecode_is_linked(filter_bytecode, event)) |
cd54f6d9 | 454 | return 0; |
2d78951a | 455 | |
f488575f | 456 | dbg_printf("Linking...\n"); |
cd54f6d9 MD |
457 | |
458 | /* We don't need the reloc table in the runtime */ | |
f488575f | 459 | runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset; |
cd54f6d9 MD |
460 | runtime = zmalloc(runtime_alloc_len); |
461 | if (!runtime) { | |
462 | ret = -ENOMEM; | |
e0a7d7ab | 463 | goto alloc_error; |
cd54f6d9 | 464 | } |
f488575f | 465 | runtime->p.bc = filter_bytecode; |
53569322 | 466 | runtime->p.session = event->chan->session; |
47e5f13e | 467 | runtime->p.event = event; |
f488575f | 468 | runtime->len = filter_bytecode->bc.reloc_offset; |
cd54f6d9 | 469 | /* copy original bytecode */ |
47e5f13e | 470 | memcpy(runtime->code, filter_bytecode->bc.data, runtime->len); |
cd54f6d9 MD |
471 | /* |
472 | * apply relocs. Those are a uint16_t (offset in bytecode) | |
473 | * followed by a string (field name). | |
474 | */ | |
f488575f MD |
475 | for (offset = filter_bytecode->bc.reloc_offset; |
476 | offset < filter_bytecode->bc.len; | |
cd54f6d9 MD |
477 | offset = next_offset) { |
478 | uint16_t reloc_offset = | |
f488575f | 479 | *(uint16_t *) &filter_bytecode->bc.data[offset]; |
77aa5901 | 480 | const char *name = |
f488575f | 481 | (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)]; |
cd54f6d9 | 482 | |
77aa5901 | 483 | ret = apply_reloc(event, runtime, runtime->len, reloc_offset, name); |
cd54f6d9 MD |
484 | if (ret) { |
485 | goto link_error; | |
486 | } | |
77aa5901 | 487 | next_offset = offset + sizeof(uint16_t) + strlen(name) + 1; |
cd54f6d9 | 488 | } |
9522a886 MD |
489 | /* Validate bytecode */ |
490 | ret = lttng_filter_validate_bytecode(runtime); | |
491 | if (ret) { | |
492 | goto link_error; | |
493 | } | |
08c84b15 | 494 | /* Specialize bytecode */ |
47e5f13e | 495 | ret = lttng_filter_specialize_bytecode(event, runtime); |
08c84b15 MD |
496 | if (ret) { |
497 | goto link_error; | |
498 | } | |
f488575f | 499 | runtime->p.filter = lttng_filter_interpret_bytecode; |
21af05a9 | 500 | runtime->p.link_failed = 0; |
e58095ef | 501 | cds_list_add_rcu(&runtime->p.node, insert_loc); |
f488575f | 502 | dbg_printf("Linking successful.\n"); |
2d78951a | 503 | return 0; |
cd54f6d9 MD |
504 | |
505 | link_error: | |
f488575f | 506 | runtime->p.filter = lttng_filter_false; |
21af05a9 | 507 | runtime->p.link_failed = 1; |
e58095ef | 508 | cds_list_add_rcu(&runtime->p.node, insert_loc); |
e0a7d7ab | 509 | alloc_error: |
f488575f | 510 | dbg_printf("Linking failed.\n"); |
cd54f6d9 | 511 | return ret; |
2d78951a MD |
512 | } |
513 | ||
e58095ef | 514 | void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime) |
2d78951a | 515 | { |
e58095ef | 516 | struct lttng_ust_filter_bytecode_node *bc = runtime->bc; |
f488575f | 517 | |
21af05a9 | 518 | if (!bc->enabler->enabled || runtime->link_failed) |
e58095ef | 519 | runtime->filter = lttng_filter_false; |
21af05a9 MD |
520 | else |
521 | runtime->filter = lttng_filter_interpret_bytecode; | |
2d78951a MD |
522 | } |
523 | ||
524 | /* | |
e58095ef | 525 | * Link bytecode for all enablers referenced by an event. |
2d78951a | 526 | */ |
e58095ef MD |
527 | void lttng_enabler_event_link_bytecode(struct lttng_event *event, |
528 | struct lttng_enabler *enabler) | |
2d78951a | 529 | { |
e58095ef MD |
530 | struct lttng_ust_filter_bytecode_node *bc; |
531 | struct lttng_bytecode_runtime *runtime; | |
532 | ||
533 | /* Can only be called for events with desc attached */ | |
534 | assert(event->desc); | |
535 | ||
536 | /* Link each bytecode. */ | |
537 | cds_list_for_each_entry(bc, &enabler->filter_bytecode_head, node) { | |
538 | int found = 0, ret; | |
539 | struct cds_list_head *insert_loc; | |
540 | ||
541 | cds_list_for_each_entry(runtime, | |
542 | &event->bytecode_runtime_head, node) { | |
543 | if (runtime->bc == bc) { | |
544 | found = 1; | |
545 | break; | |
546 | } | |
547 | } | |
548 | /* Skip bytecode already linked */ | |
549 | if (found) | |
550 | continue; | |
551 | ||
552 | /* | |
553 | * Insert at specified priority (seqnum) in increasing | |
554 | * order. | |
555 | */ | |
556 | cds_list_for_each_entry_reverse(runtime, | |
557 | &event->bytecode_runtime_head, node) { | |
558 | if (runtime->bc->bc.seqnum < bc->bc.seqnum) { | |
559 | /* insert here */ | |
560 | insert_loc = &runtime->node; | |
561 | goto add_within; | |
562 | } | |
563 | } | |
564 | /* Add to head to list */ | |
565 | insert_loc = &event->bytecode_runtime_head; | |
566 | add_within: | |
f488575f | 567 | dbg_printf("linking bytecode\n"); |
e58095ef MD |
568 | ret = _lttng_filter_event_link_bytecode(event, bc, |
569 | insert_loc); | |
570 | if (ret) { | |
571 | dbg_printf("[lttng filter] warning: cannot link event bytecode\n"); | |
572 | } | |
2d78951a | 573 | } |
2d78951a MD |
574 | } |
575 | ||
576 | /* | |
e58095ef | 577 | * We own the filter_bytecode if we return success. |
2d78951a | 578 | */ |
e58095ef | 579 | int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler, |
f488575f | 580 | struct lttng_ust_filter_bytecode_node *filter_bytecode) |
2d78951a | 581 | { |
e58095ef | 582 | cds_list_add(&filter_bytecode->node, &enabler->filter_bytecode_head); |
2d78951a MD |
583 | return 0; |
584 | } | |
f488575f | 585 | |
e58095ef | 586 | void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler) |
f488575f MD |
587 | { |
588 | struct lttng_ust_filter_bytecode_node *filter_bytecode, *tmp; | |
589 | ||
590 | cds_list_for_each_entry_safe(filter_bytecode, tmp, | |
e58095ef | 591 | &enabler->filter_bytecode_head, node) { |
f488575f MD |
592 | free(filter_bytecode); |
593 | } | |
594 | } | |
595 | ||
7dd08bec | 596 | void lttng_free_event_filter_runtime(struct lttng_event *event) |
f488575f MD |
597 | { |
598 | struct bytecode_runtime *runtime, *tmp; | |
599 | ||
600 | cds_list_for_each_entry_safe(runtime, tmp, | |
e58095ef | 601 | &event->bytecode_runtime_head, p.node) { |
47e5f13e | 602 | free(runtime->data); |
f488575f MD |
603 | free(runtime); |
604 | } | |
605 | } |