4 * LTTng modules filter code.
6 * Copyright (C) 2010-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/list.h>
24 #include <linux/slab.h>
26 #include "lttng-filter.h"
28 static const char *opnames
[] = {
29 [ FILTER_OP_UNKNOWN
] = "UNKNOWN",
31 [ FILTER_OP_RETURN
] = "RETURN",
34 [ FILTER_OP_MUL
] = "MUL",
35 [ FILTER_OP_DIV
] = "DIV",
36 [ FILTER_OP_MOD
] = "MOD",
37 [ FILTER_OP_PLUS
] = "PLUS",
38 [ FILTER_OP_MINUS
] = "MINUS",
39 [ FILTER_OP_RSHIFT
] = "RSHIFT",
40 [ FILTER_OP_LSHIFT
] = "LSHIFT",
41 [ FILTER_OP_BIN_AND
] = "BIN_AND",
42 [ FILTER_OP_BIN_OR
] = "BIN_OR",
43 [ FILTER_OP_BIN_XOR
] = "BIN_XOR",
45 /* binary comparators */
46 [ FILTER_OP_EQ
] = "EQ",
47 [ FILTER_OP_NE
] = "NE",
48 [ FILTER_OP_GT
] = "GT",
49 [ FILTER_OP_LT
] = "LT",
50 [ FILTER_OP_GE
] = "GE",
51 [ FILTER_OP_LE
] = "LE",
53 /* string binary comparators */
54 [ FILTER_OP_EQ_STRING
] = "EQ_STRING",
55 [ FILTER_OP_NE_STRING
] = "NE_STRING",
56 [ FILTER_OP_GT_STRING
] = "GT_STRING",
57 [ FILTER_OP_LT_STRING
] = "LT_STRING",
58 [ FILTER_OP_GE_STRING
] = "GE_STRING",
59 [ FILTER_OP_LE_STRING
] = "LE_STRING",
61 /* s64 binary comparators */
62 [ FILTER_OP_EQ_S64
] = "EQ_S64",
63 [ FILTER_OP_NE_S64
] = "NE_S64",
64 [ FILTER_OP_GT_S64
] = "GT_S64",
65 [ FILTER_OP_LT_S64
] = "LT_S64",
66 [ FILTER_OP_GE_S64
] = "GE_S64",
67 [ FILTER_OP_LE_S64
] = "LE_S64",
69 /* double binary comparators */
70 [ FILTER_OP_EQ_DOUBLE
] = "EQ_DOUBLE",
71 [ FILTER_OP_NE_DOUBLE
] = "NE_DOUBLE",
72 [ FILTER_OP_GT_DOUBLE
] = "GT_DOUBLE",
73 [ FILTER_OP_LT_DOUBLE
] = "LT_DOUBLE",
74 [ FILTER_OP_GE_DOUBLE
] = "GE_DOUBLE",
75 [ FILTER_OP_LE_DOUBLE
] = "LE_DOUBLE",
77 /* Mixed S64-double binary comparators */
78 [ FILTER_OP_EQ_DOUBLE_S64
] = "EQ_DOUBLE_S64",
79 [ FILTER_OP_NE_DOUBLE_S64
] = "NE_DOUBLE_S64",
80 [ FILTER_OP_GT_DOUBLE_S64
] = "GT_DOUBLE_S64",
81 [ FILTER_OP_LT_DOUBLE_S64
] = "LT_DOUBLE_S64",
82 [ FILTER_OP_GE_DOUBLE_S64
] = "GE_DOUBLE_S64",
83 [ FILTER_OP_LE_DOUBLE_S64
] = "LE_DOUBLE_S64",
85 [ FILTER_OP_EQ_S64_DOUBLE
] = "EQ_S64_DOUBLE",
86 [ FILTER_OP_NE_S64_DOUBLE
] = "NE_S64_DOUBLE",
87 [ FILTER_OP_GT_S64_DOUBLE
] = "GT_S64_DOUBLE",
88 [ FILTER_OP_LT_S64_DOUBLE
] = "LT_S64_DOUBLE",
89 [ FILTER_OP_GE_S64_DOUBLE
] = "GE_S64_DOUBLE",
90 [ FILTER_OP_LE_S64_DOUBLE
] = "LE_S64_DOUBLE",
93 [ FILTER_OP_UNARY_PLUS
] = "UNARY_PLUS",
94 [ FILTER_OP_UNARY_MINUS
] = "UNARY_MINUS",
95 [ FILTER_OP_UNARY_NOT
] = "UNARY_NOT",
96 [ FILTER_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
97 [ FILTER_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
98 [ FILTER_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
99 [ FILTER_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
100 [ FILTER_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
101 [ FILTER_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
104 [ FILTER_OP_AND
] = "AND",
105 [ FILTER_OP_OR
] = "OR",
108 [ FILTER_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
109 [ FILTER_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
110 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
111 [ FILTER_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
112 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
114 /* load from immediate operand */
115 [ FILTER_OP_LOAD_STRING
] = "LOAD_STRING",
116 [ FILTER_OP_LOAD_S64
] = "LOAD_S64",
117 [ FILTER_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
120 [ FILTER_OP_CAST_TO_S64
] = "CAST_TO_S64",
121 [ FILTER_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
122 [ FILTER_OP_CAST_NOP
] = "CAST_NOP",
124 /* get context ref */
125 [ FILTER_OP_GET_CONTEXT_REF
] = "GET_CONTEXT_REF",
126 [ FILTER_OP_GET_CONTEXT_REF_STRING
] = "GET_CONTEXT_REF_STRING",
127 [ FILTER_OP_GET_CONTEXT_REF_S64
] = "GET_CONTEXT_REF_S64",
128 [ FILTER_OP_GET_CONTEXT_REF_DOUBLE
] = "GET_CONTEXT_REF_DOUBLE",
130 /* load userspace field ref */
131 [ FILTER_OP_LOAD_FIELD_REF_USER_STRING
] = "LOAD_FIELD_REF_USER_STRING",
132 [ FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE
] = "LOAD_FIELD_REF_USER_SEQUENCE",
135 const char *lttng_filter_print_op(enum filter_op op
)
137 if (op
>= NR_FILTER_OPS
)
144 int apply_field_reloc(struct lttng_event
*event
,
145 struct bytecode_runtime
*runtime
,
146 uint32_t runtime_len
,
147 uint32_t reloc_offset
,
148 const char *field_name
)
150 const struct lttng_event_desc
*desc
;
151 const struct lttng_event_field
*fields
, *field
= NULL
;
152 unsigned int nr_fields
, i
;
153 struct field_ref
*field_ref
;
155 uint32_t field_offset
= 0;
157 dbg_printk("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
159 /* Lookup event by name */
163 fields
= desc
->fields
;
166 nr_fields
= desc
->nr_fields
;
167 for (i
= 0; i
< nr_fields
; i
++) {
168 if (!strcmp(fields
[i
].name
, field_name
)) {
172 /* compute field offset */
173 switch (fields
[i
].type
.atype
) {
176 field_offset
+= sizeof(int64_t);
180 field_offset
+= sizeof(unsigned long);
181 field_offset
+= sizeof(void *);
184 field_offset
+= sizeof(void *);
193 /* Check if field offset is too large for 16-bit offset */
194 if (field_offset
> LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN
- 1)
198 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
199 field_ref
= (struct field_ref
*) op
->data
;
200 switch (field
->type
.atype
) {
203 op
->op
= FILTER_OP_LOAD_FIELD_REF_S64
;
208 op
->op
= FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE
;
210 op
->op
= FILTER_OP_LOAD_FIELD_REF_SEQUENCE
;
214 op
->op
= FILTER_OP_LOAD_FIELD_REF_USER_STRING
;
216 op
->op
= FILTER_OP_LOAD_FIELD_REF_STRING
;
222 field_ref
->offset
= (uint16_t) field_offset
;
227 int apply_context_reloc(struct lttng_event
*event
,
228 struct bytecode_runtime
*runtime
,
229 uint32_t runtime_len
,
230 uint32_t reloc_offset
,
231 const char *context_name
)
233 struct field_ref
*field_ref
;
235 struct lttng_ctx_field
*ctx_field
;
238 dbg_printk("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
240 /* Get context index */
241 idx
= lttng_get_context_index(lttng_static_ctx
, context_name
);
245 /* Check if idx is too large for 16-bit offset */
246 if (idx
> LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN
- 1)
249 /* Get context return type */
250 ctx_field
= <tng_static_ctx
->fields
[idx
];
251 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
252 field_ref
= (struct field_ref
*) op
->data
;
253 switch (ctx_field
->event_field
.type
.atype
) {
256 op
->op
= FILTER_OP_GET_CONTEXT_REF_S64
;
258 /* Sequence and array supported as string */
262 BUG_ON(ctx_field
->event_field
.user
);
263 op
->op
= FILTER_OP_GET_CONTEXT_REF_STRING
;
268 /* set offset to context index within channel contexts */
269 field_ref
->offset
= (uint16_t) idx
;
274 int apply_reloc(struct lttng_event
*event
,
275 struct bytecode_runtime
*runtime
,
276 uint32_t runtime_len
,
277 uint32_t reloc_offset
,
282 dbg_printk("Apply reloc: %u %s\n", reloc_offset
, name
);
284 /* Ensure that the reloc is within the code */
285 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
288 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
290 case FILTER_OP_LOAD_FIELD_REF
:
291 return apply_field_reloc(event
, runtime
, runtime_len
,
293 case FILTER_OP_GET_CONTEXT_REF
:
294 return apply_context_reloc(event
, runtime
, runtime_len
,
297 printk(KERN_WARNING
"Unknown reloc op type %u\n", op
->op
);
304 int bytecode_is_linked(struct lttng_filter_bytecode_node
*filter_bytecode
,
305 struct lttng_event
*event
)
307 struct lttng_bytecode_runtime
*bc_runtime
;
309 list_for_each_entry(bc_runtime
,
310 &event
->bytecode_runtime_head
, node
) {
311 if (bc_runtime
->bc
== filter_bytecode
)
318 * Take a bytecode with reloc table and link it to an event to create a
322 int _lttng_filter_event_link_bytecode(struct lttng_event
*event
,
323 struct lttng_filter_bytecode_node
*filter_bytecode
,
324 struct list_head
*insert_loc
)
326 int ret
, offset
, next_offset
;
327 struct bytecode_runtime
*runtime
= NULL
;
328 size_t runtime_alloc_len
;
330 if (!filter_bytecode
)
332 /* Bytecode already linked */
333 if (bytecode_is_linked(filter_bytecode
, event
))
336 dbg_printk("Linking...\n");
338 /* We don't need the reloc table in the runtime */
339 runtime_alloc_len
= sizeof(*runtime
) + filter_bytecode
->bc
.reloc_offset
;
340 runtime
= kzalloc(runtime_alloc_len
, GFP_KERNEL
);
345 runtime
->p
.bc
= filter_bytecode
;
346 runtime
->len
= filter_bytecode
->bc
.reloc_offset
;
347 /* copy original bytecode */
348 memcpy(runtime
->data
, filter_bytecode
->bc
.data
, runtime
->len
);
350 * apply relocs. Those are a uint16_t (offset in bytecode)
351 * followed by a string (field name).
353 for (offset
= filter_bytecode
->bc
.reloc_offset
;
354 offset
< filter_bytecode
->bc
.len
;
355 offset
= next_offset
) {
356 uint16_t reloc_offset
=
357 *(uint16_t *) &filter_bytecode
->bc
.data
[offset
];
359 (const char *) &filter_bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
361 ret
= apply_reloc(event
, runtime
, runtime
->len
, reloc_offset
, name
);
365 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
367 /* Validate bytecode */
368 ret
= lttng_filter_validate_bytecode(runtime
);
372 /* Specialize bytecode */
373 ret
= lttng_filter_specialize_bytecode(runtime
);
377 runtime
->p
.filter
= lttng_filter_interpret_bytecode
;
378 runtime
->p
.link_failed
= 0;
379 list_add_rcu(&runtime
->p
.node
, insert_loc
);
380 dbg_printk("Linking successful.\n");
384 runtime
->p
.filter
= lttng_filter_false
;
385 runtime
->p
.link_failed
= 1;
386 list_add_rcu(&runtime
->p
.node
, insert_loc
);
388 dbg_printk("Linking failed.\n");
392 void lttng_filter_sync_state(struct lttng_bytecode_runtime
*runtime
)
394 struct lttng_filter_bytecode_node
*bc
= runtime
->bc
;
396 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
397 runtime
->filter
= lttng_filter_false
;
399 runtime
->filter
= lttng_filter_interpret_bytecode
;
403 * Link bytecode for all enablers referenced by an event.
405 void lttng_enabler_event_link_bytecode(struct lttng_event
*event
,
406 struct lttng_enabler
*enabler
)
408 struct lttng_filter_bytecode_node
*bc
;
409 struct lttng_bytecode_runtime
*runtime
;
411 /* Can only be called for events with desc attached */
412 WARN_ON_ONCE(!event
->desc
);
414 /* Link each bytecode. */
415 list_for_each_entry(bc
, &enabler
->filter_bytecode_head
, node
) {
417 struct list_head
*insert_loc
;
419 list_for_each_entry(runtime
,
420 &event
->bytecode_runtime_head
, node
) {
421 if (runtime
->bc
== bc
) {
426 /* Skip bytecode already linked */
431 * Insert at specified priority (seqnum) in increasing
434 list_for_each_entry_reverse(runtime
,
435 &event
->bytecode_runtime_head
, node
) {
436 if (runtime
->bc
->bc
.seqnum
< bc
->bc
.seqnum
) {
438 insert_loc
= &runtime
->node
;
442 /* Add to head to list */
443 insert_loc
= &event
->bytecode_runtime_head
;
445 dbg_printk("linking bytecode\n");
446 ret
= _lttng_filter_event_link_bytecode(event
, bc
,
449 dbg_printk("[lttng filter] warning: cannot link event bytecode\n");
455 * We own the filter_bytecode if we return success.
457 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
458 struct lttng_filter_bytecode_node
*filter_bytecode
)
460 list_add(&filter_bytecode
->node
, &enabler
->filter_bytecode_head
);
464 void lttng_free_enabler_filter_bytecode(struct lttng_enabler
*enabler
)
466 struct lttng_filter_bytecode_node
*filter_bytecode
, *tmp
;
468 list_for_each_entry_safe(filter_bytecode
, tmp
,
469 &enabler
->filter_bytecode_head
, node
) {
470 kfree(filter_bytecode
);
474 void lttng_free_event_filter_runtime(struct lttng_event
*event
)
476 struct bytecode_runtime
*runtime
, *tmp
;
478 list_for_each_entry_safe(runtime
, tmp
,
479 &event
->bytecode_runtime_head
, p
.node
) {