Commit | Line | Data |
---|---|---|
97b58163 MD |
1 | #ifndef _LTTNG_FILTER_H |
2 | #define _LTTNG_FILTER_H | |
3 | ||
4 | /* | |
5 | * lttng-filter.h | |
6 | * | |
7 | * LTTng UST filter header. | |
8 | * | |
9 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; only | |
14 | * version 2.1 of the License. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | ||
26 | #include <errno.h> | |
27 | #include <stdio.h> | |
28 | #include <helper.h> | |
29 | #include <lttng/ust-events.h> | |
30 | #include <stdint.h> | |
0305960f | 31 | #include <assert.h> |
97b58163 MD |
32 | #include <errno.h> |
33 | #include <string.h> | |
34 | #include <inttypes.h> | |
35 | #include <limits.h> | |
36 | #include <usterr-signal-safe.h> | |
37 | #include "filter-bytecode.h" | |
38 | ||
0305960f | 39 | /* Filter stack length, in number of entries */ |
9b33aac4 MD |
40 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
41 | #define FILTER_STACK_EMPTY 1 | |
97b58163 MD |
42 | |
43 | #ifndef min_t | |
44 | #define min_t(type, a, b) \ | |
45 | ((type) (a) < (type) (b) ? (type) (a) : (type) (b)) | |
46 | #endif | |
47 | ||
48 | #ifndef likely | |
49 | #define likely(x) __builtin_expect(!!(x), 1) | |
50 | #endif | |
51 | ||
52 | #ifndef unlikely | |
53 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
54 | #endif | |
55 | ||
56 | #ifdef DEBUG | |
f488575f MD |
57 | #define dbg_printf(fmt, args...) \ |
58 | printf("[debug bytecode in %s:%s@%u] " fmt, \ | |
59 | __FILE__, __func__, __LINE__, ## args) | |
97b58163 MD |
60 | #else |
61 | #define dbg_printf(fmt, args...) \ | |
62 | do { \ | |
63 | /* do nothing but check printf format */ \ | |
64 | if (0) \ | |
f488575f MD |
65 | printf("[debug bytecode in %s:%s@%u] " fmt, \ |
66 | __FILE__, __func__, __LINE__, ## args); \ | |
97b58163 MD |
67 | } while (0) |
68 | #endif | |
69 | ||
f488575f | 70 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
97b58163 | 71 | struct bytecode_runtime { |
f488575f | 72 | struct lttng_bytecode_runtime p; |
97b58163 MD |
73 | uint16_t len; |
74 | char data[0]; | |
75 | }; | |
76 | ||
0305960f | 77 | enum entry_type { |
97b58163 MD |
78 | REG_S64, |
79 | REG_DOUBLE, | |
80 | REG_STRING, | |
81 | REG_TYPE_UNKNOWN, | |
82 | }; | |
83 | ||
0305960f MD |
84 | /* Validation stack */ |
85 | struct vstack_entry { | |
86 | enum entry_type type; | |
97b58163 MD |
87 | }; |
88 | ||
0305960f MD |
89 | struct vstack { |
90 | int top; /* top of stack */ | |
91 | struct vstack_entry e[FILTER_STACK_LEN]; | |
92 | }; | |
93 | ||
94 | static inline | |
95 | void vstack_init(struct vstack *stack) | |
96 | { | |
97 | stack->top = -1; | |
98 | } | |
99 | ||
100 | static inline | |
101 | struct vstack_entry *vstack_ax(struct vstack *stack) | |
102 | { | |
103 | if (unlikely(stack->top < 0)) | |
104 | return NULL; | |
105 | return &stack->e[stack->top]; | |
106 | } | |
107 | ||
108 | static inline | |
109 | struct vstack_entry *vstack_bx(struct vstack *stack) | |
110 | { | |
111 | if (unlikely(stack->top < 1)) | |
112 | return NULL; | |
113 | return &stack->e[stack->top - 1]; | |
114 | } | |
115 | ||
116 | static inline | |
117 | int vstack_push(struct vstack *stack) | |
118 | { | |
119 | if (stack->top >= FILTER_STACK_LEN - 1) { | |
120 | ERR("Stack full\n"); | |
121 | return -EINVAL; | |
122 | } | |
123 | ++stack->top; | |
124 | return 0; | |
125 | } | |
126 | ||
127 | static inline | |
128 | int vstack_pop(struct vstack *stack) | |
129 | { | |
130 | if (unlikely(stack->top < 0)) { | |
131 | ERR("Stack empty\n"); | |
132 | return -EINVAL; | |
133 | } | |
134 | stack->top--; | |
135 | return 0; | |
136 | } | |
137 | ||
138 | /* Execution stack */ | |
139 | struct estack_entry { | |
0305960f MD |
140 | union { |
141 | int64_t v; | |
142 | double d; | |
143 | ||
144 | struct { | |
145 | const char *str; | |
9b33aac4 MD |
146 | size_t seq_len; |
147 | int literal; /* is string literal ? */ | |
0305960f MD |
148 | } s; |
149 | } u; | |
150 | }; | |
97b58163 | 151 | |
0305960f MD |
152 | struct estack { |
153 | int top; /* top of stack */ | |
154 | struct estack_entry e[FILTER_STACK_LEN]; | |
97b58163 MD |
155 | }; |
156 | ||
9b33aac4 MD |
157 | #define estack_ax_v ax |
158 | #define estack_bx_v bx | |
159 | ||
160 | #define estack_ax(stack, top) \ | |
161 | ({ \ | |
162 | assert((top) > FILTER_STACK_EMPTY); \ | |
163 | &(stack)->e[top]; \ | |
164 | }) | |
165 | ||
166 | #define estack_bx(stack, top) \ | |
167 | ({ \ | |
168 | assert((top) > FILTER_STACK_EMPTY + 1); \ | |
169 | &(stack)->e[(top) - 1]; \ | |
170 | }) | |
171 | ||
172 | #define estack_push(stack, top, ax, bx) \ | |
173 | do { \ | |
174 | assert((top) < FILTER_STACK_LEN - 1); \ | |
175 | (stack)->e[(top) - 1].u.v = (bx); \ | |
176 | (bx) = (ax); \ | |
177 | ++(top); \ | |
178 | } while (0) | |
179 | ||
180 | #define estack_pop(stack, top, ax, bx) \ | |
181 | do { \ | |
182 | assert((top) > FILTER_STACK_EMPTY); \ | |
183 | (ax) = (bx); \ | |
184 | (bx) = (stack)->e[(top) - 2].u.v; \ | |
185 | (top)--; \ | |
186 | } while (0) | |
0305960f | 187 | |
97b58163 MD |
188 | const char *print_op(enum filter_op op); |
189 | ||
190 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); | |
191 | int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode); | |
192 | ||
8a92ed2a | 193 | uint64_t lttng_filter_false(void *filter_data, |
97b58163 | 194 | const char *filter_stack_data); |
8a92ed2a | 195 | uint64_t lttng_filter_interpret_bytecode(void *filter_data, |
97b58163 MD |
196 | const char *filter_stack_data); |
197 | ||
198 | #endif /* _LTTNG_FILTER_H */ |