Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: MIT |
2 | * | |
80c2a69a | 3 | * lttng/lttng-bytecode.h |
07dfc1d0 | 4 | * |
80c2a69a | 5 | * LTTng modules bytecode header. |
07dfc1d0 | 6 | * |
bbf3aef5 | 7 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
07dfc1d0 MD |
8 | */ |
9 | ||
80c2a69a FD |
10 | #ifndef _LTTNG_BYTECODE_H |
11 | #define _LTTNG_BYTECODE_H | |
9f36eaed | 12 | |
07dfc1d0 MD |
13 | #include <linux/kernel.h> |
14 | ||
2df37e95 | 15 | #include <lttng/events.h> |
80c2a69a | 16 | #include <lttng/bytecode.h> |
07dfc1d0 | 17 | |
80c2a69a FD |
18 | /* Interpreter stack length, in number of entries */ |
19 | #define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */ | |
20 | #define INTERPRETER_STACK_EMPTY 1 | |
21 | #define INTERPRETER_MAX_DATA_LEN 65536 | |
3834b99f | 22 | |
07dfc1d0 MD |
23 | #ifdef DEBUG |
24 | #define dbg_printk(fmt, args...) \ | |
5a15f70c | 25 | printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \ |
07dfc1d0 MD |
26 | __FILE__, __func__, __LINE__, ## args) |
27 | #else | |
28 | #define dbg_printk(fmt, args...) \ | |
29 | do { \ | |
30 | /* do nothing but check printf format */ \ | |
31 | if (0) \ | |
5a15f70c | 32 | printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \ |
07dfc1d0 MD |
33 | __FILE__, __func__, __LINE__, ## args); \ |
34 | } while (0) | |
35 | #endif | |
36 | ||
37 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ | |
38 | struct bytecode_runtime { | |
39 | struct lttng_bytecode_runtime p; | |
3834b99f MD |
40 | size_t data_len; |
41 | size_t data_alloc_len; | |
42 | char *data; | |
07dfc1d0 | 43 | uint16_t len; |
3834b99f | 44 | char code[0]; |
07dfc1d0 MD |
45 | }; |
46 | ||
47 | enum entry_type { | |
48 | REG_S64, | |
1242217a | 49 | REG_U64, |
07dfc1d0 MD |
50 | REG_DOUBLE, |
51 | REG_STRING, | |
02aca193 | 52 | REG_STAR_GLOB_STRING, |
07dfc1d0 | 53 | REG_TYPE_UNKNOWN, |
3834b99f MD |
54 | REG_PTR, |
55 | }; | |
56 | ||
57 | enum load_type { | |
58 | LOAD_ROOT_CONTEXT, | |
59 | LOAD_ROOT_APP_CONTEXT, | |
60 | LOAD_ROOT_PAYLOAD, | |
61 | LOAD_OBJECT, | |
62 | }; | |
63 | ||
64 | enum object_type { | |
65 | OBJECT_TYPE_S8, | |
66 | OBJECT_TYPE_S16, | |
67 | OBJECT_TYPE_S32, | |
68 | OBJECT_TYPE_S64, | |
69 | OBJECT_TYPE_U8, | |
70 | OBJECT_TYPE_U16, | |
71 | OBJECT_TYPE_U32, | |
72 | OBJECT_TYPE_U64, | |
73 | ||
74 | OBJECT_TYPE_DOUBLE, | |
75 | OBJECT_TYPE_STRING, | |
76 | OBJECT_TYPE_STRING_SEQUENCE, | |
77 | ||
78 | OBJECT_TYPE_SEQUENCE, | |
79 | OBJECT_TYPE_ARRAY, | |
80 | OBJECT_TYPE_STRUCT, | |
81 | OBJECT_TYPE_VARIANT, | |
82 | ||
83 | OBJECT_TYPE_DYNAMIC, | |
84 | }; | |
85 | ||
80c2a69a | 86 | struct bytecode_get_index_data { |
3834b99f MD |
87 | uint64_t offset; /* in bytes */ |
88 | size_t ctx_index; | |
89 | size_t array_len; | |
03cb0cdd FD |
90 | /* |
91 | * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT | |
92 | * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the | |
93 | * interpreter needs to find it from the event fields and types to | |
94 | * support variants. | |
95 | */ | |
96 | const struct lttng_event_field *field; | |
3834b99f MD |
97 | struct { |
98 | size_t len; | |
99 | enum object_type type; | |
100 | bool rev_bo; /* reverse byte order */ | |
101 | } elem; | |
07dfc1d0 MD |
102 | }; |
103 | ||
104 | /* Validation stack */ | |
3834b99f MD |
105 | struct vstack_load { |
106 | enum load_type type; | |
107 | enum object_type object_type; | |
108 | const struct lttng_event_field *field; | |
109 | bool rev_bo; /* reverse byte order */ | |
110 | }; | |
111 | ||
07dfc1d0 MD |
112 | struct vstack_entry { |
113 | enum entry_type type; | |
3834b99f | 114 | struct vstack_load load; |
07dfc1d0 MD |
115 | }; |
116 | ||
117 | struct vstack { | |
118 | int top; /* top of stack */ | |
80c2a69a | 119 | struct vstack_entry e[INTERPRETER_STACK_LEN]; |
07dfc1d0 MD |
120 | }; |
121 | ||
122 | static inline | |
123 | void vstack_init(struct vstack *stack) | |
124 | { | |
125 | stack->top = -1; | |
126 | } | |
127 | ||
128 | static inline | |
129 | struct vstack_entry *vstack_ax(struct vstack *stack) | |
130 | { | |
131 | if (unlikely(stack->top < 0)) | |
132 | return NULL; | |
133 | return &stack->e[stack->top]; | |
134 | } | |
135 | ||
136 | static inline | |
137 | struct vstack_entry *vstack_bx(struct vstack *stack) | |
138 | { | |
139 | if (unlikely(stack->top < 1)) | |
140 | return NULL; | |
141 | return &stack->e[stack->top - 1]; | |
142 | } | |
143 | ||
144 | static inline | |
145 | int vstack_push(struct vstack *stack) | |
146 | { | |
80c2a69a | 147 | if (stack->top >= INTERPRETER_STACK_LEN - 1) { |
5a15f70c | 148 | printk(KERN_WARNING "LTTng: filter: Stack full\n"); |
07dfc1d0 MD |
149 | return -EINVAL; |
150 | } | |
151 | ++stack->top; | |
152 | return 0; | |
153 | } | |
154 | ||
155 | static inline | |
156 | int vstack_pop(struct vstack *stack) | |
157 | { | |
158 | if (unlikely(stack->top < 0)) { | |
5a15f70c | 159 | printk(KERN_WARNING "LTTng: filter: Stack empty\n"); |
07dfc1d0 MD |
160 | return -EINVAL; |
161 | } | |
162 | stack->top--; | |
163 | return 0; | |
164 | } | |
165 | ||
166 | /* Execution stack */ | |
02aca193 PP |
167 | enum estack_string_literal_type { |
168 | ESTACK_STRING_LITERAL_TYPE_NONE, | |
169 | ESTACK_STRING_LITERAL_TYPE_PLAIN, | |
170 | ESTACK_STRING_LITERAL_TYPE_STAR_GLOB, | |
171 | }; | |
172 | ||
3834b99f MD |
173 | struct load_ptr { |
174 | enum load_type type; | |
175 | enum object_type object_type; | |
176 | const void *ptr; | |
03cb0cdd | 177 | size_t nr_elem; |
3834b99f MD |
178 | bool rev_bo; |
179 | /* Temporary place-holders for contexts. */ | |
180 | union { | |
181 | int64_t s64; | |
182 | uint64_t u64; | |
183 | double d; | |
184 | } u; | |
3834b99f MD |
185 | const struct lttng_event_field *field; |
186 | }; | |
187 | ||
07dfc1d0 | 188 | struct estack_entry { |
d644d1df | 189 | enum entry_type type; |
07dfc1d0 MD |
190 | union { |
191 | int64_t v; | |
192 | ||
193 | struct { | |
194 | const char *str; | |
f127e61e | 195 | const char __user *user_str; |
07dfc1d0 | 196 | size_t seq_len; |
02aca193 | 197 | enum estack_string_literal_type literal_type; |
f127e61e | 198 | int user; /* is string from userspace ? */ |
07dfc1d0 | 199 | } s; |
3834b99f | 200 | struct load_ptr ptr; |
07dfc1d0 MD |
201 | } u; |
202 | }; | |
203 | ||
204 | struct estack { | |
205 | int top; /* top of stack */ | |
80c2a69a | 206 | struct estack_entry e[INTERPRETER_STACK_LEN]; |
07dfc1d0 MD |
207 | }; |
208 | ||
209 | #define estack_ax_v ax | |
210 | #define estack_bx_v bx | |
211 | ||
d644d1df FD |
212 | #define estack_ax_t ax_t |
213 | #define estack_bx_t bx_t | |
214 | ||
07dfc1d0 MD |
215 | #define estack_ax(stack, top) \ |
216 | ({ \ | |
80c2a69a | 217 | BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \ |
07dfc1d0 MD |
218 | &(stack)->e[top]; \ |
219 | }) | |
220 | ||
221 | #define estack_bx(stack, top) \ | |
222 | ({ \ | |
80c2a69a | 223 | BUG_ON((top) <= INTERPRETER_STACK_EMPTY + 1); \ |
07dfc1d0 MD |
224 | &(stack)->e[(top) - 1]; \ |
225 | }) | |
226 | ||
d644d1df | 227 | #define estack_push(stack, top, ax, bx, ax_t, bx_t) \ |
07dfc1d0 | 228 | do { \ |
80c2a69a | 229 | BUG_ON((top) >= INTERPRETER_STACK_LEN - 1); \ |
07dfc1d0 | 230 | (stack)->e[(top) - 1].u.v = (bx); \ |
d644d1df | 231 | (stack)->e[(top) - 1].type = (bx_t); \ |
07dfc1d0 | 232 | (bx) = (ax); \ |
d644d1df | 233 | (bx_t) = (ax_t); \ |
07dfc1d0 MD |
234 | ++(top); \ |
235 | } while (0) | |
236 | ||
d644d1df | 237 | #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \ |
07dfc1d0 | 238 | do { \ |
80c2a69a | 239 | BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \ |
07dfc1d0 | 240 | (ax) = (bx); \ |
d644d1df | 241 | (ax_t) = (bx_t); \ |
07dfc1d0 | 242 | (bx) = (stack)->e[(top) - 2].u.v; \ |
d644d1df | 243 | (bx_t) = (stack)->e[(top) - 2].type; \ |
07dfc1d0 MD |
244 | (top)--; \ |
245 | } while (0) | |
246 | ||
03cb0cdd FD |
247 | enum lttng_interpreter_type { |
248 | LTTNG_INTERPRETER_TYPE_S64, | |
249 | LTTNG_INTERPRETER_TYPE_U64, | |
250 | LTTNG_INTERPRETER_TYPE_SIGNED_ENUM, | |
251 | LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM, | |
252 | LTTNG_INTERPRETER_TYPE_DOUBLE, | |
253 | LTTNG_INTERPRETER_TYPE_STRING, | |
254 | LTTNG_INTERPRETER_TYPE_SEQUENCE, | |
255 | }; | |
256 | ||
257 | /* | |
258 | * Represents the output parameter of the lttng interpreter. | |
259 | * Currently capturable field classes are integer, double, string and sequence | |
260 | * of integer. | |
261 | */ | |
262 | struct lttng_interpreter_output { | |
263 | enum lttng_interpreter_type type; | |
264 | union { | |
265 | int64_t s; | |
266 | uint64_t u; | |
267 | ||
268 | struct { | |
269 | const char *str; | |
270 | size_t len; | |
271 | } str; | |
272 | struct { | |
273 | const void *ptr; | |
274 | size_t nr_elem; | |
275 | ||
276 | /* Inner type. */ | |
277 | const struct lttng_type *nested_type; | |
278 | } sequence; | |
279 | } u; | |
280 | }; | |
281 | ||
80c2a69a | 282 | const char *lttng_bytecode_print_op(enum bytecode_op op); |
07dfc1d0 | 283 | |
80c2a69a FD |
284 | int lttng_bytecode_validate(struct bytecode_runtime *bytecode); |
285 | int lttng_bytecode_specialize(const struct lttng_event_desc *event_desc, | |
3834b99f | 286 | struct bytecode_runtime *bytecode); |
07dfc1d0 | 287 | |
80c2a69a | 288 | uint64_t lttng_bytecode_filter_interpret_false(void *filter_data, |
79150a49 | 289 | struct lttng_probe_ctx *lttng_probe_ctx, |
07dfc1d0 | 290 | const char *filter_stack_data); |
80c2a69a | 291 | uint64_t lttng_bytecode_filter_interpret(void *filter_data, |
79150a49 | 292 | struct lttng_probe_ctx *lttng_probe_ctx, |
07dfc1d0 MD |
293 | const char *filter_stack_data); |
294 | ||
295 | #endif /* _LTTNG_FILTER_H */ |