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