Filter code relicensing to MIT license
[lttng-modules.git] / lttng-filter.h
1 #ifndef _LTTNG_FILTER_H
2 #define _LTTNG_FILTER_H
3
4 /*
5 * lttng-filter.h
6 *
7 * LTTng modules filter header.
8 *
9 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <linux/kernel.h>
31
32 #include <lttng-events.h>
33 #include <filter-bytecode.h>
34
35 /* Filter stack length, in number of entries */
36 #define FILTER_STACK_LEN 10 /* includes 2 dummy */
37 #define FILTER_STACK_EMPTY 1
38
39 #ifdef DEBUG
40 #define dbg_printk(fmt, args...) \
41 printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \
42 __FILE__, __func__, __LINE__, ## args)
43 #else
44 #define dbg_printk(fmt, args...) \
45 do { \
46 /* do nothing but check printf format */ \
47 if (0) \
48 printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \
49 __FILE__, __func__, __LINE__, ## args); \
50 } while (0)
51 #endif
52
53 /* Linked bytecode. Child of struct lttng_bytecode_runtime. */
54 struct bytecode_runtime {
55 struct lttng_bytecode_runtime p;
56 uint16_t len;
57 char data[0];
58 };
59
60 enum entry_type {
61 REG_S64,
62 REG_DOUBLE,
63 REG_STRING,
64 REG_TYPE_UNKNOWN,
65 };
66
67 /* Validation stack */
68 struct vstack_entry {
69 enum entry_type type;
70 };
71
72 struct vstack {
73 int top; /* top of stack */
74 struct vstack_entry e[FILTER_STACK_LEN];
75 };
76
77 static inline
78 void vstack_init(struct vstack *stack)
79 {
80 stack->top = -1;
81 }
82
83 static inline
84 struct vstack_entry *vstack_ax(struct vstack *stack)
85 {
86 if (unlikely(stack->top < 0))
87 return NULL;
88 return &stack->e[stack->top];
89 }
90
91 static inline
92 struct vstack_entry *vstack_bx(struct vstack *stack)
93 {
94 if (unlikely(stack->top < 1))
95 return NULL;
96 return &stack->e[stack->top - 1];
97 }
98
99 static inline
100 int vstack_push(struct vstack *stack)
101 {
102 if (stack->top >= FILTER_STACK_LEN - 1) {
103 printk(KERN_WARNING "Stack full\n");
104 return -EINVAL;
105 }
106 ++stack->top;
107 return 0;
108 }
109
110 static inline
111 int vstack_pop(struct vstack *stack)
112 {
113 if (unlikely(stack->top < 0)) {
114 printk(KERN_WARNING "Stack empty\n");
115 return -EINVAL;
116 }
117 stack->top--;
118 return 0;
119 }
120
121 /* Execution stack */
122 struct estack_entry {
123 union {
124 int64_t v;
125
126 struct {
127 const char *str;
128 const char __user *user_str;
129 size_t seq_len;
130 int literal; /* is string literal ? */
131 int user; /* is string from userspace ? */
132 } s;
133 } u;
134 };
135
136 struct estack {
137 int top; /* top of stack */
138 struct estack_entry e[FILTER_STACK_LEN];
139 };
140
141 #define estack_ax_v ax
142 #define estack_bx_v bx
143
144 #define estack_ax(stack, top) \
145 ({ \
146 BUG_ON((top) <= FILTER_STACK_EMPTY); \
147 &(stack)->e[top]; \
148 })
149
150 #define estack_bx(stack, top) \
151 ({ \
152 BUG_ON((top) <= FILTER_STACK_EMPTY + 1); \
153 &(stack)->e[(top) - 1]; \
154 })
155
156 #define estack_push(stack, top, ax, bx) \
157 do { \
158 BUG_ON((top) >= FILTER_STACK_LEN - 1); \
159 (stack)->e[(top) - 1].u.v = (bx); \
160 (bx) = (ax); \
161 ++(top); \
162 } while (0)
163
164 #define estack_pop(stack, top, ax, bx) \
165 do { \
166 BUG_ON((top) <= FILTER_STACK_EMPTY); \
167 (ax) = (bx); \
168 (bx) = (stack)->e[(top) - 2].u.v; \
169 (top)--; \
170 } while (0)
171
172 const char *lttng_filter_print_op(enum filter_op op);
173
174 int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
175 int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode);
176
177 uint64_t lttng_filter_false(void *filter_data,
178 struct lttng_probe_ctx *lttng_probe_ctx,
179 const char *filter_stack_data);
180 uint64_t lttng_filter_interpret_bytecode(void *filter_data,
181 struct lttng_probe_ctx *lttng_probe_ctx,
182 const char *filter_stack_data);
183
184 #endif /* _LTTNG_FILTER_H */
This page took 0.033794 seconds and 5 git commands to generate.