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