Introduce LTTNG_UST_MAP_POPULATE_POLICY environment variable
[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 <urcu/rculist.h>
29 #include "lttng-filter.h"
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",
42 [ FILTER_OP_BIT_RSHIFT ] = "BIT_RSHIFT",
43 [ FILTER_OP_BIT_LSHIFT ] = "BIT_LSHIFT",
44 [ FILTER_OP_BIT_AND ] = "BIT_AND",
45 [ FILTER_OP_BIT_OR ] = "BIT_OR",
46 [ FILTER_OP_BIT_XOR ] = "BIT_XOR",
47
48 /* binary comparators */
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
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
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",
94
95 /* unary */
96 [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS",
97 [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS",
98 [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT",
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",
105
106 /* logical */
107 [ FILTER_OP_AND ] = "AND",
108 [ FILTER_OP_OR ] = "OR",
109
110 /* load field ref */
111 [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF",
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
117 /* load from immediate operand */
118 [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING",
119 [ FILTER_OP_LOAD_S64 ] = "LOAD_S64",
120 [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
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",
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",
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",
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",
171
172 [ FILTER_OP_UNARY_BIT_NOT ] = "UNARY_BIT_NOT",
173
174 [ FILTER_OP_RETURN_S64 ] = "RETURN_S64",
175 };
176
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
185 static
186 int apply_field_reloc(struct lttng_event *event,
187 struct bytecode_runtime *runtime,
188 uint32_t runtime_len,
189 uint32_t reloc_offset,
190 const char *field_name,
191 enum filter_op filter_op)
192 {
193 const struct lttng_event_desc *desc;
194 const struct lttng_event_field *fields, *field = NULL;
195 unsigned int nr_fields, i;
196 struct load_op *op;
197 uint32_t field_offset = 0;
198
199 dbg_printf("Apply field reloc: %u %s\n", reloc_offset, field_name);
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);
230 break;
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 */
239 if (field_offset > FILTER_BYTECODE_MAX_LEN - 1)
240 return -EINVAL;
241
242 /* set type */
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:
257 case atype_sequence:
258 op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE;
259 break;
260 case atype_string:
261 op->op = FILTER_OP_LOAD_FIELD_REF_STRING;
262 break;
263 case atype_float:
264 op->op = FILTER_OP_LOAD_FIELD_REF_DOUBLE;
265 break;
266 default:
267 return -EINVAL;
268 }
269 /* set offset */
270 field_ref->offset = (uint16_t) field_offset;
271 break;
272 }
273 default:
274 return -EINVAL;
275 }
276 return 0;
277 }
278
279 static
280 int apply_context_reloc(struct lttng_event *event,
281 struct bytecode_runtime *runtime,
282 uint32_t runtime_len,
283 uint32_t reloc_offset,
284 const char *context_name,
285 enum filter_op filter_op)
286 {
287 struct load_op *op;
288 struct lttng_ctx_field *ctx_field;
289 int idx;
290 struct lttng_session *session = runtime->p.session;
291
292 dbg_printf("Apply context reloc: %u %s\n", reloc_offset, context_name);
293
294 /* Get context index */
295 idx = lttng_get_context_index(session->ctx, context_name);
296 if (idx < 0) {
297 if (lttng_context_is_app(context_name)) {
298 int ret;
299
300 ret = lttng_ust_add_app_context_to_ctx_rcu(context_name,
301 &session->ctx);
302 if (ret)
303 return ret;
304 idx = lttng_get_context_index(session->ctx,
305 context_name);
306 if (idx < 0)
307 return -ENOENT;
308 } else {
309 return -ENOENT;
310 }
311 }
312 /* Check if idx is too large for 16-bit offset */
313 if (idx > FILTER_BYTECODE_MAX_LEN - 1)
314 return -EINVAL;
315
316 /* Get context return type */
317 ctx_field = &session->ctx->fields[idx];
318 op = (struct load_op *) &runtime->code[reloc_offset];
319
320 switch (filter_op) {
321 case FILTER_OP_GET_CONTEXT_REF:
322 {
323 struct field_ref *field_ref;
324
325 field_ref = (struct field_ref *) op->data;
326 switch (ctx_field->event_field.type.atype) {
327 case atype_integer:
328 case atype_enum:
329 op->op = FILTER_OP_GET_CONTEXT_REF_S64;
330 break;
331 /* Sequence and array supported as string */
332 case atype_string:
333 case atype_array:
334 case atype_sequence:
335 op->op = FILTER_OP_GET_CONTEXT_REF_STRING;
336 break;
337 case atype_float:
338 op->op = FILTER_OP_GET_CONTEXT_REF_DOUBLE;
339 break;
340 case atype_dynamic:
341 op->op = FILTER_OP_GET_CONTEXT_REF;
342 break;
343 default:
344 return -EINVAL;
345 }
346 /* set offset to context index within channel contexts */
347 field_ref->offset = (uint16_t) idx;
348 break;
349 }
350 default:
351 return -EINVAL;
352 }
353 return 0;
354 }
355
356 static
357 int apply_reloc(struct lttng_event *event,
358 struct bytecode_runtime *runtime,
359 uint32_t runtime_len,
360 uint32_t reloc_offset,
361 const char *name)
362 {
363 struct load_op *op;
364
365 dbg_printf("Apply reloc: %u %s\n", reloc_offset, name);
366
367 /* Ensure that the reloc is within the code */
368 if (runtime_len - reloc_offset < sizeof(uint16_t))
369 return -EINVAL;
370
371 op = (struct load_op *) &runtime->code[reloc_offset];
372 switch (op->op) {
373 case FILTER_OP_LOAD_FIELD_REF:
374 return apply_field_reloc(event, runtime, runtime_len,
375 reloc_offset, name, op->op);
376 case FILTER_OP_GET_CONTEXT_REF:
377 return apply_context_reloc(event, runtime, runtime_len,
378 reloc_offset, name, op->op);
379 case FILTER_OP_GET_SYMBOL:
380 case FILTER_OP_GET_SYMBOL_FIELD:
381 /*
382 * Will be handled by load specialize phase or
383 * dynamically by interpreter.
384 */
385 return 0;
386 default:
387 ERR("Unknown reloc op type %u\n", op->op);
388 return -EINVAL;
389 }
390 return 0;
391 }
392
393 static
394 int bytecode_is_linked(struct lttng_ust_filter_bytecode_node *filter_bytecode,
395 struct lttng_event *event)
396 {
397 struct lttng_bytecode_runtime *bc_runtime;
398
399 cds_list_for_each_entry(bc_runtime,
400 &event->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_event_link_bytecode(struct lttng_event *event,
413 struct lttng_ust_filter_bytecode_node *filter_bytecode,
414 struct cds_list_head *insert_loc)
415 {
416 int ret, offset, next_offset;
417 struct bytecode_runtime *runtime = NULL;
418 size_t runtime_alloc_len;
419
420 if (!filter_bytecode)
421 return 0;
422 /* Bytecode already linked */
423 if (bytecode_is_linked(filter_bytecode, event))
424 return 0;
425
426 dbg_printf("Linking...\n");
427
428 /* We don't need the reloc table in the runtime */
429 runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset;
430 runtime = zmalloc(runtime_alloc_len);
431 if (!runtime) {
432 ret = -ENOMEM;
433 goto alloc_error;
434 }
435 runtime->p.bc = filter_bytecode;
436 runtime->p.session = event->chan->session;
437 runtime->p.event = event;
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, 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, 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_event_link_bytecode(struct lttng_event *event,
498 struct lttng_enabler *enabler)
499 {
500 struct lttng_ust_filter_bytecode_node *bc;
501 struct lttng_bytecode_runtime *runtime;
502
503 /* Can only be called for events with desc attached */
504 assert(event->desc);
505
506 /* Link each bytecode. */
507 cds_list_for_each_entry(bc, &enabler->filter_bytecode_head, node) {
508 int found = 0, ret;
509 struct cds_list_head *insert_loc;
510
511 cds_list_for_each_entry(runtime,
512 &event->bytecode_runtime_head, node) {
513 if (runtime->bc == bc) {
514 found = 1;
515 break;
516 }
517 }
518 /* Skip bytecode already linked */
519 if (found)
520 continue;
521
522 /*
523 * Insert at specified priority (seqnum) in increasing
524 * order.
525 */
526 cds_list_for_each_entry_reverse(runtime,
527 &event->bytecode_runtime_head, node) {
528 if (runtime->bc->bc.seqnum < bc->bc.seqnum) {
529 /* insert here */
530 insert_loc = &runtime->node;
531 goto add_within;
532 }
533 }
534 /* Add to head to list */
535 insert_loc = &event->bytecode_runtime_head;
536 add_within:
537 dbg_printf("linking bytecode\n");
538 ret = _lttng_filter_event_link_bytecode(event, bc,
539 insert_loc);
540 if (ret) {
541 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
542 }
543 }
544 }
545
546 /*
547 * We own the filter_bytecode if we return success.
548 */
549 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
550 struct lttng_ust_filter_bytecode_node *filter_bytecode)
551 {
552 cds_list_add(&filter_bytecode->node, &enabler->filter_bytecode_head);
553 return 0;
554 }
555
556 void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler)
557 {
558 struct lttng_ust_filter_bytecode_node *filter_bytecode, *tmp;
559
560 cds_list_for_each_entry_safe(filter_bytecode, tmp,
561 &enabler->filter_bytecode_head, node) {
562 free(filter_bytecode);
563 }
564 }
565
566 void lttng_free_event_filter_runtime(struct lttng_event *event)
567 {
568 struct bytecode_runtime *runtime, *tmp;
569
570 cds_list_for_each_entry_safe(runtime, tmp,
571 &event->bytecode_runtime_head, p.node) {
572 free(runtime->data);
573 free(runtime);
574 }
575 }
This page took 0.040156 seconds and 4 git commands to generate.