Fix: Possible memory leaks when creating filter IR root node
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-grammar-test.c
CommitLineData
953192ba
MD
1/*
2 * filter-grammar-test.c
3 *
4 * LTTng filter grammar test
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 <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <errno.h>
28#include <inttypes.h>
953192ba 29#include "filter-ast.h"
95b9bd90 30#include "filter-parser.h"
953192ba
MD
31#include "filter-bytecode.h"
32
33int main(int argc, char **argv)
34{
35 struct filter_parser_ctx *ctx;
36 int ret;
14eabff9
MD
37 int print_xml = 0, generate_ir = 0, generate_bytecode = 0,
38 print_bytecode = 0;
953192ba
MD
39 int i;
40
41 for (i = 1; i < argc; i++) {
42 if (strcmp(argv[i], "-p") == 0)
43 print_xml = 1;
44 else if (strcmp(argv[i], "-i") == 0)
45 generate_ir = 1;
46 else if (strcmp(argv[i], "-b") == 0)
47 generate_bytecode = 1;
48 else if (strcmp(argv[i], "-d") == 0)
49 filter_parser_debug = 1;
14eabff9
MD
50 else if (strcmp(argv[i], "-B") == 0)
51 print_bytecode = 1;
953192ba
MD
52 }
53
54 ctx = filter_parser_ctx_alloc(stdin);
55 if (!ctx) {
56 fprintf(stderr, "Error allocating parser\n");
57 goto alloc_error;
58 }
59 ret = filter_parser_ctx_append_ast(ctx);
60 if (ret) {
61 fprintf(stderr, "Parse error\n");
62 goto parse_error;
63 }
64 ret = filter_visitor_set_parent(ctx);
65 if (ret) {
66 fprintf(stderr, "Set parent error\n");
67 goto parse_error;
68 }
69 if (print_xml) {
70 ret = filter_visitor_print_xml(ctx, stdout, 0);
71 if (ret) {
72 fflush(stdout);
73 fprintf(stderr, "XML print error\n");
74 goto parse_error;
75 }
76 }
77 if (generate_ir) {
78 printf("Generating IR... ");
79 fflush(stdout);
80 ret = filter_visitor_ir_generate(ctx);
81 if (ret) {
82 fprintf(stderr, "Generate IR error\n");
83 goto parse_error;
84 }
85 printf("done\n");
86
87 printf("Validating IR... ");
88 fflush(stdout);
89 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
90 if (ret) {
91 goto parse_error;
92 }
93 printf("done\n");
94 }
95 if (generate_bytecode) {
96 printf("Generating bytecode... ");
97 fflush(stdout);
98 ret = filter_visitor_bytecode_generate(ctx);
99 if (ret) {
100 fprintf(stderr, "Generate bytecode error\n");
101 goto parse_error;
102 }
103 printf("done\n");
104 printf("Size of bytecode generated: %u bytes.\n",
105 bytecode_get_len(&ctx->bytecode->b));
106 }
107#if 0
108 if (run_bytecode) {
109 int64_t retval;
110
111 printf("Interpreting bytecode... ");
112 fflush(stdout);
113 ret = bytecode_interpret(&ctx->bytecode->b, &retval, NULL);
114 if (ret) {
115 fprintf(stderr, "Error interpreting bytecode\n");
116 goto parse_error;
117 } else {
118 printf("Bytecode interpret result: %" PRIi64 "\n",
119 retval);
120 }
121 printf("done\n");
122 }
123#endif //0
124
14eabff9
MD
125 if (print_bytecode) {
126 unsigned int bytecode_len, len, i;
127
128 len = bytecode_get_len(&ctx->bytecode->b);
129 bytecode_len = ctx->bytecode->b.reloc_table_offset;
130 printf("Bytecode:\n");
131 for (i = 0; i < bytecode_len; i++) {
132 printf("0x%X ",
133 ((uint8_t *) ctx->bytecode->b.data)[i]);
134 }
135 printf("\n");
136 printf("Reloc table:\n");
137 for (i = bytecode_len; i < len;) {
138 printf("{ 0x%X, ",
139 *(uint16_t *) &ctx->bytecode->b.data[i]);
140 i += sizeof(uint16_t);
141 printf("%s } ", &((char *) ctx->bytecode->b.data)[i]);
142 i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1;
143 }
144 printf("\n");
145 }
146
953192ba
MD
147 filter_bytecode_free(ctx);
148 filter_ir_free(ctx);
149 filter_parser_ctx_free(ctx);
150 return 0;
151
152parse_error:
153 filter_bytecode_free(ctx);
154 filter_ir_free(ctx);
155 filter_parser_ctx_free(ctx);
156alloc_error:
157 exit(EXIT_FAILURE);
158}
This page took 0.02778 seconds and 4 git commands to generate.