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
23 #include <urcu/rculist.h>
24 #include "lttng-filter.h"
26 static const char *opnames
[] = {
27 [ FILTER_OP_UNKNOWN
] = "UNKNOWN",
29 [ FILTER_OP_RETURN
] = "RETURN",
32 [ FILTER_OP_MUL
] = "MUL",
33 [ FILTER_OP_DIV
] = "DIV",
34 [ FILTER_OP_MOD
] = "MOD",
35 [ FILTER_OP_PLUS
] = "PLUS",
36 [ FILTER_OP_MINUS
] = "MINUS",
37 [ FILTER_OP_RSHIFT
] = "RSHIFT",
38 [ FILTER_OP_LSHIFT
] = "LSHIFT",
39 [ FILTER_OP_BIN_AND
] = "BIN_AND",
40 [ FILTER_OP_BIN_OR
] = "BIN_OR",
41 [ FILTER_OP_BIN_XOR
] = "BIN_XOR",
43 /* binary comparators */
44 [ FILTER_OP_EQ
] = "EQ",
45 [ FILTER_OP_NE
] = "NE",
46 [ FILTER_OP_GT
] = "GT",
47 [ FILTER_OP_LT
] = "LT",
48 [ FILTER_OP_GE
] = "GE",
49 [ FILTER_OP_LE
] = "LE",
51 /* string binary comparators */
52 [ FILTER_OP_EQ_STRING
] = "EQ_STRING",
53 [ FILTER_OP_NE_STRING
] = "NE_STRING",
54 [ FILTER_OP_GT_STRING
] = "GT_STRING",
55 [ FILTER_OP_LT_STRING
] = "LT_STRING",
56 [ FILTER_OP_GE_STRING
] = "GE_STRING",
57 [ FILTER_OP_LE_STRING
] = "LE_STRING",
59 /* s64 binary comparators */
60 [ FILTER_OP_EQ_S64
] = "EQ_S64",
61 [ FILTER_OP_NE_S64
] = "NE_S64",
62 [ FILTER_OP_GT_S64
] = "GT_S64",
63 [ FILTER_OP_LT_S64
] = "LT_S64",
64 [ FILTER_OP_GE_S64
] = "GE_S64",
65 [ FILTER_OP_LE_S64
] = "LE_S64",
67 /* double binary comparators */
68 [ FILTER_OP_EQ_DOUBLE
] = "EQ_DOUBLE",
69 [ FILTER_OP_NE_DOUBLE
] = "NE_DOUBLE",
70 [ FILTER_OP_GT_DOUBLE
] = "GT_DOUBLE",
71 [ FILTER_OP_LT_DOUBLE
] = "LT_DOUBLE",
72 [ FILTER_OP_GE_DOUBLE
] = "GE_DOUBLE",
73 [ FILTER_OP_LE_DOUBLE
] = "LE_DOUBLE",
75 /* Mixed S64-double binary comparators */
76 [ FILTER_OP_EQ_DOUBLE_S64
] = "EQ_DOUBLE_S64",
77 [ FILTER_OP_NE_DOUBLE_S64
] = "NE_DOUBLE_S64",
78 [ FILTER_OP_GT_DOUBLE_S64
] = "GT_DOUBLE_S64",
79 [ FILTER_OP_LT_DOUBLE_S64
] = "LT_DOUBLE_S64",
80 [ FILTER_OP_GE_DOUBLE_S64
] = "GE_DOUBLE_S64",
81 [ FILTER_OP_LE_DOUBLE_S64
] = "LE_DOUBLE_S64",
83 [ FILTER_OP_EQ_S64_DOUBLE
] = "EQ_S64_DOUBLE",
84 [ FILTER_OP_NE_S64_DOUBLE
] = "NE_S64_DOUBLE",
85 [ FILTER_OP_GT_S64_DOUBLE
] = "GT_S64_DOUBLE",
86 [ FILTER_OP_LT_S64_DOUBLE
] = "LT_S64_DOUBLE",
87 [ FILTER_OP_GE_S64_DOUBLE
] = "GE_S64_DOUBLE",
88 [ FILTER_OP_LE_S64_DOUBLE
] = "LE_S64_DOUBLE",
91 [ FILTER_OP_UNARY_PLUS
] = "UNARY_PLUS",
92 [ FILTER_OP_UNARY_MINUS
] = "UNARY_MINUS",
93 [ FILTER_OP_UNARY_NOT
] = "UNARY_NOT",
94 [ FILTER_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
95 [ FILTER_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
96 [ FILTER_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
97 [ FILTER_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
98 [ FILTER_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
99 [ FILTER_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
102 [ FILTER_OP_AND
] = "AND",
103 [ FILTER_OP_OR
] = "OR",
106 [ FILTER_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
107 [ FILTER_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
108 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
109 [ FILTER_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
110 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
112 /* load from immediate operand */
113 [ FILTER_OP_LOAD_STRING
] = "LOAD_STRING",
114 [ FILTER_OP_LOAD_S64
] = "LOAD_S64",
115 [ FILTER_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
118 [ FILTER_OP_CAST_TO_S64
] = "CAST_TO_S64",
119 [ FILTER_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
120 [ FILTER_OP_CAST_NOP
] = "CAST_NOP",
122 /* get context ref */
123 [ FILTER_OP_GET_CONTEXT_REF
] = "GET_CONTEXT_REF",
124 [ FILTER_OP_GET_CONTEXT_REF_STRING
] = "GET_CONTEXT_REF_STRING",
125 [ FILTER_OP_GET_CONTEXT_REF_S64
] = "GET_CONTEXT_REF_S64",
126 [ FILTER_OP_GET_CONTEXT_REF_DOUBLE
] = "GET_CONTEXT_REF_DOUBLE",
129 const char *print_op(enum filter_op op
)
131 if (op
>= NR_FILTER_OPS
)
138 int apply_field_reloc(struct lttng_event
*event
,
139 struct bytecode_runtime
*runtime
,
140 uint32_t runtime_len
,
141 uint32_t reloc_offset
,
142 const char *field_name
)
144 const struct lttng_event_desc
*desc
;
145 const struct lttng_event_field
*fields
, *field
= NULL
;
146 unsigned int nr_fields
, i
;
147 struct field_ref
*field_ref
;
149 uint32_t field_offset
= 0;
151 dbg_printf("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
153 /* Lookup event by name */
157 fields
= desc
->fields
;
160 nr_fields
= desc
->nr_fields
;
161 for (i
= 0; i
< nr_fields
; i
++) {
162 if (!strcmp(fields
[i
].name
, field_name
)) {
166 /* compute field offset */
167 switch (fields
[i
].type
.atype
) {
170 field_offset
+= sizeof(int64_t);
174 field_offset
+= sizeof(unsigned long);
175 field_offset
+= sizeof(void *);
178 field_offset
+= sizeof(void *);
181 field_offset
+= sizeof(double);
190 /* Check if field offset is too large for 16-bit offset */
191 if (field_offset
> FILTER_BYTECODE_MAX_LEN
- 1)
195 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
196 field_ref
= (struct field_ref
*) op
->data
;
197 switch (field
->type
.atype
) {
200 op
->op
= FILTER_OP_LOAD_FIELD_REF_S64
;
204 op
->op
= FILTER_OP_LOAD_FIELD_REF_SEQUENCE
;
207 op
->op
= FILTER_OP_LOAD_FIELD_REF_STRING
;
210 op
->op
= FILTER_OP_LOAD_FIELD_REF_DOUBLE
;
216 field_ref
->offset
= (uint16_t) field_offset
;
221 int apply_context_reloc(struct lttng_event
*event
,
222 struct bytecode_runtime
*runtime
,
223 uint32_t runtime_len
,
224 uint32_t reloc_offset
,
225 const char *context_name
)
227 struct field_ref
*field_ref
;
229 struct lttng_ctx_field
*ctx_field
;
232 dbg_printf("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
234 /* Get context index */
235 idx
= lttng_get_context_index(lttng_static_ctx
, context_name
);
239 /* Check if idx is too large for 16-bit offset */
240 if (idx
> FILTER_BYTECODE_MAX_LEN
- 1)
243 /* Get context return type */
244 ctx_field
= <tng_static_ctx
->fields
[idx
];
245 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
246 field_ref
= (struct field_ref
*) op
->data
;
247 switch (ctx_field
->event_field
.type
.atype
) {
250 op
->op
= FILTER_OP_GET_CONTEXT_REF_S64
;
252 /* Sequence and array supported as string */
256 op
->op
= FILTER_OP_GET_CONTEXT_REF_STRING
;
259 op
->op
= FILTER_OP_GET_CONTEXT_REF_DOUBLE
;
264 /* set offset to context index within channel contexts */
265 field_ref
->offset
= (uint16_t) idx
;
270 int apply_reloc(struct lttng_event
*event
,
271 struct bytecode_runtime
*runtime
,
272 uint32_t runtime_len
,
273 uint32_t reloc_offset
,
278 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, name
);
280 /* Ensure that the reloc is within the code */
281 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
284 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
286 case FILTER_OP_LOAD_FIELD_REF
:
287 return apply_field_reloc(event
, runtime
, runtime_len
,
289 case FILTER_OP_GET_CONTEXT_REF
:
290 return apply_context_reloc(event
, runtime
, runtime_len
,
293 ERR("Unknown reloc op type %u\n", op
->op
);
300 int bytecode_is_linked(struct lttng_ust_filter_bytecode_node
*filter_bytecode
,
301 struct lttng_event
*event
)
303 struct lttng_bytecode_runtime
*bc_runtime
;
305 cds_list_for_each_entry(bc_runtime
,
306 &event
->bytecode_runtime_head
, node
) {
307 if (bc_runtime
->bc
== filter_bytecode
)
314 * Take a bytecode with reloc table and link it to an event to create a
318 int _lttng_filter_event_link_bytecode(struct lttng_event
*event
,
319 struct lttng_ust_filter_bytecode_node
*filter_bytecode
,
320 struct cds_list_head
*insert_loc
)
322 int ret
, offset
, next_offset
;
323 struct bytecode_runtime
*runtime
= NULL
;
324 size_t runtime_alloc_len
;
326 if (!filter_bytecode
)
328 /* Bytecode already linked */
329 if (bytecode_is_linked(filter_bytecode
, event
))
332 dbg_printf("Linking...\n");
334 /* We don't need the reloc table in the runtime */
335 runtime_alloc_len
= sizeof(*runtime
) + filter_bytecode
->bc
.reloc_offset
;
336 runtime
= zmalloc(runtime_alloc_len
);
341 runtime
->p
.bc
= filter_bytecode
;
342 runtime
->len
= filter_bytecode
->bc
.reloc_offset
;
343 /* copy original bytecode */
344 memcpy(runtime
->data
, filter_bytecode
->bc
.data
, runtime
->len
);
346 * apply relocs. Those are a uint16_t (offset in bytecode)
347 * followed by a string (field name).
349 for (offset
= filter_bytecode
->bc
.reloc_offset
;
350 offset
< filter_bytecode
->bc
.len
;
351 offset
= next_offset
) {
352 uint16_t reloc_offset
=
353 *(uint16_t *) &filter_bytecode
->bc
.data
[offset
];
355 (const char *) &filter_bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
357 ret
= apply_reloc(event
, runtime
, runtime
->len
, reloc_offset
, name
);
361 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
363 /* Validate bytecode */
364 ret
= lttng_filter_validate_bytecode(runtime
);
368 /* Specialize bytecode */
369 ret
= lttng_filter_specialize_bytecode(runtime
);
373 runtime
->p
.filter
= lttng_filter_interpret_bytecode
;
374 runtime
->p
.link_failed
= 0;
375 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
376 dbg_printf("Linking successful.\n");
380 runtime
->p
.filter
= lttng_filter_false
;
381 runtime
->p
.link_failed
= 1;
382 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
384 dbg_printf("Linking failed.\n");
388 void lttng_filter_sync_state(struct lttng_bytecode_runtime
*runtime
)
390 struct lttng_ust_filter_bytecode_node
*bc
= runtime
->bc
;
392 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
393 runtime
->filter
= lttng_filter_false
;
395 runtime
->filter
= lttng_filter_interpret_bytecode
;
399 * Link bytecode for all enablers referenced by an event.
401 void lttng_enabler_event_link_bytecode(struct lttng_event
*event
,
402 struct lttng_enabler
*enabler
)
404 struct lttng_ust_filter_bytecode_node
*bc
;
405 struct lttng_bytecode_runtime
*runtime
;
407 /* Can only be called for events with desc attached */
410 /* Link each bytecode. */
411 cds_list_for_each_entry(bc
, &enabler
->filter_bytecode_head
, node
) {
413 struct cds_list_head
*insert_loc
;
415 cds_list_for_each_entry(runtime
,
416 &event
->bytecode_runtime_head
, node
) {
417 if (runtime
->bc
== bc
) {
422 /* Skip bytecode already linked */
427 * Insert at specified priority (seqnum) in increasing
430 cds_list_for_each_entry_reverse(runtime
,
431 &event
->bytecode_runtime_head
, node
) {
432 if (runtime
->bc
->bc
.seqnum
< bc
->bc
.seqnum
) {
434 insert_loc
= &runtime
->node
;
438 /* Add to head to list */
439 insert_loc
= &event
->bytecode_runtime_head
;
441 dbg_printf("linking bytecode\n");
442 ret
= _lttng_filter_event_link_bytecode(event
, bc
,
445 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
451 * We own the filter_bytecode if we return success.
453 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
454 struct lttng_ust_filter_bytecode_node
*filter_bytecode
)
456 cds_list_add(&filter_bytecode
->node
, &enabler
->filter_bytecode_head
);
460 void lttng_free_enabler_filter_bytecode(struct lttng_enabler
*enabler
)
462 struct lttng_ust_filter_bytecode_node
*filter_bytecode
, *tmp
;
464 cds_list_for_each_entry_safe(filter_bytecode
, tmp
,
465 &enabler
->filter_bytecode_head
, node
) {
466 free(filter_bytecode
);
470 void lttng_free_event_filter_runtime(struct lttng_event
*event
)
472 struct bytecode_runtime
*runtime
, *tmp
;
474 cds_list_for_each_entry_safe(runtime
, tmp
,
475 &event
->bytecode_runtime_head
, p
.node
) {