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