4 * LTTng UST filter code.
6 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
28 #include <urcu/rculist.h>
29 #include "lttng-filter.h"
31 static const char *opnames
[] = {
32 [ FILTER_OP_UNKNOWN
] = "UNKNOWN",
34 [ FILTER_OP_RETURN
] = "RETURN",
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_RSHIFT
] = "RSHIFT",
43 [ FILTER_OP_LSHIFT
] = "LSHIFT",
44 [ FILTER_OP_BIT_AND
] = "BIT_AND",
45 [ FILTER_OP_BIT_OR
] = "BIT_OR",
46 [ FILTER_OP_BIT_XOR
] = "BIT_XOR",
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",
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",
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",
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",
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",
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",
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",
107 [ FILTER_OP_AND
] = "AND",
108 [ FILTER_OP_OR
] = "OR",
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",
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",
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",
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",
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",
138 * load immediate star globbing pattern (literal string)
141 [ FILTER_OP_LOAD_STAR_GLOB_STRING
] = "LOAD_STAR_GLOB_STRING",
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",
148 * Instructions for recursive traversal through composed types.
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",
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",
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",
173 const char *print_op(enum filter_op op
)
175 if (op
>= NR_FILTER_OPS
)
182 int apply_field_reloc(struct lttng_event
*event
,
183 struct bytecode_runtime
*runtime
,
184 uint32_t runtime_len
,
185 uint32_t reloc_offset
,
186 const char *field_name
,
187 enum filter_op filter_op
)
189 const struct lttng_event_desc
*desc
;
190 const struct lttng_event_field
*fields
, *field
= NULL
;
191 unsigned int nr_fields
, i
;
193 uint32_t field_offset
= 0;
195 dbg_printf("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
197 /* Lookup event by name */
201 fields
= desc
->fields
;
204 nr_fields
= desc
->nr_fields
;
205 for (i
= 0; i
< nr_fields
; i
++) {
206 if (!strcmp(fields
[i
].name
, field_name
)) {
210 /* compute field offset */
211 switch (fields
[i
].type
.atype
) {
214 field_offset
+= sizeof(int64_t);
218 field_offset
+= sizeof(unsigned long);
219 field_offset
+= sizeof(void *);
222 field_offset
+= sizeof(void *);
225 field_offset
+= sizeof(double);
234 /* Check if field offset is too large for 16-bit offset */
235 if (field_offset
> FILTER_BYTECODE_MAX_LEN
- 1)
239 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
242 case FILTER_OP_LOAD_FIELD_REF
:
244 struct field_ref
*field_ref
;
246 field_ref
= (struct field_ref
*) op
->data
;
247 switch (field
->type
.atype
) {
250 op
->op
= FILTER_OP_LOAD_FIELD_REF_S64
;
254 op
->op
= FILTER_OP_LOAD_FIELD_REF_SEQUENCE
;
257 op
->op
= FILTER_OP_LOAD_FIELD_REF_STRING
;
260 op
->op
= FILTER_OP_LOAD_FIELD_REF_DOUBLE
;
266 field_ref
->offset
= (uint16_t) field_offset
;
276 int apply_context_reloc(struct lttng_event
*event
,
277 struct bytecode_runtime
*runtime
,
278 uint32_t runtime_len
,
279 uint32_t reloc_offset
,
280 const char *context_name
,
281 enum filter_op filter_op
)
284 struct lttng_ctx_field
*ctx_field
;
286 struct lttng_session
*session
= runtime
->p
.session
;
288 dbg_printf("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
290 /* Get context index */
291 idx
= lttng_get_context_index(session
->ctx
, context_name
);
293 if (lttng_context_is_app(context_name
)) {
296 ret
= lttng_ust_add_app_context_to_ctx_rcu(context_name
,
300 idx
= lttng_get_context_index(session
->ctx
,
308 /* Check if idx is too large for 16-bit offset */
309 if (idx
> FILTER_BYTECODE_MAX_LEN
- 1)
312 /* Get context return type */
313 ctx_field
= &session
->ctx
->fields
[idx
];
314 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
317 case FILTER_OP_GET_CONTEXT_REF
:
319 struct field_ref
*field_ref
;
321 field_ref
= (struct field_ref
*) op
->data
;
322 switch (ctx_field
->event_field
.type
.atype
) {
325 op
->op
= FILTER_OP_GET_CONTEXT_REF_S64
;
327 /* Sequence and array supported as string */
331 op
->op
= FILTER_OP_GET_CONTEXT_REF_STRING
;
334 op
->op
= FILTER_OP_GET_CONTEXT_REF_DOUBLE
;
337 op
->op
= FILTER_OP_GET_CONTEXT_REF
;
342 /* set offset to context index within channel contexts */
343 field_ref
->offset
= (uint16_t) idx
;
353 int apply_reloc(struct lttng_event
*event
,
354 struct bytecode_runtime
*runtime
,
355 uint32_t runtime_len
,
356 uint32_t reloc_offset
,
361 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, name
);
363 /* Ensure that the reloc is within the code */
364 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
367 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
369 case FILTER_OP_LOAD_FIELD_REF
:
370 return apply_field_reloc(event
, runtime
, runtime_len
,
371 reloc_offset
, name
, op
->op
);
372 case FILTER_OP_GET_CONTEXT_REF
:
373 return apply_context_reloc(event
, runtime
, runtime_len
,
374 reloc_offset
, name
, op
->op
);
375 case FILTER_OP_GET_SYMBOL
:
376 case FILTER_OP_GET_SYMBOL_FIELD
:
378 * Will be handled by load specialize phase or
379 * dynamically by interpreter.
383 ERR("Unknown reloc op type %u\n", op
->op
);
390 int bytecode_is_linked(struct lttng_ust_filter_bytecode_node
*filter_bytecode
,
391 struct lttng_event
*event
)
393 struct lttng_bytecode_runtime
*bc_runtime
;
395 cds_list_for_each_entry(bc_runtime
,
396 &event
->bytecode_runtime_head
, node
) {
397 if (bc_runtime
->bc
== filter_bytecode
)
404 * Take a bytecode with reloc table and link it to an event to create a
408 int _lttng_filter_event_link_bytecode(struct lttng_event
*event
,
409 struct lttng_ust_filter_bytecode_node
*filter_bytecode
,
410 struct cds_list_head
*insert_loc
)
412 int ret
, offset
, next_offset
;
413 struct bytecode_runtime
*runtime
= NULL
;
414 size_t runtime_alloc_len
;
416 if (!filter_bytecode
)
418 /* Bytecode already linked */
419 if (bytecode_is_linked(filter_bytecode
, event
))
422 dbg_printf("Linking...\n");
424 /* We don't need the reloc table in the runtime */
425 runtime_alloc_len
= sizeof(*runtime
) + filter_bytecode
->bc
.reloc_offset
;
426 runtime
= zmalloc(runtime_alloc_len
);
431 runtime
->p
.bc
= filter_bytecode
;
432 runtime
->p
.session
= event
->chan
->session
;
433 runtime
->p
.event
= event
;
434 runtime
->len
= filter_bytecode
->bc
.reloc_offset
;
435 /* copy original bytecode */
436 memcpy(runtime
->code
, filter_bytecode
->bc
.data
, runtime
->len
);
438 * apply relocs. Those are a uint16_t (offset in bytecode)
439 * followed by a string (field name).
441 for (offset
= filter_bytecode
->bc
.reloc_offset
;
442 offset
< filter_bytecode
->bc
.len
;
443 offset
= next_offset
) {
444 uint16_t reloc_offset
=
445 *(uint16_t *) &filter_bytecode
->bc
.data
[offset
];
447 (const char *) &filter_bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
449 ret
= apply_reloc(event
, runtime
, runtime
->len
, reloc_offset
, name
);
453 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
455 /* Validate bytecode */
456 ret
= lttng_filter_validate_bytecode(runtime
);
460 /* Specialize bytecode */
461 ret
= lttng_filter_specialize_bytecode(event
, runtime
);
465 runtime
->p
.filter
= lttng_filter_interpret_bytecode
;
466 runtime
->p
.link_failed
= 0;
467 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
468 dbg_printf("Linking successful.\n");
472 runtime
->p
.filter
= lttng_filter_false
;
473 runtime
->p
.link_failed
= 1;
474 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
476 dbg_printf("Linking failed.\n");
480 void lttng_filter_sync_state(struct lttng_bytecode_runtime
*runtime
)
482 struct lttng_ust_filter_bytecode_node
*bc
= runtime
->bc
;
484 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
485 runtime
->filter
= lttng_filter_false
;
487 runtime
->filter
= lttng_filter_interpret_bytecode
;
491 * Link bytecode for all enablers referenced by an event.
493 void lttng_enabler_event_link_bytecode(struct lttng_event
*event
,
494 struct lttng_enabler
*enabler
)
496 struct lttng_ust_filter_bytecode_node
*bc
;
497 struct lttng_bytecode_runtime
*runtime
;
499 /* Can only be called for events with desc attached */
502 /* Link each bytecode. */
503 cds_list_for_each_entry(bc
, &enabler
->filter_bytecode_head
, node
) {
505 struct cds_list_head
*insert_loc
;
507 cds_list_for_each_entry(runtime
,
508 &event
->bytecode_runtime_head
, node
) {
509 if (runtime
->bc
== bc
) {
514 /* Skip bytecode already linked */
519 * Insert at specified priority (seqnum) in increasing
522 cds_list_for_each_entry_reverse(runtime
,
523 &event
->bytecode_runtime_head
, node
) {
524 if (runtime
->bc
->bc
.seqnum
< bc
->bc
.seqnum
) {
526 insert_loc
= &runtime
->node
;
530 /* Add to head to list */
531 insert_loc
= &event
->bytecode_runtime_head
;
533 dbg_printf("linking bytecode\n");
534 ret
= _lttng_filter_event_link_bytecode(event
, bc
,
537 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
543 * We own the filter_bytecode if we return success.
545 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
546 struct lttng_ust_filter_bytecode_node
*filter_bytecode
)
548 cds_list_add(&filter_bytecode
->node
, &enabler
->filter_bytecode_head
);
552 void lttng_free_enabler_filter_bytecode(struct lttng_enabler
*enabler
)
554 struct lttng_ust_filter_bytecode_node
*filter_bytecode
, *tmp
;
556 cds_list_for_each_entry_safe(filter_bytecode
, tmp
,
557 &enabler
->filter_bytecode_head
, node
) {
558 free(filter_bytecode
);
562 void lttng_free_event_filter_runtime(struct lttng_event
*event
)
564 struct bytecode_runtime
*runtime
, *tmp
;
566 cds_list_for_each_entry_safe(runtime
, tmp
,
567 &event
->bytecode_runtime_head
, p
.node
) {