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 "lttng-filter.h"
25 static const char *opnames
[] = {
26 [ FILTER_OP_UNKNOWN
] = "UNKNOWN",
28 [ FILTER_OP_RETURN
] = "RETURN",
31 [ FILTER_OP_MUL
] = "MUL",
32 [ FILTER_OP_DIV
] = "DIV",
33 [ FILTER_OP_MOD
] = "MOD",
34 [ FILTER_OP_PLUS
] = "PLUS",
35 [ FILTER_OP_MINUS
] = "MINUS",
36 [ FILTER_OP_RSHIFT
] = "RSHIFT",
37 [ FILTER_OP_LSHIFT
] = "LSHIFT",
38 [ FILTER_OP_BIN_AND
] = "BIN_AND",
39 [ FILTER_OP_BIN_OR
] = "BIN_OR",
40 [ FILTER_OP_BIN_XOR
] = "BIN_XOR",
42 /* binary comparators */
43 [ FILTER_OP_EQ
] = "EQ",
44 [ FILTER_OP_NE
] = "NE",
45 [ FILTER_OP_GT
] = "GT",
46 [ FILTER_OP_LT
] = "LT",
47 [ FILTER_OP_GE
] = "GE",
48 [ FILTER_OP_LE
] = "LE",
50 /* string binary comparators */
51 [ FILTER_OP_EQ_STRING
] = "EQ_STRING",
52 [ FILTER_OP_NE_STRING
] = "NE_STRING",
53 [ FILTER_OP_GT_STRING
] = "GT_STRING",
54 [ FILTER_OP_LT_STRING
] = "LT_STRING",
55 [ FILTER_OP_GE_STRING
] = "GE_STRING",
56 [ FILTER_OP_LE_STRING
] = "LE_STRING",
58 /* s64 binary comparators */
59 [ FILTER_OP_EQ_S64
] = "EQ_S64",
60 [ FILTER_OP_NE_S64
] = "NE_S64",
61 [ FILTER_OP_GT_S64
] = "GT_S64",
62 [ FILTER_OP_LT_S64
] = "LT_S64",
63 [ FILTER_OP_GE_S64
] = "GE_S64",
64 [ FILTER_OP_LE_S64
] = "LE_S64",
66 /* double binary comparators */
67 [ FILTER_OP_EQ_DOUBLE
] = "EQ_DOUBLE",
68 [ FILTER_OP_NE_DOUBLE
] = "NE_DOUBLE",
69 [ FILTER_OP_GT_DOUBLE
] = "GT_DOUBLE",
70 [ FILTER_OP_LT_DOUBLE
] = "LT_DOUBLE",
71 [ FILTER_OP_GE_DOUBLE
] = "GE_DOUBLE",
72 [ FILTER_OP_LE_DOUBLE
] = "LE_DOUBLE",
74 /* Mixed S64-double binary comparators */
75 [ FILTER_OP_EQ_DOUBLE_S64
] = "EQ_DOUBLE_S64",
76 [ FILTER_OP_NE_DOUBLE_S64
] = "NE_DOUBLE_S64",
77 [ FILTER_OP_GT_DOUBLE_S64
] = "GT_DOUBLE_S64",
78 [ FILTER_OP_LT_DOUBLE_S64
] = "LT_DOUBLE_S64",
79 [ FILTER_OP_GE_DOUBLE_S64
] = "GE_DOUBLE_S64",
80 [ FILTER_OP_LE_DOUBLE_S64
] = "LE_DOUBLE_S64",
82 [ FILTER_OP_EQ_S64_DOUBLE
] = "EQ_S64_DOUBLE",
83 [ FILTER_OP_NE_S64_DOUBLE
] = "NE_S64_DOUBLE",
84 [ FILTER_OP_GT_S64_DOUBLE
] = "GT_S64_DOUBLE",
85 [ FILTER_OP_LT_S64_DOUBLE
] = "LT_S64_DOUBLE",
86 [ FILTER_OP_GE_S64_DOUBLE
] = "GE_S64_DOUBLE",
87 [ FILTER_OP_LE_S64_DOUBLE
] = "LE_S64_DOUBLE",
90 [ FILTER_OP_UNARY_PLUS
] = "UNARY_PLUS",
91 [ FILTER_OP_UNARY_MINUS
] = "UNARY_MINUS",
92 [ FILTER_OP_UNARY_NOT
] = "UNARY_NOT",
93 [ FILTER_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
94 [ FILTER_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
95 [ FILTER_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
96 [ FILTER_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
97 [ FILTER_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
98 [ FILTER_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
101 [ FILTER_OP_AND
] = "AND",
102 [ FILTER_OP_OR
] = "OR",
105 [ FILTER_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
106 [ FILTER_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
107 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
108 [ FILTER_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
109 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
111 [ FILTER_OP_LOAD_STRING
] = "LOAD_STRING",
112 [ FILTER_OP_LOAD_S64
] = "LOAD_S64",
113 [ FILTER_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
116 [ FILTER_OP_CAST_TO_S64
] = "CAST_TO_S64",
117 [ FILTER_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
118 [ FILTER_OP_CAST_NOP
] = "CAST_NOP",
121 const char *print_op(enum filter_op op
)
123 if (op
>= NR_FILTER_OPS
)
130 int apply_field_reloc(struct ltt_event
*event
,
131 struct bytecode_runtime
*runtime
,
132 uint32_t runtime_len
,
133 uint32_t reloc_offset
,
134 const char *field_name
)
136 const struct lttng_event_desc
*desc
;
137 const struct lttng_event_field
*fields
, *field
= NULL
;
138 unsigned int nr_fields
, i
;
139 struct field_ref
*field_ref
;
141 uint32_t field_offset
= 0;
143 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, field_name
);
145 /* Ensure that the reloc is within the code */
146 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
149 /* Lookup event by name */
153 fields
= desc
->fields
;
156 nr_fields
= desc
->nr_fields
;
157 for (i
= 0; i
< nr_fields
; i
++) {
158 if (!strcmp(fields
[i
].name
, field_name
)) {
162 /* compute field offset */
163 switch (fields
[i
].type
.atype
) {
166 field_offset
+= sizeof(int64_t);
170 field_offset
+= sizeof(unsigned long);
171 field_offset
+= sizeof(void *);
174 field_offset
+= sizeof(void *);
177 field_offset
+= sizeof(double);
186 /* Check if field offset is too large for 16-bit offset */
187 if (field_offset
> FILTER_BYTECODE_MAX_LEN
)
191 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
192 field_ref
= (struct field_ref
*) op
->data
;
193 switch (field
->type
.atype
) {
196 op
->op
= FILTER_OP_LOAD_FIELD_REF_S64
;
200 op
->op
= FILTER_OP_LOAD_FIELD_REF_SEQUENCE
;
203 op
->op
= FILTER_OP_LOAD_FIELD_REF_STRING
;
206 op
->op
= FILTER_OP_LOAD_FIELD_REF_DOUBLE
;
212 field_ref
->offset
= (uint16_t) field_offset
;
217 * Take a bytecode with reloc table and link it to an event to create a
221 int _lttng_filter_event_link_bytecode(struct ltt_event
*event
,
222 struct lttng_ust_filter_bytecode
*filter_bytecode
)
224 int ret
, offset
, next_offset
;
225 struct bytecode_runtime
*runtime
= NULL
;
226 size_t runtime_alloc_len
;
228 if (!filter_bytecode
)
230 /* Even is not connected to any description */
233 /* Bytecode already linked */
234 if (event
->filter
|| event
->filter_data
)
237 dbg_printf("Linking\n");
239 /* We don't need the reloc table in the runtime */
240 runtime_alloc_len
= sizeof(*runtime
) + filter_bytecode
->reloc_offset
;
241 runtime
= zmalloc(runtime_alloc_len
);
246 runtime
->len
= filter_bytecode
->reloc_offset
;
247 /* copy original bytecode */
248 memcpy(runtime
->data
, filter_bytecode
->data
, runtime
->len
);
250 * apply relocs. Those are a uint16_t (offset in bytecode)
251 * followed by a string (field name).
253 for (offset
= filter_bytecode
->reloc_offset
;
254 offset
< filter_bytecode
->len
;
255 offset
= next_offset
) {
256 uint16_t reloc_offset
=
257 *(uint16_t *) &filter_bytecode
->data
[offset
];
258 const char *field_name
=
259 (const char *) &filter_bytecode
->data
[offset
+ sizeof(uint16_t)];
261 ret
= apply_field_reloc(event
, runtime
, runtime
->len
, reloc_offset
, field_name
);
265 next_offset
= offset
+ sizeof(uint16_t) + strlen(field_name
) + 1;
267 /* Validate bytecode */
268 ret
= lttng_filter_validate_bytecode(runtime
);
272 /* Specialize bytecode */
273 ret
= lttng_filter_specialize_bytecode(runtime
);
277 event
->filter_data
= runtime
;
278 event
->filter
= lttng_filter_interpret_bytecode
;
282 event
->filter
= lttng_filter_false
;
287 void lttng_filter_event_link_bytecode(struct ltt_event
*event
,
288 struct lttng_ust_filter_bytecode
*filter_bytecode
)
292 ret
= _lttng_filter_event_link_bytecode(event
, filter_bytecode
);
294 fprintf(stderr
, "[lttng filter] error linking event bytecode\n");
299 * Link bytecode to all events for a wildcard. Skips events that already
300 * have a bytecode linked.
301 * We do not set each event's filter_bytecode field, because they do not
302 * own the filter_bytecode: the wildcard owns it.
304 void lttng_filter_wildcard_link_bytecode(struct session_wildcard
*wildcard
)
306 struct ltt_event
*event
;
309 if (!wildcard
->filter_bytecode
)
312 cds_list_for_each_entry(event
, &wildcard
->events
, wildcard_list
) {
315 ret
= _lttng_filter_event_link_bytecode(event
,
316 wildcard
->filter_bytecode
);
318 fprintf(stderr
, "[lttng filter] error linking wildcard bytecode\n");
326 * Need to attach filter to an event before starting tracing for the
327 * session. We own the filter_bytecode if we return success.
329 int lttng_filter_event_attach_bytecode(struct ltt_event
*event
,
330 struct lttng_ust_filter_bytecode
*filter_bytecode
)
332 if (event
->chan
->session
->been_active
)
334 if (event
->filter_bytecode
)
336 event
->filter_bytecode
= filter_bytecode
;
341 * Need to attach filter to a wildcard before starting tracing for the
342 * session. We own the filter_bytecode if we return success.
344 int lttng_filter_wildcard_attach_bytecode(struct session_wildcard
*wildcard
,
345 struct lttng_ust_filter_bytecode
*filter_bytecode
)
347 if (wildcard
->chan
->session
->been_active
)
349 if (wildcard
->filter_bytecode
)
351 wildcard
->filter_bytecode
= filter_bytecode
;