Commit | Line | Data |
---|---|---|
953192ba MD |
1 | /* |
2 | * filter-visitor-generate-bytecode.c | |
3 | * | |
4 | * LTTng filter bytecode generation | |
5 | * | |
6 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
10 | * as published by the Free Software Foundation. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this library; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
46820c8b | 25 | #include <common/align.h> |
afc5df03 | 26 | #include <common/compat/string.h> |
46820c8b | 27 | |
953192ba MD |
28 | #include "filter-bytecode.h" |
29 | #include "filter-ir.h" | |
30 | #include "filter-ast.h" | |
31 | ||
a187da1a DG |
32 | #include <common/macros.h> |
33 | ||
953192ba MD |
34 | #ifndef max_t |
35 | #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b))) | |
36 | #endif | |
37 | ||
953192ba MD |
38 | #define INIT_ALLOC_SIZE 4 |
39 | ||
40 | static | |
41 | int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx, | |
42 | struct ir_op *node); | |
43 | ||
01a204f0 CB |
44 | static inline int get_count_order(unsigned int count) |
45 | { | |
46 | int order; | |
47 | ||
afc5df03 | 48 | order = lttng_fls(count) - 1; |
01a204f0 CB |
49 | if (count & (count - 1)) |
50 | order++; | |
51 | return order; | |
52 | } | |
53 | ||
953192ba | 54 | static |
53a80697 | 55 | int bytecode_init(struct lttng_filter_bytecode_alloc **fb) |
953192ba | 56 | { |
1029587a MD |
57 | uint32_t alloc_len; |
58 | ||
59 | alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + INIT_ALLOC_SIZE; | |
60 | *fb = calloc(alloc_len, 1); | |
953192ba MD |
61 | if (!*fb) { |
62 | return -ENOMEM; | |
63 | } else { | |
1029587a | 64 | (*fb)->alloc_len = alloc_len; |
953192ba MD |
65 | return 0; |
66 | } | |
67 | } | |
68 | ||
69 | static | |
53a80697 | 70 | int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb, uint32_t align, uint32_t len) |
953192ba MD |
71 | { |
72 | int32_t ret; | |
73 | uint32_t padding = offset_align((*fb)->b.len, align); | |
ec96a8f6 | 74 | uint32_t new_len = (*fb)->b.len + padding + len; |
1029587a | 75 | uint32_t new_alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + new_len; |
ec96a8f6 | 76 | uint32_t old_alloc_len = (*fb)->alloc_len; |
953192ba | 77 | |
ec96a8f6 | 78 | if (new_len > LTTNG_FILTER_MAX_LEN) |
5ddb0a08 CB |
79 | return -EINVAL; |
80 | ||
ec96a8f6 | 81 | if (new_alloc_len > old_alloc_len) { |
d0b96690 DG |
82 | struct lttng_filter_bytecode_alloc *newptr; |
83 | ||
ec96a8f6 MD |
84 | new_alloc_len = |
85 | max_t(uint32_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1); | |
d0b96690 DG |
86 | newptr = realloc(*fb, new_alloc_len); |
87 | if (!newptr) | |
953192ba | 88 | return -ENOMEM; |
d0b96690 | 89 | *fb = newptr; |
1029587a | 90 | /* We zero directly the memory from start of allocation. */ |
ec96a8f6 MD |
91 | memset(&((char *) *fb)[old_alloc_len], 0, new_alloc_len - old_alloc_len); |
92 | (*fb)->alloc_len = new_alloc_len; | |
953192ba MD |
93 | } |
94 | (*fb)->b.len += padding; | |
95 | ret = (*fb)->b.len; | |
96 | (*fb)->b.len += len; | |
97 | return ret; | |
98 | } | |
99 | ||
100 | static | |
53a80697 | 101 | int bytecode_push(struct lttng_filter_bytecode_alloc **fb, const void *data, |
953192ba MD |
102 | uint32_t align, uint32_t len) |
103 | { | |
104 | int32_t offset; | |
105 | ||
106 | offset = bytecode_reserve(fb, align, len); | |
107 | if (offset < 0) | |
108 | return offset; | |
109 | memcpy(&(*fb)->b.data[offset], data, len); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | static | |
53a80697 | 114 | int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb, |
953192ba MD |
115 | struct logical_op *data, |
116 | uint32_t align, uint32_t len, | |
117 | uint16_t *skip_offset) | |
118 | { | |
119 | int32_t offset; | |
120 | ||
121 | offset = bytecode_reserve(fb, align, len); | |
122 | if (offset < 0) | |
123 | return offset; | |
124 | memcpy(&(*fb)->b.data[offset], data, len); | |
125 | *skip_offset = | |
126 | (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset | |
127 | - (void *) &(*fb)->b.data[0]; | |
128 | return 0; | |
129 | } | |
130 | ||
131 | static | |
53a80697 | 132 | int bytecode_patch(struct lttng_filter_bytecode_alloc **fb, |
953192ba MD |
133 | const void *data, |
134 | uint16_t offset, | |
135 | uint32_t len) | |
136 | { | |
137 | if (offset >= (*fb)->b.len) { | |
138 | return -EINVAL; | |
139 | } | |
140 | memcpy(&(*fb)->b.data[offset], data, len); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | static | |
145 | int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node) | |
146 | { | |
147 | int ret; | |
148 | struct return_op insn; | |
149 | ||
150 | /* Visit child */ | |
151 | ret = recursive_visit_gen_bytecode(ctx, node->u.root.child); | |
152 | if (ret) | |
153 | return ret; | |
154 | ||
155 | /* Generate end of bytecode instruction */ | |
156 | insn.op = FILTER_OP_RETURN; | |
157 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); | |
158 | } | |
159 | ||
016dbbb4 MD |
160 | static |
161 | int append_str(char **s, const char *append) | |
162 | { | |
163 | char *old = *s; | |
164 | char *new; | |
165 | size_t oldlen = (old == NULL) ? 0 : strlen(old); | |
166 | size_t appendlen = strlen(append); | |
167 | ||
168 | new = calloc(oldlen + appendlen + 1, 1); | |
169 | if (!new) { | |
170 | return -ENOMEM; | |
171 | } | |
172 | if (oldlen) { | |
173 | strcpy(new, old); | |
174 | } | |
175 | strcat(new, append); | |
176 | *s = new; | |
177 | free(old); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | /* | |
182 | * 1: match | |
183 | * 0: no match | |
184 | * < 0: error | |
185 | */ | |
186 | static | |
187 | int load_expression_legacy_match(const struct ir_load_expression *exp, | |
188 | enum filter_op *op_type, | |
189 | char **symbol) | |
190 | { | |
191 | const struct ir_load_expression_op *op; | |
192 | bool need_dot = false; | |
193 | ||
194 | op = exp->child; | |
195 | switch (op->type) { | |
196 | case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: | |
197 | *op_type = FILTER_OP_GET_CONTEXT_REF; | |
198 | if (append_str(symbol, "$ctx.")) { | |
199 | return -ENOMEM; | |
200 | } | |
201 | need_dot = false; | |
202 | break; | |
203 | case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: | |
204 | *op_type = FILTER_OP_GET_CONTEXT_REF; | |
205 | if (append_str(symbol, "$app.")) { | |
206 | return -ENOMEM; | |
207 | } | |
208 | need_dot = false; | |
209 | break; | |
210 | case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT: | |
211 | *op_type = FILTER_OP_LOAD_FIELD_REF; | |
212 | need_dot = false; | |
213 | break; | |
214 | ||
215 | case IR_LOAD_EXPRESSION_GET_SYMBOL: | |
216 | case IR_LOAD_EXPRESSION_GET_INDEX: | |
217 | case IR_LOAD_EXPRESSION_LOAD_FIELD: | |
218 | default: | |
219 | return 0; /* no match */ | |
220 | } | |
221 | ||
222 | for (;;) { | |
223 | op = op->next; | |
224 | if (!op) { | |
225 | return 0; /* no match */ | |
226 | } | |
227 | switch (op->type) { | |
228 | case IR_LOAD_EXPRESSION_LOAD_FIELD: | |
229 | goto end; | |
230 | case IR_LOAD_EXPRESSION_GET_SYMBOL: | |
231 | if (need_dot && append_str(symbol, ".")) { | |
232 | return -ENOMEM; | |
233 | } | |
234 | if (append_str(symbol, op->u.symbol)) { | |
235 | return -ENOMEM; | |
236 | } | |
237 | break; | |
238 | default: | |
239 | return 0; /* no match */ | |
240 | } | |
241 | need_dot = true; | |
242 | } | |
243 | end: | |
244 | return 1; /* Legacy match */ | |
245 | } | |
246 | ||
247 | /* | |
248 | * 1: legacy match | |
249 | * 0: no legacy match | |
250 | * < 0: error | |
251 | */ | |
252 | static | |
253 | int visit_node_load_expression_legacy(struct filter_parser_ctx *ctx, | |
254 | const struct ir_load_expression *exp, | |
255 | const struct ir_load_expression_op *op) | |
256 | { | |
257 | struct load_op *insn = NULL; | |
258 | uint32_t insn_len = sizeof(struct load_op) | |
259 | + sizeof(struct field_ref); | |
260 | struct field_ref ref_offset; | |
261 | uint32_t reloc_offset_u32; | |
262 | uint16_t reloc_offset; | |
263 | enum filter_op op_type; | |
264 | char *symbol = NULL; | |
265 | int ret; | |
266 | ||
267 | ret = load_expression_legacy_match(exp, &op_type, &symbol); | |
268 | if (ret <= 0) { | |
269 | goto end; | |
270 | } | |
271 | insn = calloc(insn_len, 1); | |
272 | if (!insn) { | |
273 | ret = -ENOMEM; | |
274 | goto end; | |
275 | } | |
276 | insn->op = op_type; | |
277 | ref_offset.offset = (uint16_t) -1U; | |
278 | memcpy(insn->data, &ref_offset, sizeof(ref_offset)); | |
279 | /* reloc_offset points to struct load_op */ | |
280 | reloc_offset_u32 = bytecode_get_len(&ctx->bytecode->b); | |
281 | if (reloc_offset_u32 > LTTNG_FILTER_MAX_LEN - 1) { | |
282 | ret = -EINVAL; | |
283 | goto end; | |
284 | } | |
285 | reloc_offset = (uint16_t) reloc_offset_u32; | |
286 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
287 | if (ret) { | |
288 | goto end; | |
289 | } | |
290 | /* append reloc */ | |
291 | ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset, | |
292 | 1, sizeof(reloc_offset)); | |
293 | if (ret) { | |
294 | goto end; | |
295 | } | |
296 | ret = bytecode_push(&ctx->bytecode_reloc, symbol, | |
297 | 1, strlen(symbol) + 1); | |
73e38068 JG |
298 | if (ret) { |
299 | goto end; | |
300 | } | |
016dbbb4 MD |
301 | ret = 1; /* legacy */ |
302 | end: | |
303 | free(insn); | |
304 | free(symbol); | |
305 | return ret; | |
306 | } | |
307 | ||
bff988fa MD |
308 | static |
309 | int visit_node_load_expression(struct filter_parser_ctx *ctx, | |
310 | const struct ir_op *node) | |
311 | { | |
312 | struct ir_load_expression *exp; | |
313 | struct ir_load_expression_op *op; | |
016dbbb4 | 314 | int ret; |
bff988fa MD |
315 | |
316 | exp = node->u.load.u.expression; | |
317 | if (!exp) { | |
318 | return -EINVAL; | |
319 | } | |
320 | op = exp->child; | |
321 | if (!op) { | |
322 | return -EINVAL; | |
323 | } | |
016dbbb4 | 324 | |
b4bc01f7 MD |
325 | /* |
326 | * TODO: if we remove legacy load for application contexts, we | |
327 | * need to update session bytecode parser as well. | |
328 | */ | |
016dbbb4 MD |
329 | ret = visit_node_load_expression_legacy(ctx, exp, op); |
330 | if (ret < 0) { | |
331 | return ret; | |
332 | } | |
333 | if (ret > 0) { | |
334 | return 0; /* legacy */ | |
335 | } | |
336 | ||
bff988fa MD |
337 | for (; op != NULL; op = op->next) { |
338 | switch (op->type) { | |
339 | case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: | |
340 | { | |
341 | struct load_op *insn; | |
342 | uint32_t insn_len = sizeof(struct load_op); | |
343 | int ret; | |
344 | ||
345 | insn = calloc(insn_len, 1); | |
346 | if (!insn) | |
347 | return -ENOMEM; | |
348 | insn->op = FILTER_OP_GET_CONTEXT_ROOT; | |
349 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
350 | free(insn); | |
351 | if (ret) { | |
352 | return ret; | |
353 | } | |
354 | break; | |
355 | } | |
356 | case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: | |
357 | { | |
358 | struct load_op *insn; | |
359 | uint32_t insn_len = sizeof(struct load_op); | |
360 | int ret; | |
361 | ||
362 | insn = calloc(insn_len, 1); | |
363 | if (!insn) | |
364 | return -ENOMEM; | |
365 | insn->op = FILTER_OP_GET_APP_CONTEXT_ROOT; | |
366 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
367 | free(insn); | |
368 | if (ret) { | |
369 | return ret; | |
370 | } | |
371 | break; | |
372 | } | |
373 | case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT: | |
374 | { | |
375 | struct load_op *insn; | |
376 | uint32_t insn_len = sizeof(struct load_op); | |
377 | int ret; | |
378 | ||
379 | insn = calloc(insn_len, 1); | |
380 | if (!insn) | |
381 | return -ENOMEM; | |
382 | insn->op = FILTER_OP_GET_PAYLOAD_ROOT; | |
383 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
384 | free(insn); | |
385 | if (ret) { | |
386 | return ret; | |
387 | } | |
388 | break; | |
389 | } | |
390 | case IR_LOAD_EXPRESSION_GET_SYMBOL: | |
391 | { | |
392 | struct load_op *insn; | |
393 | uint32_t insn_len = sizeof(struct load_op) | |
394 | + sizeof(struct get_symbol); | |
395 | struct get_symbol symbol_offset; | |
396 | uint32_t reloc_offset_u32; | |
397 | uint16_t reloc_offset; | |
398 | uint32_t bytecode_reloc_offset_u32; | |
399 | int ret; | |
400 | ||
401 | insn = calloc(insn_len, 1); | |
402 | if (!insn) | |
403 | return -ENOMEM; | |
404 | insn->op = FILTER_OP_GET_SYMBOL; | |
405 | bytecode_reloc_offset_u32 = | |
406 | bytecode_get_len(&ctx->bytecode_reloc->b) | |
407 | + sizeof(reloc_offset); | |
408 | symbol_offset.offset = | |
409 | (uint16_t) bytecode_reloc_offset_u32; | |
410 | memcpy(insn->data, &symbol_offset, | |
411 | sizeof(symbol_offset)); | |
412 | /* reloc_offset points to struct load_op */ | |
413 | reloc_offset_u32 = bytecode_get_len(&ctx->bytecode->b); | |
414 | if (reloc_offset_u32 > LTTNG_FILTER_MAX_LEN - 1) { | |
415 | free(insn); | |
416 | return -EINVAL; | |
417 | } | |
418 | reloc_offset = (uint16_t) reloc_offset_u32; | |
419 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
420 | if (ret) { | |
421 | free(insn); | |
422 | return ret; | |
423 | } | |
424 | /* append reloc */ | |
425 | ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset, | |
426 | 1, sizeof(reloc_offset)); | |
427 | if (ret) { | |
428 | free(insn); | |
429 | return ret; | |
430 | } | |
431 | ret = bytecode_push(&ctx->bytecode_reloc, | |
432 | op->u.symbol, | |
433 | 1, strlen(op->u.symbol) + 1); | |
434 | free(insn); | |
435 | if (ret) { | |
436 | return ret; | |
437 | } | |
438 | break; | |
439 | } | |
440 | case IR_LOAD_EXPRESSION_GET_INDEX: | |
441 | { | |
442 | struct load_op *insn; | |
443 | uint32_t insn_len = sizeof(struct load_op) | |
444 | + sizeof(struct get_index_u64); | |
445 | struct get_index_u64 index; | |
446 | int ret; | |
447 | ||
448 | insn = calloc(insn_len, 1); | |
449 | if (!insn) | |
450 | return -ENOMEM; | |
451 | insn->op = FILTER_OP_GET_INDEX_U64; | |
452 | index.index = op->u.index; | |
453 | memcpy(insn->data, &index, sizeof(index)); | |
454 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
455 | free(insn); | |
456 | if (ret) { | |
457 | return ret; | |
458 | } | |
459 | break; | |
460 | } | |
461 | case IR_LOAD_EXPRESSION_LOAD_FIELD: | |
462 | { | |
463 | struct load_op *insn; | |
464 | uint32_t insn_len = sizeof(struct load_op); | |
465 | int ret; | |
466 | ||
467 | insn = calloc(insn_len, 1); | |
468 | if (!insn) | |
469 | return -ENOMEM; | |
470 | insn->op = FILTER_OP_LOAD_FIELD; | |
471 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); | |
472 | free(insn); | |
473 | if (ret) { | |
474 | return ret; | |
475 | } | |
476 | break; | |
477 | } | |
478 | } | |
479 | } | |
480 | return 0; | |
481 | } | |
482 | ||
953192ba MD |
483 | static |
484 | int visit_node_load(struct filter_parser_ctx *ctx, struct ir_op *node) | |
485 | { | |
486 | int ret; | |
487 | ||
488 | switch (node->data_type) { | |
489 | case IR_DATA_UNKNOWN: | |
490 | default: | |
491 | fprintf(stderr, "[error] Unknown data type in %s\n", | |
492 | __func__); | |
493 | return -EINVAL; | |
494 | ||
495 | case IR_DATA_STRING: | |
496 | { | |
497 | struct load_op *insn; | |
498 | uint32_t insn_len = sizeof(struct load_op) | |
9f449915 | 499 | + strlen(node->u.load.u.string.value) + 1; |
953192ba MD |
500 | |
501 | insn = calloc(insn_len, 1); | |
502 | if (!insn) | |
503 | return -ENOMEM; | |
9f449915 PP |
504 | |
505 | switch (node->u.load.u.string.type) { | |
506 | case IR_LOAD_STRING_TYPE_GLOB_STAR: | |
507 | /* | |
508 | * We explicitly tell the interpreter here that | |
509 | * this load is a full star globbing pattern so | |
510 | * that the appropriate matching function can be | |
511 | * called. Also, see comment below. | |
512 | */ | |
513 | insn->op = FILTER_OP_LOAD_STAR_GLOB_STRING; | |
514 | break; | |
515 | default: | |
516 | /* | |
517 | * This is the "legacy" string, which includes | |
518 | * star globbing patterns with a star only at | |
519 | * the end. Both "plain" and "star at the end" | |
520 | * literal strings are handled at the same place | |
521 | * by the tracer's filter bytecode interpreter, | |
522 | * whereas full star globbing patterns (stars | |
523 | * can be anywhere in the string) is a special | |
524 | * case. | |
525 | */ | |
526 | insn->op = FILTER_OP_LOAD_STRING; | |
527 | break; | |
528 | } | |
529 | ||
530 | strcpy(insn->data, node->u.load.u.string.value); | |
953192ba MD |
531 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
532 | free(insn); | |
533 | return ret; | |
534 | } | |
535 | case IR_DATA_NUMERIC: | |
536 | { | |
537 | struct load_op *insn; | |
538 | uint32_t insn_len = sizeof(struct load_op) | |
539 | + sizeof(struct literal_numeric); | |
540 | ||
541 | insn = calloc(insn_len, 1); | |
542 | if (!insn) | |
543 | return -ENOMEM; | |
544 | insn->op = FILTER_OP_LOAD_S64; | |
58d494e4 | 545 | memcpy(insn->data, &node->u.load.u.num, sizeof(int64_t)); |
953192ba MD |
546 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
547 | free(insn); | |
548 | return ret; | |
549 | } | |
e90d8561 MD |
550 | case IR_DATA_FLOAT: |
551 | { | |
552 | struct load_op *insn; | |
553 | uint32_t insn_len = sizeof(struct load_op) | |
554 | + sizeof(struct literal_double); | |
555 | ||
556 | insn = calloc(insn_len, 1); | |
557 | if (!insn) | |
558 | return -ENOMEM; | |
559 | insn->op = FILTER_OP_LOAD_DOUBLE; | |
58d494e4 | 560 | memcpy(insn->data, &node->u.load.u.flt, sizeof(double)); |
e90d8561 MD |
561 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
562 | free(insn); | |
563 | return ret; | |
564 | } | |
bff988fa MD |
565 | case IR_DATA_EXPRESSION: |
566 | return visit_node_load_expression(ctx, node); | |
953192ba MD |
567 | } |
568 | } | |
569 | ||
570 | static | |
571 | int visit_node_unary(struct filter_parser_ctx *ctx, struct ir_op *node) | |
572 | { | |
573 | int ret; | |
574 | struct unary_op insn; | |
575 | ||
576 | /* Visit child */ | |
577 | ret = recursive_visit_gen_bytecode(ctx, node->u.unary.child); | |
578 | if (ret) | |
579 | return ret; | |
580 | ||
581 | /* Generate end of bytecode instruction */ | |
582 | switch (node->u.unary.type) { | |
583 | case AST_UNARY_UNKNOWN: | |
584 | default: | |
585 | fprintf(stderr, "[error] Unknown unary node type in %s\n", | |
586 | __func__); | |
587 | return -EINVAL; | |
588 | case AST_UNARY_PLUS: | |
589 | /* Nothing to do. */ | |
590 | return 0; | |
591 | case AST_UNARY_MINUS: | |
592 | insn.op = FILTER_OP_UNARY_MINUS; | |
953192ba MD |
593 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
594 | case AST_UNARY_NOT: | |
595 | insn.op = FILTER_OP_UNARY_NOT; | |
953192ba | 596 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
116d3c01 MD |
597 | case AST_UNARY_BIT_NOT: |
598 | insn.op = FILTER_OP_UNARY_BIT_NOT; | |
599 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); | |
953192ba MD |
600 | } |
601 | } | |
602 | ||
603 | /* | |
604 | * Binary comparator nesting is disallowed. This allows fitting into | |
605 | * only 2 registers. | |
606 | */ | |
607 | static | |
608 | int visit_node_binary(struct filter_parser_ctx *ctx, struct ir_op *node) | |
609 | { | |
610 | int ret; | |
611 | struct binary_op insn; | |
612 | ||
613 | /* Visit child */ | |
614 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left); | |
615 | if (ret) | |
616 | return ret; | |
617 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right); | |
618 | if (ret) | |
619 | return ret; | |
620 | ||
621 | switch (node->u.binary.type) { | |
622 | case AST_OP_UNKNOWN: | |
623 | default: | |
624 | fprintf(stderr, "[error] Unknown unary node type in %s\n", | |
625 | __func__); | |
626 | return -EINVAL; | |
627 | ||
628 | case AST_OP_AND: | |
629 | case AST_OP_OR: | |
630 | fprintf(stderr, "[error] Unexpected logical node type in %s\n", | |
631 | __func__); | |
632 | return -EINVAL; | |
633 | ||
634 | case AST_OP_MUL: | |
635 | insn.op = FILTER_OP_MUL; | |
636 | break; | |
637 | case AST_OP_DIV: | |
638 | insn.op = FILTER_OP_DIV; | |
639 | break; | |
640 | case AST_OP_MOD: | |
641 | insn.op = FILTER_OP_MOD; | |
642 | break; | |
643 | case AST_OP_PLUS: | |
644 | insn.op = FILTER_OP_PLUS; | |
645 | break; | |
646 | case AST_OP_MINUS: | |
647 | insn.op = FILTER_OP_MINUS; | |
648 | break; | |
116d3c01 MD |
649 | case AST_OP_BIT_RSHIFT: |
650 | insn.op = FILTER_OP_BIT_RSHIFT; | |
953192ba | 651 | break; |
116d3c01 MD |
652 | case AST_OP_BIT_LSHIFT: |
653 | insn.op = FILTER_OP_BIT_LSHIFT; | |
953192ba | 654 | break; |
bff988fa MD |
655 | case AST_OP_BIT_AND: |
656 | insn.op = FILTER_OP_BIT_AND; | |
953192ba | 657 | break; |
bff988fa MD |
658 | case AST_OP_BIT_OR: |
659 | insn.op = FILTER_OP_BIT_OR; | |
953192ba | 660 | break; |
bff988fa MD |
661 | case AST_OP_BIT_XOR: |
662 | insn.op = FILTER_OP_BIT_XOR; | |
953192ba MD |
663 | break; |
664 | ||
665 | case AST_OP_EQ: | |
666 | insn.op = FILTER_OP_EQ; | |
667 | break; | |
668 | case AST_OP_NE: | |
669 | insn.op = FILTER_OP_NE; | |
670 | break; | |
671 | case AST_OP_GT: | |
672 | insn.op = FILTER_OP_GT; | |
673 | break; | |
674 | case AST_OP_LT: | |
675 | insn.op = FILTER_OP_LT; | |
676 | break; | |
677 | case AST_OP_GE: | |
678 | insn.op = FILTER_OP_GE; | |
679 | break; | |
680 | case AST_OP_LE: | |
681 | insn.op = FILTER_OP_LE; | |
682 | break; | |
683 | } | |
684 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); | |
685 | } | |
686 | ||
8cf9540a MD |
687 | /* |
688 | * A logical op always return a s64 (1 or 0). | |
689 | */ | |
953192ba MD |
690 | static |
691 | int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node) | |
692 | { | |
693 | int ret; | |
694 | struct logical_op insn; | |
695 | uint16_t skip_offset_loc; | |
696 | uint16_t target_loc; | |
697 | ||
698 | /* Visit left child */ | |
699 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left); | |
700 | if (ret) | |
701 | return ret; | |
8cf9540a | 702 | /* Cast to s64 if float or field ref */ |
586dc72f | 703 | if ((node->u.binary.left->data_type == IR_DATA_FIELD_REF |
661dfdd1 | 704 | || node->u.binary.left->data_type == IR_DATA_GET_CONTEXT_REF |
bff988fa | 705 | || node->u.binary.left->data_type == IR_DATA_EXPRESSION) |
8cf9540a MD |
706 | || node->u.binary.left->data_type == IR_DATA_FLOAT) { |
707 | struct cast_op cast_insn; | |
708 | ||
586dc72f | 709 | if (node->u.binary.left->data_type == IR_DATA_FIELD_REF |
661dfdd1 | 710 | || node->u.binary.left->data_type == IR_DATA_GET_CONTEXT_REF |
bff988fa | 711 | || node->u.binary.left->data_type == IR_DATA_EXPRESSION) { |
29fefef8 MD |
712 | cast_insn.op = FILTER_OP_CAST_TO_S64; |
713 | } else { | |
714 | cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64; | |
715 | } | |
8cf9540a MD |
716 | ret = bytecode_push(&ctx->bytecode, &cast_insn, |
717 | 1, sizeof(cast_insn)); | |
718 | if (ret) | |
719 | return ret; | |
720 | } | |
953192ba MD |
721 | switch (node->u.logical.type) { |
722 | default: | |
723 | fprintf(stderr, "[error] Unknown node type in %s\n", | |
724 | __func__); | |
725 | return -EINVAL; | |
726 | ||
727 | case AST_OP_AND: | |
728 | insn.op = FILTER_OP_AND; | |
729 | break; | |
730 | case AST_OP_OR: | |
731 | insn.op = FILTER_OP_OR; | |
732 | break; | |
733 | } | |
734 | insn.skip_offset = (uint16_t) -1UL; /* Temporary */ | |
735 | ret = bytecode_push_logical(&ctx->bytecode, &insn, 1, sizeof(insn), | |
736 | &skip_offset_loc); | |
737 | if (ret) | |
738 | return ret; | |
739 | /* Visit right child */ | |
740 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right); | |
741 | if (ret) | |
742 | return ret; | |
8cf9540a | 743 | /* Cast to s64 if float or field ref */ |
586dc72f | 744 | if ((node->u.binary.right->data_type == IR_DATA_FIELD_REF |
661dfdd1 | 745 | || node->u.binary.right->data_type == IR_DATA_GET_CONTEXT_REF |
bff988fa | 746 | || node->u.binary.right->data_type == IR_DATA_EXPRESSION) |
8cf9540a MD |
747 | || node->u.binary.right->data_type == IR_DATA_FLOAT) { |
748 | struct cast_op cast_insn; | |
749 | ||
586dc72f | 750 | if (node->u.binary.right->data_type == IR_DATA_FIELD_REF |
661dfdd1 | 751 | || node->u.binary.right->data_type == IR_DATA_GET_CONTEXT_REF |
bff988fa | 752 | || node->u.binary.right->data_type == IR_DATA_EXPRESSION) { |
29fefef8 MD |
753 | cast_insn.op = FILTER_OP_CAST_TO_S64; |
754 | } else { | |
755 | cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64; | |
756 | } | |
8cf9540a MD |
757 | ret = bytecode_push(&ctx->bytecode, &cast_insn, |
758 | 1, sizeof(cast_insn)); | |
759 | if (ret) | |
760 | return ret; | |
761 | } | |
953192ba MD |
762 | /* We now know where the logical op can skip. */ |
763 | target_loc = (uint16_t) bytecode_get_len(&ctx->bytecode->b); | |
764 | ret = bytecode_patch(&ctx->bytecode, | |
765 | &target_loc, /* Offset to jump to */ | |
766 | skip_offset_loc, /* Where to patch */ | |
767 | sizeof(uint16_t)); | |
768 | return ret; | |
769 | } | |
770 | ||
771 | /* | |
772 | * Postorder traversal of the tree. We need the children result before | |
773 | * we can evaluate the parent. | |
774 | */ | |
775 | static | |
776 | int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx, | |
777 | struct ir_op *node) | |
778 | { | |
779 | switch (node->op) { | |
780 | case IR_OP_UNKNOWN: | |
781 | default: | |
782 | fprintf(stderr, "[error] Unknown node type in %s\n", | |
783 | __func__); | |
784 | return -EINVAL; | |
785 | ||
786 | case IR_OP_ROOT: | |
787 | return visit_node_root(ctx, node); | |
788 | case IR_OP_LOAD: | |
789 | return visit_node_load(ctx, node); | |
790 | case IR_OP_UNARY: | |
791 | return visit_node_unary(ctx, node); | |
792 | case IR_OP_BINARY: | |
793 | return visit_node_binary(ctx, node); | |
794 | case IR_OP_LOGICAL: | |
795 | return visit_node_logical(ctx, node); | |
796 | } | |
797 | } | |
798 | ||
a187da1a | 799 | LTTNG_HIDDEN |
953192ba MD |
800 | void filter_bytecode_free(struct filter_parser_ctx *ctx) |
801 | { | |
7ca1dc6f DG |
802 | if (!ctx) { |
803 | return; | |
804 | } | |
805 | ||
3f0c8837 DG |
806 | if (ctx->bytecode) { |
807 | free(ctx->bytecode); | |
808 | ctx->bytecode = NULL; | |
809 | } | |
810 | ||
811 | if (ctx->bytecode_reloc) { | |
812 | free(ctx->bytecode_reloc); | |
813 | ctx->bytecode_reloc = NULL; | |
814 | } | |
953192ba MD |
815 | } |
816 | ||
a187da1a | 817 | LTTNG_HIDDEN |
953192ba MD |
818 | int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx) |
819 | { | |
820 | int ret; | |
821 | ||
822 | ret = bytecode_init(&ctx->bytecode); | |
823 | if (ret) | |
824 | return ret; | |
825 | ret = bytecode_init(&ctx->bytecode_reloc); | |
826 | if (ret) | |
827 | goto error; | |
828 | ret = recursive_visit_gen_bytecode(ctx, ctx->ir_root); | |
829 | if (ret) | |
830 | goto error; | |
831 | ||
832 | /* Finally, append symbol table to bytecode */ | |
833 | ctx->bytecode->b.reloc_table_offset = bytecode_get_len(&ctx->bytecode->b); | |
834 | return bytecode_push(&ctx->bytecode, ctx->bytecode_reloc->b.data, | |
835 | 1, bytecode_get_len(&ctx->bytecode_reloc->b)); | |
836 | ||
837 | error: | |
838 | filter_bytecode_free(ctx); | |
839 | return ret; | |
840 | } |