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",
76 [ FILTER_OP_UNARY_PLUS
] = "UNARY_PLUS",
77 [ FILTER_OP_UNARY_MINUS
] = "UNARY_MINUS",
78 [ FILTER_OP_UNARY_NOT
] = "UNARY_NOT",
79 [ FILTER_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
80 [ FILTER_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
81 [ FILTER_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
82 [ FILTER_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
83 [ FILTER_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
84 [ FILTER_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
87 [ FILTER_OP_AND
] = "AND",
88 [ FILTER_OP_OR
] = "OR",
91 [ FILTER_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
92 [ FILTER_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
93 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
94 [ FILTER_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
95 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
97 [ FILTER_OP_LOAD_STRING
] = "LOAD_STRING",
98 [ FILTER_OP_LOAD_S64
] = "LOAD_S64",
99 [ FILTER_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
102 [ FILTER_OP_CAST_TO_S64
] = "CAST_TO_S64",
103 [ FILTER_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
104 [ FILTER_OP_CAST_NOP
] = "CAST_NOP",
107 const char *print_op(enum filter_op op
)
109 if (op
>= NR_FILTER_OPS
)
116 int apply_field_reloc(struct ltt_event
*event
,
117 struct bytecode_runtime
*runtime
,
118 uint32_t runtime_len
,
119 uint32_t reloc_offset
,
120 const char *field_name
)
122 const struct lttng_event_desc
*desc
;
123 const struct lttng_event_field
*fields
, *field
= NULL
;
124 unsigned int nr_fields
, i
;
125 struct field_ref
*field_ref
;
127 uint32_t field_offset
= 0;
129 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, field_name
);
131 /* Ensure that the reloc is within the code */
132 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
135 /* Lookup event by name */
139 fields
= desc
->fields
;
142 nr_fields
= desc
->nr_fields
;
143 for (i
= 0; i
< nr_fields
; i
++) {
144 if (!strcmp(fields
[i
].name
, field_name
)) {
148 /* compute field offset */
149 switch (fields
[i
].type
.atype
) {
152 field_offset
+= sizeof(int64_t);
156 field_offset
+= sizeof(unsigned long);
157 field_offset
+= sizeof(void *);
160 field_offset
+= sizeof(void *);
163 field_offset
+= sizeof(double);
172 /* Check if field offset is too large for 16-bit offset */
173 if (field_offset
> FILTER_BYTECODE_MAX_LEN
)
177 op
= (struct load_op
*) &runtime
->data
[reloc_offset
];
178 field_ref
= (struct field_ref
*) op
->data
;
179 switch (field
->type
.atype
) {
182 op
->op
= FILTER_OP_LOAD_FIELD_REF_S64
;
186 op
->op
= FILTER_OP_LOAD_FIELD_REF_SEQUENCE
;
189 op
->op
= FILTER_OP_LOAD_FIELD_REF_STRING
;
192 op
->op
= FILTER_OP_LOAD_FIELD_REF_DOUBLE
;
198 field_ref
->offset
= (uint16_t) field_offset
;
203 * Take a bytecode with reloc table and link it to an event to create a
207 int _lttng_filter_event_link_bytecode(struct ltt_event
*event
,
208 struct lttng_ust_filter_bytecode
*filter_bytecode
)
210 int ret
, offset
, next_offset
;
211 struct bytecode_runtime
*runtime
= NULL
;
212 size_t runtime_alloc_len
;
214 if (!filter_bytecode
)
216 /* Even is not connected to any description */
219 /* Bytecode already linked */
220 if (event
->filter
|| event
->filter_data
)
223 dbg_printf("Linking\n");
225 /* We don't need the reloc table in the runtime */
226 runtime_alloc_len
= sizeof(*runtime
) + filter_bytecode
->reloc_offset
;
227 runtime
= zmalloc(runtime_alloc_len
);
232 runtime
->len
= filter_bytecode
->reloc_offset
;
233 /* copy original bytecode */
234 memcpy(runtime
->data
, filter_bytecode
->data
, runtime
->len
);
236 * apply relocs. Those are a uint16_t (offset in bytecode)
237 * followed by a string (field name).
239 for (offset
= filter_bytecode
->reloc_offset
;
240 offset
< filter_bytecode
->len
;
241 offset
= next_offset
) {
242 uint16_t reloc_offset
=
243 *(uint16_t *) &filter_bytecode
->data
[offset
];
244 const char *field_name
=
245 (const char *) &filter_bytecode
->data
[offset
+ sizeof(uint16_t)];
247 ret
= apply_field_reloc(event
, runtime
, runtime
->len
, reloc_offset
, field_name
);
251 next_offset
= offset
+ sizeof(uint16_t) + strlen(field_name
) + 1;
253 /* Validate bytecode */
254 ret
= lttng_filter_validate_bytecode(runtime
);
258 /* Specialize bytecode */
259 ret
= lttng_filter_specialize_bytecode(runtime
);
263 event
->filter_data
= runtime
;
264 event
->filter
= lttng_filter_interpret_bytecode
;
268 event
->filter
= lttng_filter_false
;
273 void lttng_filter_event_link_bytecode(struct ltt_event
*event
,
274 struct lttng_ust_filter_bytecode
*filter_bytecode
)
278 ret
= _lttng_filter_event_link_bytecode(event
, filter_bytecode
);
280 fprintf(stderr
, "[lttng filter] error linking event bytecode\n");
285 * Link bytecode to all events for a wildcard. Skips events that already
286 * have a bytecode linked.
287 * We do not set each event's filter_bytecode field, because they do not
288 * own the filter_bytecode: the wildcard owns it.
290 void lttng_filter_wildcard_link_bytecode(struct session_wildcard
*wildcard
)
292 struct ltt_event
*event
;
295 if (!wildcard
->filter_bytecode
)
298 cds_list_for_each_entry(event
, &wildcard
->events
, wildcard_list
) {
301 ret
= _lttng_filter_event_link_bytecode(event
,
302 wildcard
->filter_bytecode
);
304 fprintf(stderr
, "[lttng filter] error linking wildcard bytecode\n");
312 * Need to attach filter to an event before starting tracing for the
313 * session. We own the filter_bytecode if we return success.
315 int lttng_filter_event_attach_bytecode(struct ltt_event
*event
,
316 struct lttng_ust_filter_bytecode
*filter_bytecode
)
318 if (event
->chan
->session
->been_active
)
320 if (event
->filter_bytecode
)
322 event
->filter_bytecode
= filter_bytecode
;
327 * Need to attach filter to a wildcard before starting tracing for the
328 * session. We own the filter_bytecode if we return success.
330 int lttng_filter_wildcard_attach_bytecode(struct session_wildcard
*wildcard
,
331 struct lttng_ust_filter_bytecode
*filter_bytecode
)
333 if (wildcard
->chan
->session
->been_active
)
335 if (wildcard
->filter_bytecode
)
337 wildcard
->filter_bytecode
= filter_bytecode
;