Fix: syscall event rule: emission sites not compared in is_equal
[lttng-tools.git] / src / common / filter-grammar-test.cpp
1 /*
2 * filter-grammar-test.c
3 *
4 * LTTng filter grammar test
5 *
6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * SPDX-License-Identifier: LGPL-2.1-only
9 *
10 */
11
12 #include <common/bytecode/bytecode.hpp>
13 #include <common/compat/errno.hpp>
14 #include <common/filter/filter-ast.hpp>
15 #include <common/filter/filter-parser.hpp>
16
17 #include <inttypes.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 /* For error.h */
24 int lttng_opt_quiet = 1;
25 int lttng_opt_verbose;
26 int lttng_opt_mi;
27
28 int main(int argc, char **argv)
29 {
30 struct filter_parser_ctx *ctx;
31 int ret;
32 int print_xml = 0, generate_ir = 0, generate_bytecode = 0, print_bytecode = 0;
33 int argidx;
34
35 for (argidx = 1; argidx < argc; argidx++) {
36 if (strcmp(argv[argidx], "-p") == 0)
37 print_xml = 1;
38 else if (strcmp(argv[argidx], "-i") == 0)
39 generate_ir = 1;
40 else if (strcmp(argv[argidx], "-b") == 0)
41 generate_bytecode = 1;
42 else if (strcmp(argv[argidx], "-d") == 0)
43 filter_parser_debug = 1;
44 else if (strcmp(argv[argidx], "-B") == 0)
45 print_bytecode = 1;
46 }
47
48 /*
49 * Force generate the bytecode if the user asks to print the bytecode
50 * (can't print it without generating it first).
51 */
52 if (print_bytecode) {
53 generate_bytecode = 1;
54 }
55
56 /*
57 * Force generate the IR if the user asks to generate the bytecode
58 * (the bytecode is generated by visiting the IR).
59 */
60 if (generate_bytecode) {
61 generate_ir = 1;
62 }
63
64 ctx = filter_parser_ctx_alloc(stdin);
65 if (!ctx) {
66 fprintf(stderr, "Error allocating parser\n");
67 goto alloc_error;
68 }
69 ret = filter_parser_ctx_append_ast(ctx);
70 if (ret) {
71 fprintf(stderr, "Parse error\n");
72 goto parse_error;
73 }
74 if (print_xml) {
75 ret = filter_visitor_print_xml(ctx, stdout, 0);
76 if (ret) {
77 fflush(stdout);
78 fprintf(stderr, "XML print error\n");
79 goto parse_error;
80 }
81 }
82 if (generate_ir) {
83 printf("Generating IR... ");
84 fflush(stdout);
85 ret = filter_visitor_ir_generate(ctx);
86 if (ret) {
87 fprintf(stderr, "Generate IR error\n");
88 goto parse_error;
89 }
90 printf("done\n");
91
92 printf("Validating IR... ");
93 fflush(stdout);
94 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
95 if (ret) {
96 goto parse_error;
97 }
98 printf("done\n");
99 }
100 if (generate_bytecode) {
101 printf("Generating bytecode... ");
102 fflush(stdout);
103 ret = filter_visitor_bytecode_generate(ctx);
104 if (ret) {
105 fprintf(stderr, "Generate bytecode error\n");
106 goto parse_error;
107 }
108 printf("done\n");
109 printf("Size of bytecode generated: %u bytes.\n",
110 bytecode_get_len(&ctx->bytecode->b));
111 }
112
113 if (print_bytecode) {
114 unsigned int bytecode_len, len, i;
115
116 len = bytecode_get_len(&ctx->bytecode->b);
117 bytecode_len = ctx->bytecode->b.reloc_table_offset;
118 printf("Bytecode:\n");
119 for (i = 0; i < bytecode_len; i++) {
120 printf("0x%X ", ((uint8_t *) ctx->bytecode->b.data)[i]);
121 }
122 printf("\n");
123 printf("Reloc table:\n");
124 for (i = bytecode_len; i < len;) {
125 printf("{ 0x%X, ", *(uint16_t *) &ctx->bytecode->b.data[i]);
126 i += sizeof(uint16_t);
127 printf("%s } ", &((char *) ctx->bytecode->b.data)[i]);
128 i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1;
129 }
130 printf("\n");
131 }
132
133 filter_bytecode_free(ctx);
134 filter_ir_free(ctx);
135 filter_parser_ctx_free(ctx);
136 return 0;
137
138 parse_error:
139 filter_bytecode_free(ctx);
140 filter_ir_free(ctx);
141 filter_parser_ctx_free(ctx);
142 alloc_error:
143 exit(EXIT_FAILURE);
144 }
This page took 0.031379 seconds and 4 git commands to generate.