Performance: define _LGPL_SOURCE in LGPL c files
[lttng-ust.git] / liblttng-ust / lttng-filter.c
CommitLineData
2d78951a
MD
1/*
2 * lttng-filter.c
3 *
4 * LTTng UST filter code.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
3fbec7dc 23#define _LGPL_SOURCE
f488575f 24#include <urcu/rculist.h>
97b58163 25#include "lttng-filter.h"
cd54f6d9
MD
26
27static const char *opnames[] = {
28 [ FILTER_OP_UNKNOWN ] = "UNKNOWN",
29
30 [ FILTER_OP_RETURN ] = "RETURN",
31
32 /* binary */
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",
226106c0
MD
43
44 /* binary comparators */
cd54f6d9
MD
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",
51
226106c0
MD
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",
59
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",
67
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",
75
1e5f62b4
MD
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",
83
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",
226106c0 90
cd54f6d9
MD
91 /* unary */
92 [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS",
93 [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS",
94 [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT",
226106c0
MD
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",
cd54f6d9
MD
101
102 /* logical */
103 [ FILTER_OP_AND ] = "AND",
104 [ FILTER_OP_OR ] = "OR",
105
77aa5901 106 /* load field ref */
cd54f6d9 107 [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF",
2f0145d1
MD
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",
112
77aa5901 113 /* load from immediate operand */
cd54f6d9
MD
114 [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING",
115 [ FILTER_OP_LOAD_S64 ] = "LOAD_S64",
da6eed25 116 [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
49905038
MD
117
118 /* cast */
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",
77aa5901
MD
122
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",
cd54f6d9
MD
128};
129
cd54f6d9
MD
130const char *print_op(enum filter_op op)
131{
132 if (op >= NR_FILTER_OPS)
133 return "UNKNOWN";
134 else
135 return opnames[op];
136}
137
cd54f6d9 138static
7dd08bec 139int apply_field_reloc(struct lttng_event *event,
cd54f6d9
MD
140 struct bytecode_runtime *runtime,
141 uint32_t runtime_len,
142 uint32_t reloc_offset,
143 const char *field_name)
144{
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;
2f0145d1 149 struct load_op *op;
cd54f6d9
MD
150 uint32_t field_offset = 0;
151
77aa5901 152 dbg_printf("Apply field reloc: %u %s\n", reloc_offset, field_name);
cd54f6d9
MD
153
154 /* Lookup event by name */
155 desc = event->desc;
156 if (!desc)
157 return -EINVAL;
158 fields = desc->fields;
159 if (!fields)
160 return -EINVAL;
161 nr_fields = desc->nr_fields;
162 for (i = 0; i < nr_fields; i++) {
163 if (!strcmp(fields[i].name, field_name)) {
164 field = &fields[i];
165 break;
166 }
167 /* compute field offset */
168 switch (fields[i].type.atype) {
169 case atype_integer:
170 case atype_enum:
171 field_offset += sizeof(int64_t);
172 break;
173 case atype_array:
174 case atype_sequence:
175 field_offset += sizeof(unsigned long);
176 field_offset += sizeof(void *);
177 break;
178 case atype_string:
179 field_offset += sizeof(void *);
180 break;
181 case atype_float:
182 field_offset += sizeof(double);
da6eed25 183 break;
cd54f6d9
MD
184 default:
185 return -EINVAL;
186 }
187 }
188 if (!field)
189 return -EINVAL;
190
191 /* Check if field offset is too large for 16-bit offset */
5b4839a8 192 if (field_offset > FILTER_BYTECODE_MAX_LEN - 1)
cd54f6d9
MD
193 return -EINVAL;
194
195 /* set type */
2f0145d1
MD
196 op = (struct load_op *) &runtime->data[reloc_offset];
197 field_ref = (struct field_ref *) op->data;
cd54f6d9
MD
198 switch (field->type.atype) {
199 case atype_integer:
200 case atype_enum:
2f0145d1 201 op->op = FILTER_OP_LOAD_FIELD_REF_S64;
cd54f6d9
MD
202 break;
203 case atype_array:
204 case atype_sequence:
2f0145d1 205 op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE;
cd54f6d9
MD
206 break;
207 case atype_string:
2f0145d1 208 op->op = FILTER_OP_LOAD_FIELD_REF_STRING;
cd54f6d9
MD
209 break;
210 case atype_float:
2f0145d1 211 op->op = FILTER_OP_LOAD_FIELD_REF_DOUBLE;
da6eed25 212 break;
cd54f6d9
MD
213 default:
214 return -EINVAL;
215 }
216 /* set offset */
217 field_ref->offset = (uint16_t) field_offset;
2d78951a
MD
218 return 0;
219}
220
77aa5901
MD
221static
222int 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)
227{
228 struct field_ref *field_ref;
229 struct load_op *op;
230 struct lttng_ctx_field *ctx_field;
231 int idx;
53569322 232 struct lttng_session *session = runtime->p.session;
77aa5901
MD
233
234 dbg_printf("Apply context reloc: %u %s\n", reloc_offset, context_name);
235
236 /* Get context index */
53569322
MD
237 idx = lttng_get_context_index(session->ctx, context_name);
238 if (idx < 0) {
239 if (lttng_context_is_app(context_name)) {
240 int ret;
241
242 ret = lttng_ust_add_app_context_to_ctx_rcu(context_name,
243 &session->ctx);
244 if (ret)
245 return ret;
246 idx = lttng_get_context_index(session->ctx,
247 context_name);
248 if (idx < 0)
249 return -ENOENT;
250 } else {
251 return -ENOENT;
252 }
253 }
77aa5901
MD
254 /* Check if idx is too large for 16-bit offset */
255 if (idx > FILTER_BYTECODE_MAX_LEN - 1)
256 return -EINVAL;
257
258 /* Get context return type */
53569322 259 ctx_field = &session->ctx->fields[idx];
77aa5901
MD
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) {
263 case atype_integer:
264 case atype_enum:
265 op->op = FILTER_OP_GET_CONTEXT_REF_S64;
266 break;
267 /* Sequence and array supported as string */
268 case atype_string:
269 case atype_array:
270 case atype_sequence:
271 op->op = FILTER_OP_GET_CONTEXT_REF_STRING;
272 break;
273 case atype_float:
274 op->op = FILTER_OP_GET_CONTEXT_REF_DOUBLE;
275 break;
53569322
MD
276 case atype_dynamic:
277 op->op = FILTER_OP_GET_CONTEXT_REF;
278 break;
77aa5901
MD
279 default:
280 return -EINVAL;
281 }
282 /* set offset to context index within channel contexts */
283 field_ref->offset = (uint16_t) idx;
284 return 0;
285}
286
287static
288int apply_reloc(struct lttng_event *event,
289 struct bytecode_runtime *runtime,
290 uint32_t runtime_len,
291 uint32_t reloc_offset,
292 const char *name)
293{
294 struct load_op *op;
295
296 dbg_printf("Apply reloc: %u %s\n", reloc_offset, name);
297
298 /* Ensure that the reloc is within the code */
299 if (runtime_len - reloc_offset < sizeof(uint16_t))
300 return -EINVAL;
301
302 op = (struct load_op *) &runtime->data[reloc_offset];
303 switch (op->op) {
304 case FILTER_OP_LOAD_FIELD_REF:
305 return apply_field_reloc(event, runtime, runtime_len,
306 reloc_offset, name);
307 case FILTER_OP_GET_CONTEXT_REF:
308 return apply_context_reloc(event, runtime, runtime_len,
309 reloc_offset, name);
310 default:
311 ERR("Unknown reloc op type %u\n", op->op);
312 return -EINVAL;
313 }
314 return 0;
315}
316
f488575f
MD
317static
318int bytecode_is_linked(struct lttng_ust_filter_bytecode_node *filter_bytecode,
7dd08bec 319 struct lttng_event *event)
f488575f
MD
320{
321 struct lttng_bytecode_runtime *bc_runtime;
322
e58095ef
MD
323 cds_list_for_each_entry(bc_runtime,
324 &event->bytecode_runtime_head, node) {
f488575f
MD
325 if (bc_runtime->bc == filter_bytecode)
326 return 1;
327 }
328 return 0;
329}
330
cd54f6d9
MD
331/*
332 * Take a bytecode with reloc table and link it to an event to create a
333 * bytecode runtime.
334 */
2d78951a 335static
7dd08bec 336int _lttng_filter_event_link_bytecode(struct lttng_event *event,
e58095ef
MD
337 struct lttng_ust_filter_bytecode_node *filter_bytecode,
338 struct cds_list_head *insert_loc)
2d78951a 339{
cd54f6d9
MD
340 int ret, offset, next_offset;
341 struct bytecode_runtime *runtime = NULL;
342 size_t runtime_alloc_len;
343
2d78951a
MD
344 if (!filter_bytecode)
345 return 0;
cd54f6d9 346 /* Bytecode already linked */
f488575f 347 if (bytecode_is_linked(filter_bytecode, event))
cd54f6d9 348 return 0;
2d78951a 349
f488575f 350 dbg_printf("Linking...\n");
cd54f6d9
MD
351
352 /* We don't need the reloc table in the runtime */
f488575f 353 runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset;
cd54f6d9
MD
354 runtime = zmalloc(runtime_alloc_len);
355 if (!runtime) {
356 ret = -ENOMEM;
e0a7d7ab 357 goto alloc_error;
cd54f6d9 358 }
f488575f 359 runtime->p.bc = filter_bytecode;
53569322 360 runtime->p.session = event->chan->session;
f488575f 361 runtime->len = filter_bytecode->bc.reloc_offset;
cd54f6d9 362 /* copy original bytecode */
f488575f 363 memcpy(runtime->data, filter_bytecode->bc.data, runtime->len);
cd54f6d9
MD
364 /*
365 * apply relocs. Those are a uint16_t (offset in bytecode)
366 * followed by a string (field name).
367 */
f488575f
MD
368 for (offset = filter_bytecode->bc.reloc_offset;
369 offset < filter_bytecode->bc.len;
cd54f6d9
MD
370 offset = next_offset) {
371 uint16_t reloc_offset =
f488575f 372 *(uint16_t *) &filter_bytecode->bc.data[offset];
77aa5901 373 const char *name =
f488575f 374 (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)];
cd54f6d9 375
77aa5901 376 ret = apply_reloc(event, runtime, runtime->len, reloc_offset, name);
cd54f6d9
MD
377 if (ret) {
378 goto link_error;
379 }
77aa5901 380 next_offset = offset + sizeof(uint16_t) + strlen(name) + 1;
cd54f6d9 381 }
9522a886
MD
382 /* Validate bytecode */
383 ret = lttng_filter_validate_bytecode(runtime);
384 if (ret) {
385 goto link_error;
386 }
08c84b15
MD
387 /* Specialize bytecode */
388 ret = lttng_filter_specialize_bytecode(runtime);
389 if (ret) {
390 goto link_error;
391 }
f488575f 392 runtime->p.filter = lttng_filter_interpret_bytecode;
21af05a9 393 runtime->p.link_failed = 0;
e58095ef 394 cds_list_add_rcu(&runtime->p.node, insert_loc);
f488575f 395 dbg_printf("Linking successful.\n");
2d78951a 396 return 0;
cd54f6d9
MD
397
398link_error:
f488575f 399 runtime->p.filter = lttng_filter_false;
21af05a9 400 runtime->p.link_failed = 1;
e58095ef 401 cds_list_add_rcu(&runtime->p.node, insert_loc);
e0a7d7ab 402alloc_error:
f488575f 403 dbg_printf("Linking failed.\n");
cd54f6d9 404 return ret;
2d78951a
MD
405}
406
e58095ef 407void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime)
2d78951a 408{
e58095ef 409 struct lttng_ust_filter_bytecode_node *bc = runtime->bc;
f488575f 410
21af05a9 411 if (!bc->enabler->enabled || runtime->link_failed)
e58095ef 412 runtime->filter = lttng_filter_false;
21af05a9
MD
413 else
414 runtime->filter = lttng_filter_interpret_bytecode;
2d78951a
MD
415}
416
417/*
e58095ef 418 * Link bytecode for all enablers referenced by an event.
2d78951a 419 */
e58095ef
MD
420void lttng_enabler_event_link_bytecode(struct lttng_event *event,
421 struct lttng_enabler *enabler)
2d78951a 422{
e58095ef
MD
423 struct lttng_ust_filter_bytecode_node *bc;
424 struct lttng_bytecode_runtime *runtime;
425
426 /* Can only be called for events with desc attached */
427 assert(event->desc);
428
429 /* Link each bytecode. */
430 cds_list_for_each_entry(bc, &enabler->filter_bytecode_head, node) {
431 int found = 0, ret;
432 struct cds_list_head *insert_loc;
433
434 cds_list_for_each_entry(runtime,
435 &event->bytecode_runtime_head, node) {
436 if (runtime->bc == bc) {
437 found = 1;
438 break;
439 }
440 }
441 /* Skip bytecode already linked */
442 if (found)
443 continue;
444
445 /*
446 * Insert at specified priority (seqnum) in increasing
447 * order.
448 */
449 cds_list_for_each_entry_reverse(runtime,
450 &event->bytecode_runtime_head, node) {
451 if (runtime->bc->bc.seqnum < bc->bc.seqnum) {
452 /* insert here */
453 insert_loc = &runtime->node;
454 goto add_within;
455 }
456 }
457 /* Add to head to list */
458 insert_loc = &event->bytecode_runtime_head;
459 add_within:
f488575f 460 dbg_printf("linking bytecode\n");
e58095ef
MD
461 ret = _lttng_filter_event_link_bytecode(event, bc,
462 insert_loc);
463 if (ret) {
464 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
465 }
2d78951a 466 }
2d78951a
MD
467}
468
469/*
e58095ef 470 * We own the filter_bytecode if we return success.
2d78951a 471 */
e58095ef 472int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
f488575f 473 struct lttng_ust_filter_bytecode_node *filter_bytecode)
2d78951a 474{
e58095ef 475 cds_list_add(&filter_bytecode->node, &enabler->filter_bytecode_head);
2d78951a
MD
476 return 0;
477}
f488575f 478
e58095ef 479void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler)
f488575f
MD
480{
481 struct lttng_ust_filter_bytecode_node *filter_bytecode, *tmp;
482
483 cds_list_for_each_entry_safe(filter_bytecode, tmp,
e58095ef 484 &enabler->filter_bytecode_head, node) {
f488575f
MD
485 free(filter_bytecode);
486 }
487}
488
7dd08bec 489void lttng_free_event_filter_runtime(struct lttng_event *event)
f488575f
MD
490{
491 struct bytecode_runtime *runtime, *tmp;
492
493 cds_list_for_each_entry_safe(runtime, tmp,
e58095ef 494 &event->bytecode_runtime_head, p.node) {
f488575f
MD
495 free(runtime);
496 }
497}
This page took 0.052995 seconds and 4 git commands to generate.