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