Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: MIT |
2 | * | |
80c2a69a | 3 | * lttng-bytecode.c |
07dfc1d0 | 4 | * |
80c2a69a | 5 | * LTTng modules bytecode code. |
07dfc1d0 | 6 | * |
bbf3aef5 | 7 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
07dfc1d0 MD |
8 | */ |
9 | ||
10 | #include <linux/list.h> | |
11 | #include <linux/slab.h> | |
12 | ||
80c2a69a | 13 | #include <lttng/lttng-bytecode.h> |
07dfc1d0 MD |
14 | |
15 | static const char *opnames[] = { | |
80c2a69a | 16 | [ BYTECODE_OP_UNKNOWN ] = "UNKNOWN", |
07dfc1d0 | 17 | |
80c2a69a | 18 | [ BYTECODE_OP_RETURN ] = "RETURN", |
07dfc1d0 MD |
19 | |
20 | /* binary */ | |
80c2a69a FD |
21 | [ BYTECODE_OP_MUL ] = "MUL", |
22 | [ BYTECODE_OP_DIV ] = "DIV", | |
23 | [ BYTECODE_OP_MOD ] = "MOD", | |
24 | [ BYTECODE_OP_PLUS ] = "PLUS", | |
25 | [ BYTECODE_OP_MINUS ] = "MINUS", | |
26 | [ BYTECODE_OP_BIT_RSHIFT ] = "BIT_RSHIFT", | |
27 | [ BYTECODE_OP_BIT_LSHIFT ] = "BIT_LSHIFT", | |
28 | [ BYTECODE_OP_BIT_AND ] = "BIT_AND", | |
29 | [ BYTECODE_OP_BIT_OR ] = "BIT_OR", | |
30 | [ BYTECODE_OP_BIT_XOR ] = "BIT_XOR", | |
07dfc1d0 MD |
31 | |
32 | /* binary comparators */ | |
80c2a69a FD |
33 | [ BYTECODE_OP_EQ ] = "EQ", |
34 | [ BYTECODE_OP_NE ] = "NE", | |
35 | [ BYTECODE_OP_GT ] = "GT", | |
36 | [ BYTECODE_OP_LT ] = "LT", | |
37 | [ BYTECODE_OP_GE ] = "GE", | |
38 | [ BYTECODE_OP_LE ] = "LE", | |
07dfc1d0 MD |
39 | |
40 | /* string binary comparators */ | |
80c2a69a FD |
41 | [ BYTECODE_OP_EQ_STRING ] = "EQ_STRING", |
42 | [ BYTECODE_OP_NE_STRING ] = "NE_STRING", | |
43 | [ BYTECODE_OP_GT_STRING ] = "GT_STRING", | |
44 | [ BYTECODE_OP_LT_STRING ] = "LT_STRING", | |
45 | [ BYTECODE_OP_GE_STRING ] = "GE_STRING", | |
46 | [ BYTECODE_OP_LE_STRING ] = "LE_STRING", | |
07dfc1d0 MD |
47 | |
48 | /* s64 binary comparators */ | |
80c2a69a FD |
49 | [ BYTECODE_OP_EQ_S64 ] = "EQ_S64", |
50 | [ BYTECODE_OP_NE_S64 ] = "NE_S64", | |
51 | [ BYTECODE_OP_GT_S64 ] = "GT_S64", | |
52 | [ BYTECODE_OP_LT_S64 ] = "LT_S64", | |
53 | [ BYTECODE_OP_GE_S64 ] = "GE_S64", | |
54 | [ BYTECODE_OP_LE_S64 ] = "LE_S64", | |
07dfc1d0 MD |
55 | |
56 | /* double binary comparators */ | |
80c2a69a FD |
57 | [ BYTECODE_OP_EQ_DOUBLE ] = "EQ_DOUBLE", |
58 | [ BYTECODE_OP_NE_DOUBLE ] = "NE_DOUBLE", | |
59 | [ BYTECODE_OP_GT_DOUBLE ] = "GT_DOUBLE", | |
60 | [ BYTECODE_OP_LT_DOUBLE ] = "LT_DOUBLE", | |
61 | [ BYTECODE_OP_GE_DOUBLE ] = "GE_DOUBLE", | |
62 | [ BYTECODE_OP_LE_DOUBLE ] = "LE_DOUBLE", | |
07dfc1d0 MD |
63 | |
64 | /* Mixed S64-double binary comparators */ | |
80c2a69a FD |
65 | [ BYTECODE_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64", |
66 | [ BYTECODE_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64", | |
67 | [ BYTECODE_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64", | |
68 | [ BYTECODE_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64", | |
69 | [ BYTECODE_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64", | |
70 | [ BYTECODE_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64", | |
71 | ||
72 | [ BYTECODE_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE", | |
73 | [ BYTECODE_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE", | |
74 | [ BYTECODE_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE", | |
75 | [ BYTECODE_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE", | |
76 | [ BYTECODE_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE", | |
77 | [ BYTECODE_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE", | |
07dfc1d0 MD |
78 | |
79 | /* unary */ | |
80c2a69a FD |
80 | [ BYTECODE_OP_UNARY_PLUS ] = "UNARY_PLUS", |
81 | [ BYTECODE_OP_UNARY_MINUS ] = "UNARY_MINUS", | |
82 | [ BYTECODE_OP_UNARY_NOT ] = "UNARY_NOT", | |
83 | [ BYTECODE_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64", | |
84 | [ BYTECODE_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64", | |
85 | [ BYTECODE_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64", | |
86 | [ BYTECODE_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE", | |
87 | [ BYTECODE_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE", | |
88 | [ BYTECODE_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE", | |
07dfc1d0 MD |
89 | |
90 | /* logical */ | |
80c2a69a FD |
91 | [ BYTECODE_OP_AND ] = "AND", |
92 | [ BYTECODE_OP_OR ] = "OR", | |
07dfc1d0 MD |
93 | |
94 | /* load field ref */ | |
80c2a69a FD |
95 | [ BYTECODE_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF", |
96 | [ BYTECODE_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING", | |
97 | [ BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE", | |
98 | [ BYTECODE_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64", | |
99 | [ BYTECODE_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE", | |
07dfc1d0 MD |
100 | |
101 | /* load from immediate operand */ | |
80c2a69a FD |
102 | [ BYTECODE_OP_LOAD_STRING ] = "LOAD_STRING", |
103 | [ BYTECODE_OP_LOAD_S64 ] = "LOAD_S64", | |
104 | [ BYTECODE_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE", | |
07dfc1d0 MD |
105 | |
106 | /* cast */ | |
80c2a69a FD |
107 | [ BYTECODE_OP_CAST_TO_S64 ] = "CAST_TO_S64", |
108 | [ BYTECODE_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64", | |
109 | [ BYTECODE_OP_CAST_NOP ] = "CAST_NOP", | |
07dfc1d0 MD |
110 | |
111 | /* get context ref */ | |
80c2a69a FD |
112 | [ BYTECODE_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF", |
113 | [ BYTECODE_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING", | |
114 | [ BYTECODE_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64", | |
115 | [ BYTECODE_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE", | |
f127e61e MD |
116 | |
117 | /* load userspace field ref */ | |
80c2a69a FD |
118 | [ BYTECODE_OP_LOAD_FIELD_REF_USER_STRING ] = "LOAD_FIELD_REF_USER_STRING", |
119 | [ BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE ] = "LOAD_FIELD_REF_USER_SEQUENCE", | |
83fb57ab MD |
120 | |
121 | /* | |
122 | * load immediate star globbing pattern (literal string) | |
123 | * from immediate. | |
124 | */ | |
80c2a69a | 125 | [ BYTECODE_OP_LOAD_STAR_GLOB_STRING ] = "LOAD_STAR_GLOB_STRING", |
83fb57ab MD |
126 | |
127 | /* globbing pattern binary operator: apply to */ | |
80c2a69a FD |
128 | [ BYTECODE_OP_EQ_STAR_GLOB_STRING ] = "EQ_STAR_GLOB_STRING", |
129 | [ BYTECODE_OP_NE_STAR_GLOB_STRING ] = "NE_STAR_GLOB_STRING", | |
3834b99f MD |
130 | |
131 | /* | |
132 | * Instructions for recursive traversal through composed types. | |
133 | */ | |
80c2a69a FD |
134 | [ BYTECODE_OP_GET_CONTEXT_ROOT ] = "GET_CONTEXT_ROOT", |
135 | [ BYTECODE_OP_GET_APP_CONTEXT_ROOT ] = "GET_APP_CONTEXT_ROOT", | |
136 | [ BYTECODE_OP_GET_PAYLOAD_ROOT ] = "GET_PAYLOAD_ROOT", | |
137 | ||
138 | [ BYTECODE_OP_GET_SYMBOL ] = "GET_SYMBOL", | |
139 | [ BYTECODE_OP_GET_SYMBOL_FIELD ] = "GET_SYMBOL_FIELD", | |
140 | [ BYTECODE_OP_GET_INDEX_U16 ] = "GET_INDEX_U16", | |
141 | [ BYTECODE_OP_GET_INDEX_U64 ] = "GET_INDEX_U64", | |
142 | ||
143 | [ BYTECODE_OP_LOAD_FIELD ] = "LOAD_FIELD", | |
144 | [ BYTECODE_OP_LOAD_FIELD_S8 ] = "LOAD_FIELD_S8", | |
145 | [ BYTECODE_OP_LOAD_FIELD_S16 ] = "LOAD_FIELD_S16", | |
146 | [ BYTECODE_OP_LOAD_FIELD_S32 ] = "LOAD_FIELD_S32", | |
147 | [ BYTECODE_OP_LOAD_FIELD_S64 ] = "LOAD_FIELD_S64", | |
148 | [ BYTECODE_OP_LOAD_FIELD_U8 ] = "LOAD_FIELD_U8", | |
149 | [ BYTECODE_OP_LOAD_FIELD_U16 ] = "LOAD_FIELD_U16", | |
150 | [ BYTECODE_OP_LOAD_FIELD_U32 ] = "LOAD_FIELD_U32", | |
151 | [ BYTECODE_OP_LOAD_FIELD_U64 ] = "LOAD_FIELD_U64", | |
152 | [ BYTECODE_OP_LOAD_FIELD_STRING ] = "LOAD_FIELD_STRING", | |
153 | [ BYTECODE_OP_LOAD_FIELD_SEQUENCE ] = "LOAD_FIELD_SEQUENCE", | |
154 | [ BYTECODE_OP_LOAD_FIELD_DOUBLE ] = "LOAD_FIELD_DOUBLE", | |
155 | ||
156 | [ BYTECODE_OP_UNARY_BIT_NOT ] = "UNARY_BIT_NOT", | |
157 | ||
158 | [ BYTECODE_OP_RETURN_S64 ] = "RETURN_S64", | |
07dfc1d0 MD |
159 | }; |
160 | ||
80c2a69a | 161 | const char *lttng_bytecode_print_op(enum bytecode_op op) |
07dfc1d0 | 162 | { |
80c2a69a | 163 | if (op >= NR_BYTECODE_OPS) |
07dfc1d0 MD |
164 | return "UNKNOWN"; |
165 | else | |
166 | return opnames[op]; | |
167 | } | |
168 | ||
169 | static | |
2dfda770 | 170 | int apply_field_reloc(const struct lttng_event_desc *event_desc, |
07dfc1d0 MD |
171 | struct bytecode_runtime *runtime, |
172 | uint32_t runtime_len, | |
173 | uint32_t reloc_offset, | |
3834b99f | 174 | const char *field_name, |
80c2a69a | 175 | enum bytecode_op bytecode_op) |
07dfc1d0 | 176 | { |
07dfc1d0 MD |
177 | const struct lttng_event_field *fields, *field = NULL; |
178 | unsigned int nr_fields, i; | |
07dfc1d0 MD |
179 | struct load_op *op; |
180 | uint32_t field_offset = 0; | |
181 | ||
182 | dbg_printk("Apply field reloc: %u %s\n", reloc_offset, field_name); | |
183 | ||
184 | /* Lookup event by name */ | |
2dfda770 | 185 | if (!event_desc) |
07dfc1d0 | 186 | return -EINVAL; |
2dfda770 | 187 | fields = event_desc->fields; |
07dfc1d0 MD |
188 | if (!fields) |
189 | return -EINVAL; | |
2dfda770 | 190 | nr_fields = event_desc->nr_fields; |
07dfc1d0 | 191 | for (i = 0; i < nr_fields; i++) { |
ceabb767 MD |
192 | if (fields[i].nofilter) |
193 | continue; | |
07dfc1d0 MD |
194 | if (!strcmp(fields[i].name, field_name)) { |
195 | field = &fields[i]; | |
196 | break; | |
197 | } | |
198 | /* compute field offset */ | |
199 | switch (fields[i].type.atype) { | |
200 | case atype_integer: | |
ceabb767 | 201 | case atype_enum_nestable: |
07dfc1d0 MD |
202 | field_offset += sizeof(int64_t); |
203 | break; | |
ceabb767 MD |
204 | case atype_array_nestable: |
205 | if (!lttng_is_bytewise_integer(fields[i].type.u.array_nestable.elem_type)) | |
206 | return -EINVAL; | |
207 | field_offset += sizeof(unsigned long); | |
208 | field_offset += sizeof(void *); | |
209 | break; | |
210 | case atype_sequence_nestable: | |
211 | if (!lttng_is_bytewise_integer(fields[i].type.u.sequence_nestable.elem_type)) | |
212 | return -EINVAL; | |
07dfc1d0 MD |
213 | field_offset += sizeof(unsigned long); |
214 | field_offset += sizeof(void *); | |
215 | break; | |
216 | case atype_string: | |
217 | field_offset += sizeof(void *); | |
218 | break; | |
ceabb767 MD |
219 | case atype_struct_nestable: /* Unsupported. */ |
220 | case atype_variant_nestable: /* Unsupported. */ | |
07dfc1d0 MD |
221 | default: |
222 | return -EINVAL; | |
223 | } | |
224 | } | |
225 | if (!field) | |
226 | return -EINVAL; | |
227 | ||
228 | /* Check if field offset is too large for 16-bit offset */ | |
f127e61e | 229 | if (field_offset > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1) |
07dfc1d0 MD |
230 | return -EINVAL; |
231 | ||
232 | /* set type */ | |
3834b99f MD |
233 | op = (struct load_op *) &runtime->code[reloc_offset]; |
234 | ||
80c2a69a FD |
235 | switch (bytecode_op) { |
236 | case BYTECODE_OP_LOAD_FIELD_REF: | |
3834b99f MD |
237 | { |
238 | struct field_ref *field_ref; | |
239 | ||
240 | field_ref = (struct field_ref *) op->data; | |
241 | switch (field->type.atype) { | |
242 | case atype_integer: | |
ceabb767 | 243 | case atype_enum_nestable: |
80c2a69a | 244 | op->op = BYTECODE_OP_LOAD_FIELD_REF_S64; |
3834b99f | 245 | break; |
ceabb767 MD |
246 | case atype_array_nestable: |
247 | case atype_sequence_nestable: | |
3834b99f | 248 | if (field->user) |
80c2a69a | 249 | op->op = BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE; |
3834b99f | 250 | else |
80c2a69a | 251 | op->op = BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE; |
3834b99f MD |
252 | break; |
253 | case atype_string: | |
254 | if (field->user) | |
80c2a69a | 255 | op->op = BYTECODE_OP_LOAD_FIELD_REF_USER_STRING; |
3834b99f | 256 | else |
80c2a69a | 257 | op->op = BYTECODE_OP_LOAD_FIELD_REF_STRING; |
3834b99f | 258 | break; |
ceabb767 MD |
259 | case atype_struct_nestable: /* Unsupported. */ |
260 | case atype_variant_nestable: /* Unsupported. */ | |
3834b99f MD |
261 | default: |
262 | return -EINVAL; | |
263 | } | |
264 | /* set offset */ | |
265 | field_ref->offset = (uint16_t) field_offset; | |
07dfc1d0 | 266 | break; |
3834b99f | 267 | } |
07dfc1d0 MD |
268 | default: |
269 | return -EINVAL; | |
270 | } | |
07dfc1d0 MD |
271 | return 0; |
272 | } | |
273 | ||
274 | static | |
2dfda770 | 275 | int apply_context_reloc(struct bytecode_runtime *runtime, |
07dfc1d0 MD |
276 | uint32_t runtime_len, |
277 | uint32_t reloc_offset, | |
3834b99f | 278 | const char *context_name, |
80c2a69a | 279 | enum bytecode_op bytecode_op) |
07dfc1d0 | 280 | { |
07dfc1d0 MD |
281 | struct load_op *op; |
282 | struct lttng_ctx_field *ctx_field; | |
283 | int idx; | |
284 | ||
285 | dbg_printk("Apply context reloc: %u %s\n", reloc_offset, context_name); | |
286 | ||
287 | /* Get context index */ | |
288 | idx = lttng_get_context_index(lttng_static_ctx, context_name); | |
289 | if (idx < 0) | |
290 | return -ENOENT; | |
291 | ||
292 | /* Check if idx is too large for 16-bit offset */ | |
f127e61e | 293 | if (idx > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1) |
07dfc1d0 MD |
294 | return -EINVAL; |
295 | ||
296 | /* Get context return type */ | |
297 | ctx_field = <tng_static_ctx->fields[idx]; | |
3834b99f MD |
298 | op = (struct load_op *) &runtime->code[reloc_offset]; |
299 | ||
80c2a69a FD |
300 | switch (bytecode_op) { |
301 | case BYTECODE_OP_GET_CONTEXT_REF: | |
3834b99f MD |
302 | { |
303 | struct field_ref *field_ref; | |
304 | ||
305 | field_ref = (struct field_ref *) op->data; | |
306 | switch (ctx_field->event_field.type.atype) { | |
307 | case atype_integer: | |
ceabb767 | 308 | case atype_enum_nestable: |
80c2a69a | 309 | op->op = BYTECODE_OP_GET_CONTEXT_REF_S64; |
3834b99f MD |
310 | break; |
311 | /* Sequence and array supported as string */ | |
312 | case atype_string: | |
3834b99f | 313 | BUG_ON(ctx_field->event_field.user); |
80c2a69a | 314 | op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING; |
3834b99f | 315 | break; |
ceabb767 MD |
316 | case atype_array_nestable: |
317 | if (!lttng_is_bytewise_integer(ctx_field->event_field.type.u.array_nestable.elem_type)) | |
318 | return -EINVAL; | |
319 | BUG_ON(ctx_field->event_field.user); | |
80c2a69a | 320 | op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING; |
ceabb767 MD |
321 | break; |
322 | case atype_sequence_nestable: | |
323 | if (!lttng_is_bytewise_integer(ctx_field->event_field.type.u.sequence_nestable.elem_type)) | |
324 | return -EINVAL; | |
325 | BUG_ON(ctx_field->event_field.user); | |
80c2a69a | 326 | op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING; |
ceabb767 MD |
327 | break; |
328 | case atype_struct_nestable: /* Unsupported. */ | |
329 | case atype_variant_nestable: /* Unsupported. */ | |
3834b99f MD |
330 | default: |
331 | return -EINVAL; | |
332 | } | |
333 | /* set offset to context index within channel contexts */ | |
334 | field_ref->offset = (uint16_t) idx; | |
07dfc1d0 | 335 | break; |
3834b99f | 336 | } |
07dfc1d0 MD |
337 | default: |
338 | return -EINVAL; | |
339 | } | |
07dfc1d0 MD |
340 | return 0; |
341 | } | |
342 | ||
343 | static | |
2dfda770 | 344 | int apply_reloc(const struct lttng_event_desc *event_desc, |
07dfc1d0 MD |
345 | struct bytecode_runtime *runtime, |
346 | uint32_t runtime_len, | |
347 | uint32_t reloc_offset, | |
348 | const char *name) | |
349 | { | |
350 | struct load_op *op; | |
351 | ||
352 | dbg_printk("Apply reloc: %u %s\n", reloc_offset, name); | |
353 | ||
354 | /* Ensure that the reloc is within the code */ | |
355 | if (runtime_len - reloc_offset < sizeof(uint16_t)) | |
356 | return -EINVAL; | |
357 | ||
3834b99f | 358 | op = (struct load_op *) &runtime->code[reloc_offset]; |
07dfc1d0 | 359 | switch (op->op) { |
80c2a69a | 360 | case BYTECODE_OP_LOAD_FIELD_REF: |
2dfda770 | 361 | return apply_field_reloc(event_desc, runtime, runtime_len, |
3834b99f | 362 | reloc_offset, name, op->op); |
80c2a69a | 363 | case BYTECODE_OP_GET_CONTEXT_REF: |
2dfda770 | 364 | return apply_context_reloc(runtime, runtime_len, |
3834b99f | 365 | reloc_offset, name, op->op); |
80c2a69a FD |
366 | case BYTECODE_OP_GET_SYMBOL: |
367 | case BYTECODE_OP_GET_SYMBOL_FIELD: | |
3834b99f MD |
368 | /* |
369 | * Will be handled by load specialize phase or | |
370 | * dynamically by interpreter. | |
371 | */ | |
372 | return 0; | |
07dfc1d0 | 373 | default: |
5a15f70c | 374 | printk(KERN_WARNING "LTTng: filter: Unknown reloc op type %u\n", op->op); |
07dfc1d0 MD |
375 | return -EINVAL; |
376 | } | |
377 | return 0; | |
378 | } | |
379 | ||
380 | static | |
89ec2b91 | 381 | int bytecode_is_linked(struct lttng_bytecode_node *bytecode, |
2dfda770 | 382 | struct list_head *bytecode_runtime_head) |
07dfc1d0 MD |
383 | { |
384 | struct lttng_bytecode_runtime *bc_runtime; | |
385 | ||
2dfda770 | 386 | list_for_each_entry(bc_runtime, bytecode_runtime_head, node) { |
89ec2b91 | 387 | if (bc_runtime->bc == bytecode) |
07dfc1d0 MD |
388 | return 1; |
389 | } | |
390 | return 0; | |
391 | } | |
392 | ||
393 | /* | |
394 | * Take a bytecode with reloc table and link it to an event to create a | |
395 | * bytecode runtime. | |
396 | */ | |
397 | static | |
fdd179d7 | 398 | int link_bytecode(const struct lttng_event_desc *event_desc, |
2dfda770 | 399 | struct lttng_ctx *ctx, |
89ec2b91 | 400 | struct lttng_bytecode_node *bytecode, |
07dfc1d0 MD |
401 | struct list_head *insert_loc) |
402 | { | |
403 | int ret, offset, next_offset; | |
404 | struct bytecode_runtime *runtime = NULL; | |
405 | size_t runtime_alloc_len; | |
406 | ||
89ec2b91 | 407 | if (!bytecode) |
07dfc1d0 MD |
408 | return 0; |
409 | /* Bytecode already linked */ | |
89ec2b91 | 410 | if (bytecode_is_linked(bytecode, insert_loc)) |
07dfc1d0 MD |
411 | return 0; |
412 | ||
413 | dbg_printk("Linking...\n"); | |
414 | ||
415 | /* We don't need the reloc table in the runtime */ | |
89ec2b91 | 416 | runtime_alloc_len = sizeof(*runtime) + bytecode->bc.reloc_offset; |
07dfc1d0 MD |
417 | runtime = kzalloc(runtime_alloc_len, GFP_KERNEL); |
418 | if (!runtime) { | |
419 | ret = -ENOMEM; | |
420 | goto alloc_error; | |
421 | } | |
89ec2b91 | 422 | runtime->p.bc = bytecode; |
2dfda770 | 423 | runtime->p.ctx = ctx; |
89ec2b91 | 424 | runtime->len = bytecode->bc.reloc_offset; |
07dfc1d0 | 425 | /* copy original bytecode */ |
89ec2b91 | 426 | memcpy(runtime->code, bytecode->bc.data, runtime->len); |
07dfc1d0 MD |
427 | /* |
428 | * apply relocs. Those are a uint16_t (offset in bytecode) | |
429 | * followed by a string (field name). | |
430 | */ | |
89ec2b91 FD |
431 | for (offset = bytecode->bc.reloc_offset; |
432 | offset < bytecode->bc.len; | |
07dfc1d0 MD |
433 | offset = next_offset) { |
434 | uint16_t reloc_offset = | |
89ec2b91 | 435 | *(uint16_t *) &bytecode->bc.data[offset]; |
07dfc1d0 | 436 | const char *name = |
89ec2b91 | 437 | (const char *) &bytecode->bc.data[offset + sizeof(uint16_t)]; |
07dfc1d0 | 438 | |
2dfda770 | 439 | ret = apply_reloc(event_desc, runtime, runtime->len, reloc_offset, name); |
07dfc1d0 MD |
440 | if (ret) { |
441 | goto link_error; | |
442 | } | |
443 | next_offset = offset + sizeof(uint16_t) + strlen(name) + 1; | |
444 | } | |
445 | /* Validate bytecode */ | |
80c2a69a | 446 | ret = lttng_bytecode_validate(runtime); |
07dfc1d0 MD |
447 | if (ret) { |
448 | goto link_error; | |
449 | } | |
450 | /* Specialize bytecode */ | |
80c2a69a | 451 | ret = lttng_bytecode_specialize(event_desc, runtime); |
07dfc1d0 MD |
452 | if (ret) { |
453 | goto link_error; | |
454 | } | |
3d650c7b FD |
455 | |
456 | switch (bytecode->type) { | |
457 | case LTTNG_BYTECODE_NODE_TYPE_FILTER: | |
458 | runtime->p.interpreter_funcs.filter = lttng_bytecode_filter_interpret; | |
459 | break; | |
460 | default: | |
461 | WARN_ON(1); | |
462 | } | |
463 | ||
07dfc1d0 MD |
464 | runtime->p.link_failed = 0; |
465 | list_add_rcu(&runtime->p.node, insert_loc); | |
466 | dbg_printk("Linking successful.\n"); | |
467 | return 0; | |
468 | ||
469 | link_error: | |
3d650c7b FD |
470 | |
471 | switch (bytecode->type) { | |
472 | case LTTNG_BYTECODE_NODE_TYPE_FILTER: | |
473 | runtime->p.interpreter_funcs.filter = lttng_bytecode_filter_interpret_false; | |
474 | break; | |
475 | default: | |
476 | WARN_ON(1); | |
477 | } | |
07dfc1d0 MD |
478 | runtime->p.link_failed = 1; |
479 | list_add_rcu(&runtime->p.node, insert_loc); | |
480 | alloc_error: | |
481 | dbg_printk("Linking failed.\n"); | |
482 | return ret; | |
483 | } | |
484 | ||
0b365677 | 485 | void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime) |
07dfc1d0 | 486 | { |
89ec2b91 | 487 | struct lttng_bytecode_node *bc = runtime->bc; |
07dfc1d0 MD |
488 | |
489 | if (!bc->enabler->enabled || runtime->link_failed) | |
3d650c7b | 490 | runtime->interpreter_funcs.filter = lttng_bytecode_filter_interpret_false; |
07dfc1d0 | 491 | else |
3d650c7b | 492 | runtime->interpreter_funcs.filter = lttng_bytecode_filter_interpret; |
07dfc1d0 MD |
493 | } |
494 | ||
495 | /* | |
60206944 FD |
496 | * Given the lists of bytecode programs of an instance (trigger or event) and |
497 | * of a matching enabler, try to link all the enabler's bytecode programs with | |
498 | * the instance. | |
499 | * | |
500 | * This function is called after we confirmed that name enabler and the | |
501 | * instance are matching names (or glob pattern matching). | |
07dfc1d0 | 502 | */ |
2dfda770 FD |
503 | void lttng_enabler_link_bytecode(const struct lttng_event_desc *event_desc, |
504 | struct lttng_ctx *ctx, | |
60206944 FD |
505 | struct list_head *instance_bytecode_head, |
506 | struct list_head *enabler_bytecode_head) | |
07dfc1d0 | 507 | { |
60206944 | 508 | struct lttng_bytecode_node *enabler_bc; |
07dfc1d0 MD |
509 | struct lttng_bytecode_runtime *runtime; |
510 | ||
2dfda770 | 511 | WARN_ON_ONCE(!event_desc); |
07dfc1d0 | 512 | |
60206944 FD |
513 | /* Go over all the bytecode programs of the enabler. */ |
514 | list_for_each_entry(enabler_bc, enabler_bytecode_head, node) { | |
07dfc1d0 MD |
515 | int found = 0, ret; |
516 | struct list_head *insert_loc; | |
517 | ||
60206944 FD |
518 | /* |
519 | * Check if the current enabler bytecode program is already | |
520 | * linked with the instance. | |
521 | */ | |
522 | list_for_each_entry(runtime, instance_bytecode_head, node) { | |
523 | if (runtime->bc == enabler_bc) { | |
07dfc1d0 MD |
524 | found = 1; |
525 | break; | |
526 | } | |
527 | } | |
60206944 FD |
528 | |
529 | /* | |
530 | * Skip bytecode already linked, go to the next enabler | |
531 | * bytecode program. | |
532 | */ | |
07dfc1d0 MD |
533 | if (found) |
534 | continue; | |
535 | ||
536 | /* | |
537 | * Insert at specified priority (seqnum) in increasing | |
df9d0220 FD |
538 | * order. If there already is a bytecode of the same priority, |
539 | * insert the new bytecode right after it. | |
07dfc1d0 MD |
540 | */ |
541 | list_for_each_entry_reverse(runtime, | |
60206944 FD |
542 | instance_bytecode_head, node) { |
543 | if (runtime->bc->bc.seqnum <= enabler_bc->bc.seqnum) { | |
07dfc1d0 MD |
544 | /* insert here */ |
545 | insert_loc = &runtime->node; | |
546 | goto add_within; | |
547 | } | |
548 | } | |
549 | /* Add to head to list */ | |
60206944 | 550 | insert_loc = instance_bytecode_head; |
07dfc1d0 MD |
551 | add_within: |
552 | dbg_printk("linking bytecode\n"); | |
60206944 | 553 | ret = link_bytecode(event_desc, ctx, enabler_bc, insert_loc); |
07dfc1d0 MD |
554 | if (ret) { |
555 | dbg_printk("[lttng filter] warning: cannot link event bytecode\n"); | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | /* | |
561 | * We own the filter_bytecode if we return success. | |
562 | */ | |
563 | int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler, | |
89ec2b91 | 564 | struct lttng_bytecode_node *filter_bytecode) |
07dfc1d0 MD |
565 | { |
566 | list_add(&filter_bytecode->node, &enabler->filter_bytecode_head); | |
567 | return 0; | |
568 | } | |
569 | ||
570 | void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler) | |
571 | { | |
89ec2b91 | 572 | struct lttng_bytecode_node *filter_bytecode, *tmp; |
07dfc1d0 MD |
573 | |
574 | list_for_each_entry_safe(filter_bytecode, tmp, | |
575 | &enabler->filter_bytecode_head, node) { | |
576 | kfree(filter_bytecode); | |
577 | } | |
578 | } | |
579 | ||
580 | void lttng_free_event_filter_runtime(struct lttng_event *event) | |
581 | { | |
582 | struct bytecode_runtime *runtime, *tmp; | |
583 | ||
584 | list_for_each_entry_safe(runtime, tmp, | |
183e8b3a | 585 | &event->filter_bytecode_runtime_head, p.node) { |
3834b99f | 586 | kfree(runtime->data); |
07dfc1d0 MD |
587 | kfree(runtime); |
588 | } | |
589 | } |