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