docs: Add supported versions and fix-backport policy
[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 <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <inttypes.h>
17
18 #include <common/compat/errno.hpp>
19 #include <common/bytecode/bytecode.hpp>
20 #include <common/filter/filter-ast.hpp>
21 #include <common/filter/filter-parser.hpp>
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,
33 print_bytecode = 0;
34 int argidx;
35
36 for (argidx = 1; argidx < argc; argidx++) {
37 if (strcmp(argv[argidx], "-p") == 0)
38 print_xml = 1;
39 else if (strcmp(argv[argidx], "-i") == 0)
40 generate_ir = 1;
41 else if (strcmp(argv[argidx], "-b") == 0)
42 generate_bytecode = 1;
43 else if (strcmp(argv[argidx], "-d") == 0)
44 filter_parser_debug = 1;
45 else if (strcmp(argv[argidx], "-B") == 0)
46 print_bytecode = 1;
47 }
48
49 /*
50 * Force generate the bytecode if the user asks to print the bytecode
51 * (can't print it without generating it first).
52 */
53 if (print_bytecode) {
54 generate_bytecode = 1;
55 }
56
57 /*
58 * Force generate the IR if the user asks to generate the bytecode
59 * (the bytecode is generated by visiting the IR).
60 */
61 if (generate_bytecode) {
62 generate_ir = 1;
63 }
64
65 ctx = filter_parser_ctx_alloc(stdin);
66 if (!ctx) {
67 fprintf(stderr, "Error allocating parser\n");
68 goto alloc_error;
69 }
70 ret = filter_parser_ctx_append_ast(ctx);
71 if (ret) {
72 fprintf(stderr, "Parse error\n");
73 goto parse_error;
74 }
75 if (print_xml) {
76 ret = filter_visitor_print_xml(ctx, stdout, 0);
77 if (ret) {
78 fflush(stdout);
79 fprintf(stderr, "XML print error\n");
80 goto parse_error;
81 }
82 }
83 if (generate_ir) {
84 printf("Generating IR... ");
85 fflush(stdout);
86 ret = filter_visitor_ir_generate(ctx);
87 if (ret) {
88 fprintf(stderr, "Generate IR error\n");
89 goto parse_error;
90 }
91 printf("done\n");
92
93 printf("Validating IR... ");
94 fflush(stdout);
95 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
96 if (ret) {
97 goto parse_error;
98 }
99 printf("done\n");
100 }
101 if (generate_bytecode) {
102 printf("Generating bytecode... ");
103 fflush(stdout);
104 ret = filter_visitor_bytecode_generate(ctx);
105 if (ret) {
106 fprintf(stderr, "Generate bytecode error\n");
107 goto parse_error;
108 }
109 printf("done\n");
110 printf("Size of bytecode generated: %u bytes.\n",
111 bytecode_get_len(&ctx->bytecode->b));
112 }
113
114 if (print_bytecode) {
115 unsigned int bytecode_len, len, i;
116
117 len = bytecode_get_len(&ctx->bytecode->b);
118 bytecode_len = ctx->bytecode->b.reloc_table_offset;
119 printf("Bytecode:\n");
120 for (i = 0; i < bytecode_len; i++) {
121 printf("0x%X ",
122 ((uint8_t *) ctx->bytecode->b.data)[i]);
123 }
124 printf("\n");
125 printf("Reloc table:\n");
126 for (i = bytecode_len; i < len;) {
127 printf("{ 0x%X, ",
128 *(uint16_t *) &ctx->bytecode->b.data[i]);
129 i += sizeof(uint16_t);
130 printf("%s } ", &((char *) ctx->bytecode->b.data)[i]);
131 i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1;
132 }
133 printf("\n");
134 }
135
136 filter_bytecode_free(ctx);
137 filter_ir_free(ctx);
138 filter_parser_ctx_free(ctx);
139 return 0;
140
141 parse_error:
142 filter_bytecode_free(ctx);
143 filter_ir_free(ctx);
144 filter_parser_ctx_free(ctx);
145 alloc_error:
146 exit(EXIT_FAILURE);
147 }
This page took 0.032593 seconds and 4 git commands to generate.