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