911f8e7848aa4c3d6a907cdf56715e5213a78084
[lttng-ust.git] / liblttng-ust / lttng-filter.c
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
23 #define _LGPL_SOURCE
24 #include <urcu/rculist.h>
25 #include "lttng-filter.h"
26
27 static 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",
43
44 /* binary comparators */
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
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
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",
90
91 /* unary */
92 [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS",
93 [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS",
94 [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT",
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",
101
102 /* logical */
103 [ FILTER_OP_AND ] = "AND",
104 [ FILTER_OP_OR ] = "OR",
105
106 /* load field ref */
107 [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF",
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
113 /* load from immediate operand */
114 [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING",
115 [ FILTER_OP_LOAD_S64 ] = "LOAD_S64",
116 [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
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",
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",
128 };
129
130 const 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
138 static
139 int apply_field_reloc(struct lttng_event *event,
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;
149 struct load_op *op;
150 uint32_t field_offset = 0;
151
152 dbg_printf("Apply field reloc: %u %s\n", reloc_offset, field_name);
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);
183 break;
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 */
192 if (field_offset > FILTER_BYTECODE_MAX_LEN - 1)
193 return -EINVAL;
194
195 /* set type */
196 op = (struct load_op *) &runtime->data[reloc_offset];
197 field_ref = (struct field_ref *) op->data;
198 switch (field->type.atype) {
199 case atype_integer:
200 case atype_enum:
201 op->op = FILTER_OP_LOAD_FIELD_REF_S64;
202 break;
203 case atype_array:
204 case atype_sequence:
205 op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE;
206 break;
207 case atype_string:
208 op->op = FILTER_OP_LOAD_FIELD_REF_STRING;
209 break;
210 case atype_float:
211 op->op = FILTER_OP_LOAD_FIELD_REF_DOUBLE;
212 break;
213 default:
214 return -EINVAL;
215 }
216 /* set offset */
217 field_ref->offset = (uint16_t) field_offset;
218 return 0;
219 }
220
221 static
222 int 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;
232 struct lttng_session *session = runtime->p.session;
233
234 dbg_printf("Apply context reloc: %u %s\n", reloc_offset, context_name);
235
236 /* Get context index */
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 }
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 */
259 ctx_field = &session->ctx->fields[idx];
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;
276 case atype_dynamic:
277 op->op = FILTER_OP_GET_CONTEXT_REF;
278 break;
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
287 static
288 int 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
317 static
318 int bytecode_is_linked(struct lttng_ust_filter_bytecode_node *filter_bytecode,
319 struct lttng_event *event)
320 {
321 struct lttng_bytecode_runtime *bc_runtime;
322
323 cds_list_for_each_entry(bc_runtime,
324 &event->bytecode_runtime_head, node) {
325 if (bc_runtime->bc == filter_bytecode)
326 return 1;
327 }
328 return 0;
329 }
330
331 /*
332 * Take a bytecode with reloc table and link it to an event to create a
333 * bytecode runtime.
334 */
335 static
336 int _lttng_filter_event_link_bytecode(struct lttng_event *event,
337 struct lttng_ust_filter_bytecode_node *filter_bytecode,
338 struct cds_list_head *insert_loc)
339 {
340 int ret, offset, next_offset;
341 struct bytecode_runtime *runtime = NULL;
342 size_t runtime_alloc_len;
343
344 if (!filter_bytecode)
345 return 0;
346 /* Bytecode already linked */
347 if (bytecode_is_linked(filter_bytecode, event))
348 return 0;
349
350 dbg_printf("Linking...\n");
351
352 /* We don't need the reloc table in the runtime */
353 runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset;
354 runtime = zmalloc(runtime_alloc_len);
355 if (!runtime) {
356 ret = -ENOMEM;
357 goto alloc_error;
358 }
359 runtime->p.bc = filter_bytecode;
360 runtime->p.session = event->chan->session;
361 runtime->len = filter_bytecode->bc.reloc_offset;
362 /* copy original bytecode */
363 memcpy(runtime->data, filter_bytecode->bc.data, runtime->len);
364 /*
365 * apply relocs. Those are a uint16_t (offset in bytecode)
366 * followed by a string (field name).
367 */
368 for (offset = filter_bytecode->bc.reloc_offset;
369 offset < filter_bytecode->bc.len;
370 offset = next_offset) {
371 uint16_t reloc_offset =
372 *(uint16_t *) &filter_bytecode->bc.data[offset];
373 const char *name =
374 (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)];
375
376 ret = apply_reloc(event, runtime, runtime->len, reloc_offset, name);
377 if (ret) {
378 goto link_error;
379 }
380 next_offset = offset + sizeof(uint16_t) + strlen(name) + 1;
381 }
382 /* Validate bytecode */
383 ret = lttng_filter_validate_bytecode(runtime);
384 if (ret) {
385 goto link_error;
386 }
387 /* Specialize bytecode */
388 ret = lttng_filter_specialize_bytecode(runtime);
389 if (ret) {
390 goto link_error;
391 }
392 runtime->p.filter = lttng_filter_interpret_bytecode;
393 runtime->p.link_failed = 0;
394 cds_list_add_rcu(&runtime->p.node, insert_loc);
395 dbg_printf("Linking successful.\n");
396 return 0;
397
398 link_error:
399 runtime->p.filter = lttng_filter_false;
400 runtime->p.link_failed = 1;
401 cds_list_add_rcu(&runtime->p.node, insert_loc);
402 alloc_error:
403 dbg_printf("Linking failed.\n");
404 return ret;
405 }
406
407 void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime)
408 {
409 struct lttng_ust_filter_bytecode_node *bc = runtime->bc;
410
411 if (!bc->enabler->enabled || runtime->link_failed)
412 runtime->filter = lttng_filter_false;
413 else
414 runtime->filter = lttng_filter_interpret_bytecode;
415 }
416
417 /*
418 * Link bytecode for all enablers referenced by an event.
419 */
420 void lttng_enabler_event_link_bytecode(struct lttng_event *event,
421 struct lttng_enabler *enabler)
422 {
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:
460 dbg_printf("linking bytecode\n");
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 }
466 }
467 }
468
469 /*
470 * We own the filter_bytecode if we return success.
471 */
472 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
473 struct lttng_ust_filter_bytecode_node *filter_bytecode)
474 {
475 cds_list_add(&filter_bytecode->node, &enabler->filter_bytecode_head);
476 return 0;
477 }
478
479 void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler)
480 {
481 struct lttng_ust_filter_bytecode_node *filter_bytecode, *tmp;
482
483 cds_list_for_each_entry_safe(filter_bytecode, tmp,
484 &enabler->filter_bytecode_head, node) {
485 free(filter_bytecode);
486 }
487 }
488
489 void lttng_free_event_filter_runtime(struct lttng_event *event)
490 {
491 struct bytecode_runtime *runtime, *tmp;
492
493 cds_list_for_each_entry_safe(runtime, tmp,
494 &event->bytecode_runtime_head, p.node) {
495 free(runtime);
496 }
497 }
This page took 0.037747 seconds and 3 git commands to generate.