4 * LTTng UST filter code.
6 * Copyright (C) 2010-2012 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
24 #include <urcu/rculist.h>
25 #include "lttng-filter.h"
27 static const char *opnames
[] = {
28 [ FILTER_OP_UNKNOWN
] = "UNKNOWN",
30 [ FILTER_OP_RETURN
] = "RETURN",
33 [ FILTER_OP_MUL
] = "MUL",
34 [ FILTER_OP_DIV
] = "DIV",
35 [ FILTER_OP_MOD
] = "MOD",
36 [ FILTER_OP_PLUS
] = "PLUS",
37 [ FILTER_OP_MINUS
] = "MINUS",
38 [ FILTER_OP_RSHIFT
] = "RSHIFT",
39 [ FILTER_OP_LSHIFT
] = "LSHIFT",
40 [ FILTER_OP_BIN_AND
] = "BIN_AND",
41 [ FILTER_OP_BIN_OR
] = "BIN_OR",
42 [ FILTER_OP_BIN_XOR
] = "BIN_XOR",
44 /* binary comparators */
45 [ FILTER_OP_EQ
] = "EQ",
46 [ FILTER_OP_NE
] = "NE",
47 [ FILTER_OP_GT
] = "GT",
48 [ FILTER_OP_LT
] = "LT",
49 [ FILTER_OP_GE
] = "GE",
50 [ FILTER_OP_LE
] = "LE",
52 /* string binary comparators */
53 [ FILTER_OP_EQ_STRING
] = "EQ_STRING",
54 [ FILTER_OP_NE_STRING
] = "NE_STRING",
55 [ FILTER_OP_GT_STRING
] = "GT_STRING",
56 [ FILTER_OP_LT_STRING
] = "LT_STRING",
57 [ FILTER_OP_GE_STRING
] = "GE_STRING",
58 [ FILTER_OP_LE_STRING
] = "LE_STRING",
60 /* s64 binary comparators */
61 [ FILTER_OP_EQ_S64
] = "EQ_S64",
62 [ FILTER_OP_NE_S64
] = "NE_S64",
63 [ FILTER_OP_GT_S64
] = "GT_S64",
64 [ FILTER_OP_LT_S64
] = "LT_S64",
65 [ FILTER_OP_GE_S64
] = "GE_S64",
66 [ FILTER_OP_LE_S64
] = "LE_S64",
68 /* double binary comparators */
69 [ FILTER_OP_EQ_DOUBLE
] = "EQ_DOUBLE",
70 [ FILTER_OP_NE_DOUBLE
] = "NE_DOUBLE",
71 [ FILTER_OP_GT_DOUBLE
] = "GT_DOUBLE",
72 [ FILTER_OP_LT_DOUBLE
] = "LT_DOUBLE",
73 [ FILTER_OP_GE_DOUBLE
] = "GE_DOUBLE",
74 [ FILTER_OP_LE_DOUBLE
] = "LE_DOUBLE",
76 /* Mixed S64-double binary comparators */
77 [ FILTER_OP_EQ_DOUBLE_S64
] = "EQ_DOUBLE_S64",
78 [ FILTER_OP_NE_DOUBLE_S64
] = "NE_DOUBLE_S64",
79 [ FILTER_OP_GT_DOUBLE_S64
] = "GT_DOUBLE_S64",
80 [ FILTER_OP_LT_DOUBLE_S64
] = "LT_DOUBLE_S64",
81 [ FILTER_OP_GE_DOUBLE_S64
] = "GE_DOUBLE_S64",
82 [ FILTER_OP_LE_DOUBLE_S64
] = "LE_DOUBLE_S64",
84 [ FILTER_OP_EQ_S64_DOUBLE
] = "EQ_S64_DOUBLE",
85 [ FILTER_OP_NE_S64_DOUBLE
] = "NE_S64_DOUBLE",
86 [ FILTER_OP_GT_S64_DOUBLE
] = "GT_S64_DOUBLE",
87 [ FILTER_OP_LT_S64_DOUBLE
] = "LT_S64_DOUBLE",
88 [ FILTER_OP_GE_S64_DOUBLE
] = "GE_S64_DOUBLE",
89 [ FILTER_OP_LE_S64_DOUBLE
] = "LE_S64_DOUBLE",
92 [ FILTER_OP_UNARY_PLUS
] = "UNARY_PLUS",
93 [ FILTER_OP_UNARY_MINUS
] = "UNARY_MINUS",
94 [ FILTER_OP_UNARY_NOT
] = "UNARY_NOT",
95 [ FILTER_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
96 [ FILTER_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
97 [ FILTER_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
98 [ FILTER_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
99 [ FILTER_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
100 [ FILTER_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
103 [ FILTER_OP_AND
] = "AND",
104 [ FILTER_OP_OR
] = "OR",
107 [ FILTER_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
108 [ FILTER_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
109 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
110 [ FILTER_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
111 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
113 /* load from immediate operand */
114 [ FILTER_OP_LOAD_STRING
] = "LOAD_STRING",
115 [ FILTER_OP_LOAD_S64
] = "LOAD_S64",
116 [ FILTER_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
119 [ FILTER_OP_CAST_TO_S64
] = "CAST_TO_S64",
120 [ FILTER_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
121 [ FILTER_OP_CAST_NOP
] = "CAST_NOP",
123 /* get context ref */
124 [ FILTER_OP_GET_CONTEXT_REF
] = "GET_CONTEXT_REF",
125 [ FILTER_OP_GET_CONTEXT_REF_STRING
] = "GET_CONTEXT_REF_STRING",
126 [ FILTER_OP_GET_CONTEXT_REF_S64
] = "GET_CONTEXT_REF_S64",
127 [ FILTER_OP_GET_CONTEXT_REF_DOUBLE
] = "GET_CONTEXT_REF_DOUBLE",
130 const char *print_op(enum filter_op op
)
132 if (op
>= NR_FILTER_OPS
)
139 int apply_field_reloc(struct lttng_event
*event
,
140 struct bytecode_runtime
*runtime
,
141 uint32_t runtime_len
,
142 uint32_t reloc_offset
,
143 const char *field_name
)
145 const struct lttng_event_desc
*desc
;
146 const struct lttng_event_field
*fields
, *field
= NULL
;
147 unsigned int nr_fields
, i
;
148 struct field_ref
*field_ref
;
150 uint32_t field_offset
= 0;
152 dbg_printf("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
154 /* Lookup event by name */
158 fields
= desc
->fields
;
161 nr_fields
= desc
->nr_fields
;
162 for (i
= 0; i
< nr_fields
; i
++) {
163 if (!strcmp(fields
[i
].name
, field_name
)) {
167 /* compute field offset */
168 switch (fields
[i
].type
.atype
) {
171 field_offset
+= sizeof(int64_t);
175 field_offset
+= sizeof(unsigned long);
176 field_offset
+= sizeof(void *);
179 field_offset
+= sizeof(void *);
182 field_offset
+= sizeof(double);
191 /* Check if field offset is too large for 16-bit offset */
192 if (field_offset
> FILTER_BYTECODE_MAX_LEN
- 1)
196 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
197 field_ref
= (struct field_ref
*) op
->data
;
198 switch (field
->type
.atype
) {
201 op
->op
= FILTER_OP_LOAD_FIELD_REF_S64
;
205 op
->op
= FILTER_OP_LOAD_FIELD_REF_SEQUENCE
;
208 op
->op
= FILTER_OP_LOAD_FIELD_REF_STRING
;
211 op
->op
= FILTER_OP_LOAD_FIELD_REF_DOUBLE
;
217 field_ref
->offset
= (uint16_t) field_offset
;
222 int apply_context_reloc(struct lttng_event
*event
,
223 struct bytecode_runtime
*runtime
,
224 uint32_t runtime_len
,
225 uint32_t reloc_offset
,
226 const char *context_name
)
228 struct field_ref
*field_ref
;
230 struct lttng_ctx_field
*ctx_field
;
232 struct lttng_session
*session
= runtime
->p
.session
;
234 dbg_printf("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
236 /* Get context index */
237 idx
= lttng_get_context_index(session
->ctx
, context_name
);
239 if (lttng_context_is_app(context_name
)) {
242 ret
= lttng_ust_add_app_context_to_ctx_rcu(context_name
,
246 idx
= lttng_get_context_index(session
->ctx
,
254 /* Check if idx is too large for 16-bit offset */
255 if (idx
> FILTER_BYTECODE_MAX_LEN
- 1)
258 /* Get context return type */
259 ctx_field
= &session
->ctx
->fields
[idx
];
260 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
261 field_ref
= (struct field_ref
*) op
->data
;
262 switch (ctx_field
->event_field
.type
.atype
) {
265 op
->op
= FILTER_OP_GET_CONTEXT_REF_S64
;
267 /* Sequence and array supported as string */
271 op
->op
= FILTER_OP_GET_CONTEXT_REF_STRING
;
274 op
->op
= FILTER_OP_GET_CONTEXT_REF_DOUBLE
;
277 op
->op
= FILTER_OP_GET_CONTEXT_REF
;
282 /* set offset to context index within channel contexts */
283 field_ref
->offset
= (uint16_t) idx
;
288 int apply_reloc(struct lttng_event
*event
,
289 struct bytecode_runtime
*runtime
,
290 uint32_t runtime_len
,
291 uint32_t reloc_offset
,
296 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, name
);
298 /* Ensure that the reloc is within the code */
299 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
302 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
304 case FILTER_OP_LOAD_FIELD_REF
:
305 return apply_field_reloc(event
, runtime
, runtime_len
,
307 case FILTER_OP_GET_CONTEXT_REF
:
308 return apply_context_reloc(event
, runtime
, runtime_len
,
311 ERR("Unknown reloc op type %u\n", op
->op
);
318 int bytecode_is_linked(struct lttng_ust_filter_bytecode_node
*filter_bytecode
,
319 struct lttng_event
*event
)
321 struct lttng_bytecode_runtime
*bc_runtime
;
323 cds_list_for_each_entry(bc_runtime
,
324 &event
->bytecode_runtime_head
, node
) {
325 if (bc_runtime
->bc
== filter_bytecode
)
332 * Take a bytecode with reloc table and link it to an event to create a
336 int _lttng_filter_event_link_bytecode(struct lttng_event
*event
,
337 struct lttng_ust_filter_bytecode_node
*filter_bytecode
,
338 struct cds_list_head
*insert_loc
)
340 int ret
, offset
, next_offset
;
341 struct bytecode_runtime
*runtime
= NULL
;
342 size_t runtime_alloc_len
;
344 if (!filter_bytecode
)
346 /* Bytecode already linked */
347 if (bytecode_is_linked(filter_bytecode
, event
))
350 dbg_printf("Linking...\n");
352 /* We don't need the reloc table in the runtime */
353 runtime_alloc_len
= sizeof(*runtime
) + filter_bytecode
->bc
.reloc_offset
;
354 runtime
= zmalloc(runtime_alloc_len
);
359 runtime
->p
.bc
= filter_bytecode
;
360 runtime
->p
.session
= event
->chan
->session
;
361 runtime
->len
= filter_bytecode
->bc
.reloc_offset
;
362 /* copy original bytecode */
363 memcpy(runtime
->data
, filter_bytecode
->bc
.data
, runtime
->len
);
365 * apply relocs. Those are a uint16_t (offset in bytecode)
366 * followed by a string (field name).
368 for (offset
= filter_bytecode
->bc
.reloc_offset
;
369 offset
< filter_bytecode
->bc
.len
;
370 offset
= next_offset
) {
371 uint16_t reloc_offset
=
372 *(uint16_t *) &filter_bytecode
->bc
.data
[offset
];
374 (const char *) &filter_bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
376 ret
= apply_reloc(event
, runtime
, runtime
->len
, reloc_offset
, name
);
380 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
382 /* Validate bytecode */
383 ret
= lttng_filter_validate_bytecode(runtime
);
387 /* Specialize bytecode */
388 ret
= lttng_filter_specialize_bytecode(runtime
);
392 runtime
->p
.filter
= lttng_filter_interpret_bytecode
;
393 runtime
->p
.link_failed
= 0;
394 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
395 dbg_printf("Linking successful.\n");
399 runtime
->p
.filter
= lttng_filter_false
;
400 runtime
->p
.link_failed
= 1;
401 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
403 dbg_printf("Linking failed.\n");
407 void lttng_filter_sync_state(struct lttng_bytecode_runtime
*runtime
)
409 struct lttng_ust_filter_bytecode_node
*bc
= runtime
->bc
;
411 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
412 runtime
->filter
= lttng_filter_false
;
414 runtime
->filter
= lttng_filter_interpret_bytecode
;
418 * Link bytecode for all enablers referenced by an event.
420 void lttng_enabler_event_link_bytecode(struct lttng_event
*event
,
421 struct lttng_enabler
*enabler
)
423 struct lttng_ust_filter_bytecode_node
*bc
;
424 struct lttng_bytecode_runtime
*runtime
;
426 /* Can only be called for events with desc attached */
429 /* Link each bytecode. */
430 cds_list_for_each_entry(bc
, &enabler
->filter_bytecode_head
, node
) {
432 struct cds_list_head
*insert_loc
;
434 cds_list_for_each_entry(runtime
,
435 &event
->bytecode_runtime_head
, node
) {
436 if (runtime
->bc
== bc
) {
441 /* Skip bytecode already linked */
446 * Insert at specified priority (seqnum) in increasing
449 cds_list_for_each_entry_reverse(runtime
,
450 &event
->bytecode_runtime_head
, node
) {
451 if (runtime
->bc
->bc
.seqnum
< bc
->bc
.seqnum
) {
453 insert_loc
= &runtime
->node
;
457 /* Add to head to list */
458 insert_loc
= &event
->bytecode_runtime_head
;
460 dbg_printf("linking bytecode\n");
461 ret
= _lttng_filter_event_link_bytecode(event
, bc
,
464 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
470 * We own the filter_bytecode if we return success.
472 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
473 struct lttng_ust_filter_bytecode_node
*filter_bytecode
)
475 cds_list_add(&filter_bytecode
->node
, &enabler
->filter_bytecode_head
);
479 void lttng_free_enabler_filter_bytecode(struct lttng_enabler
*enabler
)
481 struct lttng_ust_filter_bytecode_node
*filter_bytecode
, *tmp
;
483 cds_list_for_each_entry_safe(filter_bytecode
, tmp
,
484 &enabler
->filter_bytecode_head
, node
) {
485 free(filter_bytecode
);
489 void lttng_free_event_filter_runtime(struct lttng_event
*event
)
491 struct bytecode_runtime
*runtime
, *tmp
;
493 cds_list_for_each_entry_safe(runtime
, tmp
,
494 &event
->bytecode_runtime_head
, p
.node
) {