Implement filter bytecode support in lttng-session, and parse filter string
[lttng-tools.git] / src / lib / lttng-ctl / filter-ast.h
CommitLineData
953192ba
MD
1#ifndef _FILTER_AST_H
2#define _FILTER_AST_H
3
4/*
5 * filter-ast.h
6 *
7 * LTTng filter AST
8 *
9 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License, version 2.1 only,
13 * as published by the Free Software Foundation.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <urcu/list.h>
26#include <stdint.h>
27
28#define printf_debug(fmt, args...) \
29 do { \
30 if (filter_parser_debug) \
31 fprintf(stdout, "[debug] " fmt, ## args); \
32 } while (0)
33
34// the parameter name (of the reentrant 'yyparse' function)
35// data is a pointer to a 'SParserParam' structure
36//#define YYPARSE_PARAM parser_ctx
37
38// the argument for the 'yylex' function
39#define YYLEX_PARAM ((struct filter_parser_ctx *) parser_ctx)->scanner
40
41#ifndef YY_TYPEDEF_YY_SCANNER_T
42#define YY_TYPEDEF_YY_SCANNER_T
43typedef void* yyscan_t;
44#endif
45
46extern int filter_parser_debug;
47
48struct filter_node;
49struct filter_parser;
50
51enum node_type {
52 NODE_UNKNOWN = 0,
53 NODE_ROOT,
54
55 NODE_EXPRESSION,
56 NODE_OP,
57 NODE_UNARY_OP,
58
59 NR_NODE_TYPES,
60};
61
62enum op_type {
63 AST_OP_UNKNOWN = 0,
64 AST_OP_MUL,
65 AST_OP_DIV,
66 AST_OP_MOD,
67 AST_OP_PLUS,
68 AST_OP_MINUS,
69 AST_OP_RSHIFT,
70 AST_OP_LSHIFT,
71 AST_OP_AND,
72 AST_OP_OR,
73 AST_OP_BIN_AND,
74 AST_OP_BIN_OR,
75 AST_OP_BIN_XOR,
76
77 AST_OP_EQ,
78 AST_OP_NE,
79 AST_OP_GT,
80 AST_OP_LT,
81 AST_OP_GE,
82 AST_OP_LE,
83};
84
85enum unary_op_type {
86 AST_UNARY_UNKNOWN = 0,
87 AST_UNARY_PLUS,
88 AST_UNARY_MINUS,
89 AST_UNARY_NOT,
90};
91
92enum ast_link_type {
93 AST_LINK_UNKNOWN = 0,
94 AST_LINK_DOT,
95 AST_LINK_RARROW,
96};
97
98struct filter_node {
99 /*
100 * Parent node is only set on demand by specific visitor.
101 */
102 struct filter_node *parent;
103 struct cds_list_head gc;
104
105 enum node_type type;
106 union {
107 struct {
108 } unknown;
109 struct {
110 struct filter_node *child;
111 } root;
112 struct {
113 enum {
114 AST_EXP_UNKNOWN = 0,
115 AST_EXP_STRING,
116 AST_EXP_CONSTANT,
117 AST_EXP_IDENTIFIER,
118 AST_EXP_NESTED,
119 } type;
120 enum ast_link_type post_op; /* reverse */
121 enum ast_link_type pre_op; /* forward */
122 union {
123 char *string;
124 uint64_t constant;
125 char *identifier;
126 /*
127 * child can be nested.
128 */
129 struct filter_node *child;
130 } u;
131 /* linked dot/arrow chain */
132 struct filter_node *prev;
133 struct filter_node *next;
134 } expression;
135 struct {
136 enum op_type type;
137 struct filter_node *lchild;
138 struct filter_node *rchild;
139 } op;
140 struct {
141 enum unary_op_type type;
142 struct filter_node *child;
143 } unary_op;
144 } u;
145};
146
147struct filter_ast {
148 struct filter_node root;
149 struct cds_list_head allocated_nodes;
150};
151
152const char *node_type(struct filter_node *node);
153
154struct ir_op;
953192ba
MD
155
156struct filter_parser_ctx {
157 yyscan_t scanner;
158 struct filter_ast *ast;
159 struct cds_list_head allocated_strings;
160 struct ir_op *ir_root;
53a80697
MD
161 struct lttng_filter_bytecode_alloc *bytecode;
162 struct lttng_filter_bytecode_alloc *bytecode_reloc;
953192ba
MD
163};
164
165struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input);
166void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx);
167int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx);
168
169static inline
170struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx)
171{
172 return parser_ctx->ast;
173}
174
175int filter_visitor_set_parent(struct filter_parser_ctx *ctx);
176int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream,
177 int indent);
178int filter_visitor_ir_generate(struct filter_parser_ctx *ctx);
179void filter_ir_free(struct filter_parser_ctx *ctx);
180int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx);
181void filter_bytecode_free(struct filter_parser_ctx *ctx);
182int filter_visitor_ir_check_binary_op_nesting(struct filter_parser_ctx *ctx);
183int filter_visitor_ir_check_binary_comparator(struct filter_parser_ctx *ctx);
184
185#endif /* _FILTER_AST_H */
This page took 0.041816 seconds and 4 git commands to generate.