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