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