| 1 | /* |
| 2 | * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include <common/error.h> |
| 10 | #include <common/event-expr-to-bytecode.h> |
| 11 | #include <common/macros.h> |
| 12 | #include <inttypes.h> |
| 13 | #include <limits.h> |
| 14 | #include <lttng/condition/condition-internal.h> |
| 15 | #include <lttng/condition/on-event-internal.h> |
| 16 | #include <lttng/condition/on-event.h> |
| 17 | #include <lttng/event-expr-internal.h> |
| 18 | #include <lttng/event-expr.h> |
| 19 | #include <lttng/event-field-value-internal.h> |
| 20 | #include <lttng/event-rule/event-rule-internal.h> |
| 21 | #include <lttng/lttng-error.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <stdint.h> |
| 24 | #include <vendor/msgpack/msgpack.h> |
| 25 | |
| 26 | #define IS_ON_EVENT_CONDITION(condition) \ |
| 27 | (lttng_condition_get_type(condition) == \ |
| 28 | LTTNG_CONDITION_TYPE_ON_EVENT) |
| 29 | |
| 30 | typedef LTTNG_OPTIONAL(uint64_t) optional_uint64; |
| 31 | |
| 32 | static bool is_on_event_evaluation(const struct lttng_evaluation *evaluation) |
| 33 | { |
| 34 | enum lttng_condition_type type = lttng_evaluation_get_type(evaluation); |
| 35 | |
| 36 | return type == LTTNG_CONDITION_TYPE_ON_EVENT; |
| 37 | } |
| 38 | |
| 39 | static bool lttng_condition_on_event_validate( |
| 40 | const struct lttng_condition *condition); |
| 41 | static int lttng_condition_on_event_serialize( |
| 42 | const struct lttng_condition *condition, |
| 43 | struct lttng_payload *payload); |
| 44 | static bool lttng_condition_on_event_is_equal( |
| 45 | const struct lttng_condition *_a, |
| 46 | const struct lttng_condition *_b); |
| 47 | static void lttng_condition_on_event_destroy( |
| 48 | struct lttng_condition *condition); |
| 49 | |
| 50 | static bool lttng_condition_on_event_validate( |
| 51 | const struct lttng_condition *condition) |
| 52 | { |
| 53 | bool valid = false; |
| 54 | struct lttng_condition_on_event *event_rule; |
| 55 | |
| 56 | if (!condition) { |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | event_rule = container_of( |
| 61 | condition, struct lttng_condition_on_event, parent); |
| 62 | if (!event_rule->rule) { |
| 63 | ERR("Invalid on event condition: a rule must be set"); |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | valid = lttng_event_rule_validate(event_rule->rule); |
| 68 | end: |
| 69 | return valid; |
| 70 | } |
| 71 | |
| 72 | static const char *msgpack_object_type_str(msgpack_object_type type) |
| 73 | { |
| 74 | const char *name; |
| 75 | |
| 76 | switch (type) { |
| 77 | case MSGPACK_OBJECT_NIL: |
| 78 | name = "MSGPACK_OBJECT_NIL"; |
| 79 | break; |
| 80 | case MSGPACK_OBJECT_BOOLEAN: |
| 81 | name = "MSGPACK_OBJECT_BOOLEAN"; |
| 82 | break; |
| 83 | case MSGPACK_OBJECT_POSITIVE_INTEGER: |
| 84 | name = "MSGPACK_OBJECT_POSITIVE_INTEGER"; |
| 85 | break; |
| 86 | case MSGPACK_OBJECT_NEGATIVE_INTEGER: |
| 87 | name = "MSGPACK_OBJECT_NEGATIVE_INTEGER"; |
| 88 | break; |
| 89 | case MSGPACK_OBJECT_FLOAT32: |
| 90 | name = "MSGPACK_OBJECT_FLOAT32"; |
| 91 | break; |
| 92 | case MSGPACK_OBJECT_FLOAT: |
| 93 | /* Same value as MSGPACK_OBJECT_FLOAT64 */ |
| 94 | name = "MSGPACK_OBJECT_FLOAT(64)"; |
| 95 | break; |
| 96 | case MSGPACK_OBJECT_STR: |
| 97 | name = "MSGPACK_OBJECT_STR"; |
| 98 | break; |
| 99 | case MSGPACK_OBJECT_ARRAY: |
| 100 | name = "MSGPACK_OBJECT_ARRAY"; |
| 101 | break; |
| 102 | case MSGPACK_OBJECT_MAP: |
| 103 | name = "MSGPACK_OBJECT_MAP"; |
| 104 | break; |
| 105 | case MSGPACK_OBJECT_BIN: |
| 106 | name = "MSGPACK_OBJECT_BIN"; |
| 107 | break; |
| 108 | case MSGPACK_OBJECT_EXT: |
| 109 | name = "MSGPACK_OBJECT_EXT"; |
| 110 | break; |
| 111 | default: |
| 112 | abort(); |
| 113 | } |
| 114 | |
| 115 | return name; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Serializes the C string `str` into `buf`. |
| 120 | * |
| 121 | * Encoding is the length of `str` plus one (for the null character), |
| 122 | * and then the string, including its null terminator. |
| 123 | */ |
| 124 | static |
| 125 | int serialize_cstr(const char *str, struct lttng_dynamic_buffer *buf) |
| 126 | { |
| 127 | int ret; |
| 128 | const uint32_t len = strlen(str) + 1; |
| 129 | |
| 130 | /* Serialize the length, including the null terminator. */ |
| 131 | DBG("Serializing C string's length (including null terminator): " |
| 132 | "%" PRIu32, len); |
| 133 | ret = lttng_dynamic_buffer_append(buf, &len, sizeof(len)); |
| 134 | if (ret) { |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | /* Serialize the string. */ |
| 139 | DBG("Serializing C string: '%s'", str); |
| 140 | ret = lttng_dynamic_buffer_append(buf, str, len); |
| 141 | if (ret) { |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | end: |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Serializes the event expression `expr` into `buf`. |
| 151 | */ |
| 152 | static |
| 153 | int serialize_event_expr(const struct lttng_event_expr *expr, |
| 154 | struct lttng_payload *payload) |
| 155 | { |
| 156 | const uint8_t type = expr->type; |
| 157 | int ret; |
| 158 | |
| 159 | /* Serialize the expression's type. */ |
| 160 | DBG("Serializing event expression's type: %d", expr->type); |
| 161 | ret = lttng_dynamic_buffer_append(&payload->buffer, &type, sizeof(type)); |
| 162 | if (ret) { |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | /* Serialize the expression */ |
| 167 | switch (expr->type) { |
| 168 | case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: |
| 169 | case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: |
| 170 | { |
| 171 | const struct lttng_event_expr_field *field_expr = |
| 172 | container_of(expr, |
| 173 | const struct lttng_event_expr_field, |
| 174 | parent); |
| 175 | |
| 176 | /* Serialize the field name. */ |
| 177 | DBG("Serializing field event expression's field name: '%s'", |
| 178 | field_expr->name); |
| 179 | ret = serialize_cstr(field_expr->name, &payload->buffer); |
| 180 | if (ret) { |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | break; |
| 185 | } |
| 186 | case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: |
| 187 | { |
| 188 | const struct lttng_event_expr_app_specific_context_field *field_expr = |
| 189 | container_of(expr, |
| 190 | const struct lttng_event_expr_app_specific_context_field, |
| 191 | parent); |
| 192 | |
| 193 | /* Serialize the provider name. */ |
| 194 | DBG("Serializing app-specific context field event expression's " |
| 195 | "provider name: '%s'", |
| 196 | field_expr->provider_name); |
| 197 | ret = serialize_cstr(field_expr->provider_name, &payload->buffer); |
| 198 | if (ret) { |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | /* Serialize the type name. */ |
| 203 | DBG("Serializing app-specific context field event expression's " |
| 204 | "type name: '%s'", |
| 205 | field_expr->provider_name); |
| 206 | ret = serialize_cstr(field_expr->type_name, &payload->buffer); |
| 207 | if (ret) { |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | break; |
| 212 | } |
| 213 | case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: |
| 214 | { |
| 215 | const struct lttng_event_expr_array_field_element *elem_expr = |
| 216 | container_of(expr, |
| 217 | const struct lttng_event_expr_array_field_element, |
| 218 | parent); |
| 219 | const uint32_t index = elem_expr->index; |
| 220 | |
| 221 | /* Serialize the index. */ |
| 222 | DBG("Serializing array field element event expression's " |
| 223 | "index: %u", elem_expr->index); |
| 224 | ret = lttng_dynamic_buffer_append(&payload->buffer, &index, sizeof(index)); |
| 225 | if (ret) { |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | /* Serialize the parent array field expression. */ |
| 230 | DBG("Serializing array field element event expression's " |
| 231 | "parent array field event expression"); |
| 232 | ret = serialize_event_expr(elem_expr->array_field_expr, payload); |
| 233 | if (ret) { |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | break; |
| 238 | } |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static |
| 248 | struct lttng_capture_descriptor * |
| 249 | lttng_condition_on_event_get_internal_capture_descriptor_at_index( |
| 250 | const struct lttng_condition *condition, unsigned int index) |
| 251 | { |
| 252 | const struct lttng_condition_on_event *on_event_cond = |
| 253 | container_of(condition, |
| 254 | const struct lttng_condition_on_event, |
| 255 | parent); |
| 256 | struct lttng_capture_descriptor *desc = NULL; |
| 257 | unsigned int count; |
| 258 | enum lttng_condition_status status; |
| 259 | |
| 260 | if (!condition || !IS_ON_EVENT_CONDITION(condition)) { |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | status = lttng_condition_on_event_get_capture_descriptor_count( |
| 265 | condition, &count); |
| 266 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | if (index >= count) { |
| 271 | goto end; |
| 272 | } |
| 273 | |
| 274 | desc = lttng_dynamic_pointer_array_get_pointer( |
| 275 | &on_event_cond->capture_descriptors, index); |
| 276 | end: |
| 277 | return desc; |
| 278 | } |
| 279 | |
| 280 | static int lttng_condition_on_event_serialize( |
| 281 | const struct lttng_condition *condition, |
| 282 | struct lttng_payload *payload) |
| 283 | { |
| 284 | int ret; |
| 285 | struct lttng_condition_on_event *on_event_condition; |
| 286 | enum lttng_condition_status status; |
| 287 | /* Used for iteration and communication (size matters). */ |
| 288 | uint32_t i, capture_descr_count; |
| 289 | LTTNG_OPTIONAL_COMM(typeof(on_event_condition->error_count.value)) error_count_comm; |
| 290 | |
| 291 | if (!condition || !IS_ON_EVENT_CONDITION(condition)) { |
| 292 | ret = -1; |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | DBG("Serializing on event condition"); |
| 297 | on_event_condition = container_of( |
| 298 | condition, struct lttng_condition_on_event, parent); |
| 299 | |
| 300 | DBG("Serializing on event condition's event rule"); |
| 301 | ret = lttng_event_rule_serialize(on_event_condition->rule, payload); |
| 302 | if (ret) { |
| 303 | goto end; |
| 304 | } |
| 305 | |
| 306 | error_count_comm = (typeof(error_count_comm)) { |
| 307 | .is_set = on_event_condition->error_count.is_set, |
| 308 | .value = on_event_condition->error_count.value, |
| 309 | }; |
| 310 | |
| 311 | { |
| 312 | char error_count_value_str[MAX_INT_DEC_LEN(on_event_condition->error_count.value)]; |
| 313 | const int fmt_ret = snprintf(error_count_value_str, |
| 314 | sizeof(error_count_value_str), "%" PRIu64, |
| 315 | on_event_condition->error_count.value); |
| 316 | |
| 317 | assert(fmt_ret > 0); |
| 318 | DBG("Serializing event rule condition's error count: value = %s", |
| 319 | on_event_condition->error_count.is_set ? |
| 320 | error_count_value_str : |
| 321 | "(unset)"); |
| 322 | } |
| 323 | |
| 324 | ret = lttng_dynamic_buffer_append(&payload->buffer, &error_count_comm, |
| 325 | sizeof(error_count_comm)); |
| 326 | if (ret) { |
| 327 | goto end; |
| 328 | } |
| 329 | |
| 330 | status = lttng_condition_on_event_get_capture_descriptor_count( |
| 331 | condition, &capture_descr_count); |
| 332 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 333 | ret = -1; |
| 334 | goto end; |
| 335 | }; |
| 336 | |
| 337 | DBG("Serializing on event condition's capture descriptor count: %" PRIu32, |
| 338 | capture_descr_count); |
| 339 | ret = lttng_dynamic_buffer_append(&payload->buffer, &capture_descr_count, |
| 340 | sizeof(capture_descr_count)); |
| 341 | if (ret) { |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | for (i = 0; i < capture_descr_count; i++) { |
| 346 | const struct lttng_capture_descriptor *desc = |
| 347 | lttng_condition_on_event_get_internal_capture_descriptor_at_index( |
| 348 | condition, i); |
| 349 | |
| 350 | DBG("Serializing on event condition's capture descriptor %" PRIu32, |
| 351 | i); |
| 352 | ret = serialize_event_expr(desc->event_expression, payload); |
| 353 | if (ret) { |
| 354 | goto end; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | end: |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | static |
| 363 | bool capture_descriptors_are_equal( |
| 364 | const struct lttng_condition *condition_a, |
| 365 | const struct lttng_condition *condition_b) |
| 366 | { |
| 367 | bool is_equal = true; |
| 368 | unsigned int capture_descr_count_a; |
| 369 | unsigned int capture_descr_count_b; |
| 370 | size_t i; |
| 371 | enum lttng_condition_status status; |
| 372 | |
| 373 | status = lttng_condition_on_event_get_capture_descriptor_count( |
| 374 | condition_a, &capture_descr_count_a); |
| 375 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 376 | goto not_equal; |
| 377 | } |
| 378 | |
| 379 | status = lttng_condition_on_event_get_capture_descriptor_count( |
| 380 | condition_b, &capture_descr_count_b); |
| 381 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 382 | goto not_equal; |
| 383 | } |
| 384 | |
| 385 | if (capture_descr_count_a != capture_descr_count_b) { |
| 386 | goto not_equal; |
| 387 | } |
| 388 | |
| 389 | for (i = 0; i < capture_descr_count_a; i++) { |
| 390 | const struct lttng_event_expr *expr_a = |
| 391 | lttng_condition_on_event_get_capture_descriptor_at_index( |
| 392 | condition_a, |
| 393 | i); |
| 394 | const struct lttng_event_expr *expr_b = |
| 395 | lttng_condition_on_event_get_capture_descriptor_at_index( |
| 396 | condition_b, |
| 397 | i); |
| 398 | |
| 399 | if (!lttng_event_expr_is_equal(expr_a, expr_b)) { |
| 400 | goto not_equal; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | goto end; |
| 405 | |
| 406 | not_equal: |
| 407 | is_equal = false; |
| 408 | |
| 409 | end: |
| 410 | return is_equal; |
| 411 | } |
| 412 | |
| 413 | static bool lttng_condition_on_event_is_equal( |
| 414 | const struct lttng_condition *_a, |
| 415 | const struct lttng_condition *_b) |
| 416 | { |
| 417 | bool is_equal = false; |
| 418 | struct lttng_condition_on_event *a, *b; |
| 419 | |
| 420 | a = container_of(_a, struct lttng_condition_on_event, parent); |
| 421 | b = container_of(_b, struct lttng_condition_on_event, parent); |
| 422 | |
| 423 | /* Both event rules must be set or both must be unset. */ |
| 424 | if ((a->rule && !b->rule) || (!a->rule && b->rule)) { |
| 425 | WARN("Comparing event_rule conditions with uninitialized rule"); |
| 426 | goto end; |
| 427 | } |
| 428 | |
| 429 | is_equal = lttng_event_rule_is_equal(a->rule, b->rule); |
| 430 | if (!is_equal) { |
| 431 | goto end; |
| 432 | } |
| 433 | |
| 434 | is_equal = capture_descriptors_are_equal(_a, _b); |
| 435 | |
| 436 | end: |
| 437 | return is_equal; |
| 438 | } |
| 439 | |
| 440 | static void lttng_condition_on_event_destroy( |
| 441 | struct lttng_condition *condition) |
| 442 | { |
| 443 | struct lttng_condition_on_event *on_event_condition; |
| 444 | |
| 445 | on_event_condition = container_of( |
| 446 | condition, struct lttng_condition_on_event, parent); |
| 447 | |
| 448 | lttng_event_rule_put(on_event_condition->rule); |
| 449 | lttng_dynamic_pointer_array_reset(&on_event_condition->capture_descriptors); |
| 450 | free(on_event_condition); |
| 451 | } |
| 452 | |
| 453 | static |
| 454 | void destroy_capture_descriptor(void *ptr) |
| 455 | { |
| 456 | struct lttng_capture_descriptor *desc = |
| 457 | (struct lttng_capture_descriptor *) ptr; |
| 458 | |
| 459 | lttng_event_expr_destroy(desc->event_expression); |
| 460 | free(desc->bytecode); |
| 461 | free(desc); |
| 462 | } |
| 463 | |
| 464 | struct lttng_condition *lttng_condition_on_event_create( |
| 465 | struct lttng_event_rule *rule) |
| 466 | { |
| 467 | struct lttng_condition *parent = NULL; |
| 468 | struct lttng_condition_on_event *condition = NULL; |
| 469 | |
| 470 | if (!rule) { |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | condition = zmalloc(sizeof(struct lttng_condition_on_event)); |
| 475 | if (!condition) { |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | lttng_condition_init(&condition->parent, |
| 480 | LTTNG_CONDITION_TYPE_ON_EVENT); |
| 481 | condition->parent.validate = lttng_condition_on_event_validate, |
| 482 | condition->parent.serialize = lttng_condition_on_event_serialize, |
| 483 | condition->parent.equal = lttng_condition_on_event_is_equal, |
| 484 | condition->parent.destroy = lttng_condition_on_event_destroy, |
| 485 | |
| 486 | lttng_event_rule_get(rule); |
| 487 | condition->rule = rule; |
| 488 | rule = NULL; |
| 489 | |
| 490 | lttng_dynamic_pointer_array_init(&condition->capture_descriptors, |
| 491 | destroy_capture_descriptor); |
| 492 | |
| 493 | parent = &condition->parent; |
| 494 | end: |
| 495 | return parent; |
| 496 | } |
| 497 | |
| 498 | static |
| 499 | uint64_t uint_from_buffer(const struct lttng_buffer_view *view, size_t size, |
| 500 | size_t *offset) |
| 501 | { |
| 502 | uint64_t ret; |
| 503 | const struct lttng_buffer_view uint_view = |
| 504 | lttng_buffer_view_from_view(view, *offset, size); |
| 505 | |
| 506 | if (!lttng_buffer_view_is_valid(&uint_view)) { |
| 507 | ret = UINT64_C(-1); |
| 508 | goto end; |
| 509 | } |
| 510 | |
| 511 | switch (size) { |
| 512 | case 1: |
| 513 | ret = (uint64_t) *uint_view.data; |
| 514 | break; |
| 515 | case sizeof(uint32_t): |
| 516 | { |
| 517 | uint32_t u32; |
| 518 | |
| 519 | memcpy(&u32, uint_view.data, sizeof(u32)); |
| 520 | ret = (uint64_t) u32; |
| 521 | break; |
| 522 | } |
| 523 | case sizeof(ret): |
| 524 | memcpy(&ret, uint_view.data, sizeof(ret)); |
| 525 | break; |
| 526 | default: |
| 527 | abort(); |
| 528 | } |
| 529 | |
| 530 | *offset += size; |
| 531 | |
| 532 | end: |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | static |
| 537 | int optional_uint_from_buffer( |
| 538 | const struct lttng_buffer_view *view, |
| 539 | size_t inner_value_size, |
| 540 | size_t *offset, |
| 541 | optional_uint64 *value) |
| 542 | { |
| 543 | int ret; |
| 544 | |
| 545 | /* |
| 546 | * Those cases are identical except for the optional's inner type width. |
| 547 | */ |
| 548 | switch (inner_value_size) { |
| 549 | case sizeof(uint8_t): |
| 550 | { |
| 551 | LTTNG_OPTIONAL_COMM(uint8_t) *value_comm; |
| 552 | const struct lttng_buffer_view optional_uint_view = |
| 553 | lttng_buffer_view_from_view(view, *offset, |
| 554 | sizeof(*value_comm)); |
| 555 | |
| 556 | if (!lttng_buffer_view_is_valid(&optional_uint_view)) { |
| 557 | ret = -1; |
| 558 | goto end; |
| 559 | } |
| 560 | |
| 561 | value_comm = (typeof(value_comm)) optional_uint_view.data; |
| 562 | *value = (typeof(*value)) { |
| 563 | .is_set = value_comm->is_set, |
| 564 | .value = (uint64_t) value_comm->value, |
| 565 | }; |
| 566 | |
| 567 | *offset += sizeof(*value_comm); |
| 568 | break; |
| 569 | } |
| 570 | case sizeof(uint16_t): |
| 571 | { |
| 572 | LTTNG_OPTIONAL_COMM(uint16_t) *value_comm; |
| 573 | const struct lttng_buffer_view optional_uint_view = |
| 574 | lttng_buffer_view_from_view(view, *offset, |
| 575 | sizeof(*value_comm)); |
| 576 | |
| 577 | if (!lttng_buffer_view_is_valid(&optional_uint_view)) { |
| 578 | ret = -1; |
| 579 | goto end; |
| 580 | } |
| 581 | |
| 582 | value_comm = (typeof(value_comm)) optional_uint_view.data; |
| 583 | *value = (typeof(*value)) { |
| 584 | .is_set = value_comm->is_set, |
| 585 | .value = (uint64_t) value_comm->value, |
| 586 | }; |
| 587 | |
| 588 | *offset += sizeof(*value_comm); |
| 589 | break; |
| 590 | } |
| 591 | case sizeof(uint32_t): |
| 592 | { |
| 593 | LTTNG_OPTIONAL_COMM(uint32_t) *value_comm; |
| 594 | const struct lttng_buffer_view optional_uint_view = |
| 595 | lttng_buffer_view_from_view(view, *offset, |
| 596 | sizeof(*value_comm)); |
| 597 | |
| 598 | if (!lttng_buffer_view_is_valid(&optional_uint_view)) { |
| 599 | ret = -1; |
| 600 | goto end; |
| 601 | } |
| 602 | |
| 603 | value_comm = (typeof(value_comm)) optional_uint_view.data; |
| 604 | *value = (typeof(*value)) { |
| 605 | .is_set = value_comm->is_set, |
| 606 | .value = (uint64_t) value_comm->value, |
| 607 | }; |
| 608 | |
| 609 | *offset += sizeof(*value_comm); |
| 610 | break; |
| 611 | } |
| 612 | case sizeof(uint64_t): |
| 613 | { |
| 614 | LTTNG_OPTIONAL_COMM(uint64_t) *value_comm; |
| 615 | const struct lttng_buffer_view optional_uint_view = |
| 616 | lttng_buffer_view_from_view(view, *offset, |
| 617 | sizeof(*value_comm)); |
| 618 | |
| 619 | if (!lttng_buffer_view_is_valid(&optional_uint_view)) { |
| 620 | ret = -1; |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | value_comm = (typeof(value_comm)) optional_uint_view.data; |
| 625 | *value = (typeof(*value)) { |
| 626 | .is_set = value_comm->is_set, |
| 627 | .value = (uint64_t) value_comm->value, |
| 628 | }; |
| 629 | |
| 630 | *offset += sizeof(*value_comm); |
| 631 | break; |
| 632 | } |
| 633 | default: |
| 634 | abort(); |
| 635 | } |
| 636 | |
| 637 | ret = 0; |
| 638 | end: |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | static |
| 643 | const char *str_from_buffer(const struct lttng_buffer_view *view, |
| 644 | size_t *offset) |
| 645 | { |
| 646 | uint64_t len; |
| 647 | const char *ret; |
| 648 | |
| 649 | len = uint_from_buffer(view, sizeof(uint32_t), offset); |
| 650 | if (len == UINT64_C(-1)) { |
| 651 | goto error; |
| 652 | } |
| 653 | |
| 654 | ret = &view->data[*offset]; |
| 655 | |
| 656 | if (!lttng_buffer_view_contains_string(view, ret, len)) { |
| 657 | goto error; |
| 658 | } |
| 659 | |
| 660 | *offset += len; |
| 661 | goto end; |
| 662 | |
| 663 | error: |
| 664 | ret = NULL; |
| 665 | |
| 666 | end: |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | static |
| 671 | struct lttng_event_expr *event_expr_from_payload( |
| 672 | struct lttng_payload_view *view, size_t *offset) |
| 673 | { |
| 674 | struct lttng_event_expr *expr = NULL; |
| 675 | const char *str; |
| 676 | uint64_t type; |
| 677 | |
| 678 | type = uint_from_buffer(&view->buffer, sizeof(uint8_t), offset); |
| 679 | if (type == UINT64_C(-1)) { |
| 680 | goto error; |
| 681 | } |
| 682 | |
| 683 | switch (type) { |
| 684 | case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: |
| 685 | str = str_from_buffer(&view->buffer, offset); |
| 686 | if (!str) { |
| 687 | goto error; |
| 688 | } |
| 689 | |
| 690 | expr = lttng_event_expr_event_payload_field_create(str); |
| 691 | break; |
| 692 | case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: |
| 693 | str = str_from_buffer(&view->buffer, offset); |
| 694 | if (!str) { |
| 695 | goto error; |
| 696 | } |
| 697 | |
| 698 | expr = lttng_event_expr_channel_context_field_create(str); |
| 699 | break; |
| 700 | case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: |
| 701 | { |
| 702 | const char *provider_name; |
| 703 | const char *type_name; |
| 704 | |
| 705 | provider_name = str_from_buffer(&view->buffer, offset); |
| 706 | if (!provider_name) { |
| 707 | goto error; |
| 708 | } |
| 709 | |
| 710 | type_name = str_from_buffer(&view->buffer, offset); |
| 711 | if (!type_name) { |
| 712 | goto error; |
| 713 | } |
| 714 | |
| 715 | expr = lttng_event_expr_app_specific_context_field_create( |
| 716 | provider_name, type_name); |
| 717 | break; |
| 718 | } |
| 719 | case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: |
| 720 | { |
| 721 | struct lttng_event_expr *array_field_expr; |
| 722 | const uint64_t index = uint_from_buffer( |
| 723 | &view->buffer, sizeof(uint32_t), offset); |
| 724 | |
| 725 | if (index == UINT64_C(-1)) { |
| 726 | goto error; |
| 727 | } |
| 728 | |
| 729 | /* Array field expression is the encoded after this. */ |
| 730 | array_field_expr = event_expr_from_payload(view, offset); |
| 731 | if (!array_field_expr) { |
| 732 | goto error; |
| 733 | } |
| 734 | |
| 735 | /* Move ownership of `array_field_expr` to new expression. */ |
| 736 | expr = lttng_event_expr_array_field_element_create( |
| 737 | array_field_expr, (unsigned int) index); |
| 738 | if (!expr) { |
| 739 | /* `array_field_expr` not moved: destroy it. */ |
| 740 | lttng_event_expr_destroy(array_field_expr); |
| 741 | } |
| 742 | |
| 743 | break; |
| 744 | } |
| 745 | default: |
| 746 | ERR("Invalid event expression type encoutered while deserializing event expression: type = %" PRIu64, |
| 747 | type); |
| 748 | goto error; |
| 749 | } |
| 750 | |
| 751 | goto end; |
| 752 | |
| 753 | error: |
| 754 | lttng_event_expr_destroy(expr); |
| 755 | expr = NULL; |
| 756 | |
| 757 | end: |
| 758 | return expr; |
| 759 | } |
| 760 | |
| 761 | LTTNG_HIDDEN |
| 762 | ssize_t lttng_condition_on_event_create_from_payload( |
| 763 | struct lttng_payload_view *view, |
| 764 | struct lttng_condition **_condition) |
| 765 | { |
| 766 | int optional_ret; |
| 767 | ssize_t consumed_length; |
| 768 | size_t offset = 0; |
| 769 | ssize_t event_rule_length; |
| 770 | uint32_t i, capture_descr_count; |
| 771 | optional_uint64 error_count; |
| 772 | struct lttng_condition *condition = NULL; |
| 773 | struct lttng_event_rule *event_rule = NULL; |
| 774 | |
| 775 | if (!view || !_condition) { |
| 776 | goto error; |
| 777 | } |
| 778 | |
| 779 | /* Struct lttng_event_rule. */ |
| 780 | { |
| 781 | struct lttng_payload_view event_rule_view = |
| 782 | lttng_payload_view_from_view(view, offset, -1); |
| 783 | |
| 784 | event_rule_length = lttng_event_rule_create_from_payload( |
| 785 | &event_rule_view, &event_rule); |
| 786 | } |
| 787 | |
| 788 | if (event_rule_length < 0 || !event_rule) { |
| 789 | goto error; |
| 790 | } |
| 791 | |
| 792 | offset += event_rule_length; |
| 793 | |
| 794 | /* Error count. */ |
| 795 | optional_ret = optional_uint_from_buffer(&view->buffer, |
| 796 | sizeof(error_count.value), &offset, |
| 797 | &error_count); |
| 798 | if (optional_ret) { |
| 799 | goto error; |
| 800 | } |
| 801 | |
| 802 | /* Create condition (no capture descriptors yet) at this point */ |
| 803 | condition = lttng_condition_on_event_create(event_rule); |
| 804 | if (!condition) { |
| 805 | goto error; |
| 806 | } |
| 807 | |
| 808 | if (error_count.is_set) { |
| 809 | lttng_condition_on_event_set_error_count( |
| 810 | condition, error_count.value); |
| 811 | } |
| 812 | |
| 813 | /* Capture descriptor count. */ |
| 814 | assert(event_rule_length >= 0); |
| 815 | capture_descr_count = uint_from_buffer(&view->buffer, sizeof(uint32_t), &offset); |
| 816 | if (capture_descr_count == UINT32_C(-1)) { |
| 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | /* Capture descriptors. */ |
| 821 | for (i = 0; i < capture_descr_count; i++) { |
| 822 | enum lttng_condition_status status; |
| 823 | struct lttng_event_expr *expr = event_expr_from_payload( |
| 824 | view, &offset); |
| 825 | |
| 826 | if (!expr) { |
| 827 | goto error; |
| 828 | } |
| 829 | |
| 830 | /* Move ownership of `expr` to `condition`. */ |
| 831 | status = lttng_condition_on_event_append_capture_descriptor( |
| 832 | condition, expr); |
| 833 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 834 | /* `expr` not moved: destroy it. */ |
| 835 | lttng_event_expr_destroy(expr); |
| 836 | goto error; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | consumed_length = (ssize_t) offset; |
| 841 | *_condition = condition; |
| 842 | condition = NULL; |
| 843 | goto end; |
| 844 | |
| 845 | error: |
| 846 | consumed_length = -1; |
| 847 | |
| 848 | end: |
| 849 | lttng_event_rule_put(event_rule); |
| 850 | lttng_condition_put(condition); |
| 851 | return consumed_length; |
| 852 | } |
| 853 | |
| 854 | LTTNG_HIDDEN |
| 855 | enum lttng_condition_status lttng_condition_on_event_borrow_rule_mutable( |
| 856 | const struct lttng_condition *condition, |
| 857 | struct lttng_event_rule **rule) |
| 858 | { |
| 859 | struct lttng_condition_on_event *event_rule; |
| 860 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; |
| 861 | |
| 862 | if (!condition || !IS_ON_EVENT_CONDITION(condition) || !rule) { |
| 863 | status = LTTNG_CONDITION_STATUS_INVALID; |
| 864 | goto end; |
| 865 | } |
| 866 | |
| 867 | event_rule = container_of( |
| 868 | condition, struct lttng_condition_on_event, parent); |
| 869 | if (!event_rule->rule) { |
| 870 | status = LTTNG_CONDITION_STATUS_UNSET; |
| 871 | goto end; |
| 872 | } |
| 873 | |
| 874 | *rule = event_rule->rule; |
| 875 | end: |
| 876 | return status; |
| 877 | } |
| 878 | |
| 879 | enum lttng_condition_status lttng_condition_on_event_get_rule( |
| 880 | const struct lttng_condition *condition, |
| 881 | const struct lttng_event_rule **rule) |
| 882 | { |
| 883 | struct lttng_event_rule *mutable_rule = NULL; |
| 884 | const enum lttng_condition_status status = |
| 885 | lttng_condition_on_event_borrow_rule_mutable( |
| 886 | condition, &mutable_rule); |
| 887 | |
| 888 | *rule = mutable_rule; |
| 889 | return status; |
| 890 | } |
| 891 | |
| 892 | LTTNG_HIDDEN |
| 893 | void lttng_condition_on_event_set_error_counter_index( |
| 894 | struct lttng_condition *condition, uint64_t error_counter_index) |
| 895 | { |
| 896 | struct lttng_condition_on_event *on_event_cond = |
| 897 | container_of(condition, |
| 898 | struct lttng_condition_on_event, parent); |
| 899 | |
| 900 | LTTNG_OPTIONAL_SET(&on_event_cond->error_counter_index, error_counter_index); |
| 901 | } |
| 902 | |
| 903 | LTTNG_HIDDEN |
| 904 | uint64_t lttng_condition_on_event_get_error_counter_index( |
| 905 | const struct lttng_condition *condition) |
| 906 | { |
| 907 | const struct lttng_condition_on_event *on_event_cond = |
| 908 | container_of(condition, |
| 909 | const struct lttng_condition_on_event, parent); |
| 910 | |
| 911 | return LTTNG_OPTIONAL_GET(on_event_cond->error_counter_index); |
| 912 | } |
| 913 | |
| 914 | LTTNG_HIDDEN |
| 915 | void lttng_condition_on_event_set_error_count(struct lttng_condition *condition, |
| 916 | uint64_t error_count) |
| 917 | { |
| 918 | struct lttng_condition_on_event *on_event_cond = |
| 919 | container_of(condition, |
| 920 | struct lttng_condition_on_event, parent); |
| 921 | |
| 922 | LTTNG_OPTIONAL_SET(&on_event_cond->error_count, error_count); |
| 923 | } |
| 924 | |
| 925 | uint64_t lttng_condition_on_event_get_error_count( |
| 926 | const struct lttng_condition *condition) |
| 927 | { |
| 928 | const struct lttng_condition_on_event *on_event_cond = |
| 929 | container_of(condition, |
| 930 | const struct lttng_condition_on_event, parent); |
| 931 | |
| 932 | return LTTNG_OPTIONAL_GET(on_event_cond->error_count); |
| 933 | } |
| 934 | |
| 935 | enum lttng_condition_status |
| 936 | lttng_condition_on_event_append_capture_descriptor( |
| 937 | struct lttng_condition *condition, |
| 938 | struct lttng_event_expr *expr) |
| 939 | { |
| 940 | int ret; |
| 941 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; |
| 942 | struct lttng_condition_on_event *on_event_cond = |
| 943 | container_of(condition, |
| 944 | struct lttng_condition_on_event, parent); |
| 945 | struct lttng_capture_descriptor *descriptor = NULL; |
| 946 | const struct lttng_event_rule *rule = NULL; |
| 947 | |
| 948 | /* Only accept l-values. */ |
| 949 | if (!condition || !IS_ON_EVENT_CONDITION(condition) || !expr || |
| 950 | !lttng_event_expr_is_lvalue(expr)) { |
| 951 | status = LTTNG_CONDITION_STATUS_INVALID; |
| 952 | goto end; |
| 953 | } |
| 954 | |
| 955 | status = lttng_condition_on_event_get_rule(condition, &rule); |
| 956 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 957 | goto end; |
| 958 | } |
| 959 | |
| 960 | switch(lttng_event_rule_get_type(rule)) { |
| 961 | case LTTNG_EVENT_RULE_TYPE_TRACEPOINT: |
| 962 | case LTTNG_EVENT_RULE_TYPE_SYSCALL: |
| 963 | /* Supported. */ |
| 964 | status = LTTNG_CONDITION_STATUS_OK; |
| 965 | break; |
| 966 | case LTTNG_EVENT_RULE_TYPE_UNKNOWN: |
| 967 | status = LTTNG_CONDITION_STATUS_INVALID; |
| 968 | break; |
| 969 | default: |
| 970 | status = LTTNG_CONDITION_STATUS_UNSUPPORTED; |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 975 | goto end; |
| 976 | } |
| 977 | |
| 978 | descriptor = malloc(sizeof(*descriptor)); |
| 979 | if (descriptor == NULL) { |
| 980 | status = LTTNG_CONDITION_STATUS_ERROR; |
| 981 | goto end; |
| 982 | } |
| 983 | |
| 984 | descriptor->event_expression = expr; |
| 985 | descriptor->bytecode = NULL; |
| 986 | |
| 987 | ret = lttng_dynamic_pointer_array_add_pointer( |
| 988 | &on_event_cond->capture_descriptors, descriptor); |
| 989 | if (ret) { |
| 990 | status = LTTNG_CONDITION_STATUS_ERROR; |
| 991 | goto end; |
| 992 | } |
| 993 | |
| 994 | /* Ownership is transfered to the internal capture_descriptors array */ |
| 995 | descriptor = NULL; |
| 996 | end: |
| 997 | free(descriptor); |
| 998 | return status; |
| 999 | } |
| 1000 | |
| 1001 | enum lttng_condition_status |
| 1002 | lttng_condition_on_event_get_capture_descriptor_count( |
| 1003 | const struct lttng_condition *condition, unsigned int *count) |
| 1004 | { |
| 1005 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; |
| 1006 | const struct lttng_condition_on_event *on_event_condition = |
| 1007 | container_of(condition, |
| 1008 | const struct lttng_condition_on_event, |
| 1009 | parent); |
| 1010 | |
| 1011 | if (!condition || !IS_ON_EVENT_CONDITION(condition) || !count) { |
| 1012 | status = LTTNG_CONDITION_STATUS_INVALID; |
| 1013 | goto end; |
| 1014 | } |
| 1015 | |
| 1016 | *count = lttng_dynamic_pointer_array_get_count( |
| 1017 | &on_event_condition->capture_descriptors); |
| 1018 | |
| 1019 | end: |
| 1020 | return status; |
| 1021 | } |
| 1022 | |
| 1023 | const struct lttng_event_expr * |
| 1024 | lttng_condition_on_event_get_capture_descriptor_at_index( |
| 1025 | const struct lttng_condition *condition, unsigned int index) |
| 1026 | { |
| 1027 | const struct lttng_event_expr *expr = NULL; |
| 1028 | const struct lttng_capture_descriptor *desc = NULL; |
| 1029 | |
| 1030 | desc = lttng_condition_on_event_get_internal_capture_descriptor_at_index( |
| 1031 | condition, index); |
| 1032 | if (desc == NULL) { |
| 1033 | goto end; |
| 1034 | } |
| 1035 | expr = desc->event_expression; |
| 1036 | |
| 1037 | end: |
| 1038 | return expr; |
| 1039 | } |
| 1040 | |
| 1041 | LTTNG_HIDDEN |
| 1042 | ssize_t lttng_evaluation_on_event_create_from_payload( |
| 1043 | const struct lttng_condition_on_event *condition, |
| 1044 | struct lttng_payload_view *view, |
| 1045 | struct lttng_evaluation **_evaluation) |
| 1046 | { |
| 1047 | ssize_t ret, offset = 0; |
| 1048 | const char *trigger_name; |
| 1049 | struct lttng_evaluation *evaluation = NULL; |
| 1050 | const struct lttng_evaluation_on_event_comm *header; |
| 1051 | const struct lttng_payload_view header_view = |
| 1052 | lttng_payload_view_from_view( |
| 1053 | view, 0, sizeof(*header)); |
| 1054 | uint32_t capture_payload_size; |
| 1055 | const char *capture_payload = NULL; |
| 1056 | |
| 1057 | if (!_evaluation) { |
| 1058 | ret = -1; |
| 1059 | goto error; |
| 1060 | } |
| 1061 | |
| 1062 | if (!lttng_payload_view_is_valid(&header_view)) { |
| 1063 | ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain header"); |
| 1064 | ret = -1; |
| 1065 | goto error; |
| 1066 | } |
| 1067 | |
| 1068 | header = (typeof(header)) header_view.buffer.data; |
| 1069 | |
| 1070 | /* Map the originating trigger's name. */ |
| 1071 | offset += sizeof(*header); |
| 1072 | { |
| 1073 | const struct lttng_payload_view current_view = |
| 1074 | lttng_payload_view_from_view(view, offset, |
| 1075 | header->trigger_name_length); |
| 1076 | |
| 1077 | if (!lttng_payload_view_is_valid(¤t_view)) { |
| 1078 | ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name"); |
| 1079 | ret = -1; |
| 1080 | goto error; |
| 1081 | } |
| 1082 | |
| 1083 | trigger_name = current_view.buffer.data; |
| 1084 | if (!lttng_buffer_view_contains_string(¤t_view.buffer, |
| 1085 | trigger_name, header->trigger_name_length)) { |
| 1086 | ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name"); |
| 1087 | ret = -1; |
| 1088 | goto error; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | offset += header->trigger_name_length; |
| 1093 | { |
| 1094 | const struct lttng_payload_view current_view = |
| 1095 | lttng_payload_view_from_view(view, offset, -1); |
| 1096 | |
| 1097 | if (current_view.buffer.size < sizeof(capture_payload_size)) { |
| 1098 | ret = -1; |
| 1099 | goto error; |
| 1100 | } |
| 1101 | |
| 1102 | memcpy(&capture_payload_size, current_view.buffer.data, |
| 1103 | sizeof(capture_payload_size)); |
| 1104 | } |
| 1105 | offset += sizeof(capture_payload_size); |
| 1106 | |
| 1107 | if (capture_payload_size > 0) { |
| 1108 | const struct lttng_payload_view current_view = |
| 1109 | lttng_payload_view_from_view(view, offset, -1); |
| 1110 | |
| 1111 | if (current_view.buffer.size < capture_payload_size) { |
| 1112 | ret = -1; |
| 1113 | goto error; |
| 1114 | } |
| 1115 | |
| 1116 | capture_payload = current_view.buffer.data; |
| 1117 | } |
| 1118 | |
| 1119 | evaluation = lttng_evaluation_on_event_create(condition, trigger_name, |
| 1120 | capture_payload, capture_payload_size, true); |
| 1121 | if (!evaluation) { |
| 1122 | ret = -1; |
| 1123 | goto error; |
| 1124 | } |
| 1125 | |
| 1126 | offset += capture_payload_size; |
| 1127 | *_evaluation = evaluation; |
| 1128 | evaluation = NULL; |
| 1129 | ret = offset; |
| 1130 | |
| 1131 | error: |
| 1132 | lttng_evaluation_destroy(evaluation); |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | static int lttng_evaluation_on_event_serialize( |
| 1137 | const struct lttng_evaluation *evaluation, |
| 1138 | struct lttng_payload *payload) |
| 1139 | { |
| 1140 | int ret = 0; |
| 1141 | struct lttng_evaluation_on_event *hit; |
| 1142 | struct lttng_evaluation_on_event_comm comm; |
| 1143 | uint32_t capture_payload_size; |
| 1144 | |
| 1145 | hit = container_of( |
| 1146 | evaluation, struct lttng_evaluation_on_event, parent); |
| 1147 | |
| 1148 | assert(hit->name); |
| 1149 | comm.trigger_name_length = strlen(hit->name) + 1; |
| 1150 | |
| 1151 | ret = lttng_dynamic_buffer_append( |
| 1152 | &payload->buffer, &comm, sizeof(comm)); |
| 1153 | if (ret) { |
| 1154 | goto end; |
| 1155 | } |
| 1156 | |
| 1157 | ret = lttng_dynamic_buffer_append( |
| 1158 | &payload->buffer, hit->name, comm.trigger_name_length); |
| 1159 | if (ret) { |
| 1160 | goto end; |
| 1161 | } |
| 1162 | |
| 1163 | capture_payload_size = (uint32_t) hit->capture_payload.size; |
| 1164 | ret = lttng_dynamic_buffer_append(&payload->buffer, &capture_payload_size, |
| 1165 | sizeof(capture_payload_size)); |
| 1166 | if (ret) { |
| 1167 | goto end; |
| 1168 | } |
| 1169 | |
| 1170 | ret = lttng_dynamic_buffer_append(&payload->buffer, hit->capture_payload.data, |
| 1171 | hit->capture_payload.size); |
| 1172 | if (ret) { |
| 1173 | goto end; |
| 1174 | } |
| 1175 | |
| 1176 | end: |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | static |
| 1181 | bool msgpack_str_is_equal(const struct msgpack_object *obj, const char *str) |
| 1182 | { |
| 1183 | bool is_equal = true; |
| 1184 | |
| 1185 | assert(obj->type == MSGPACK_OBJECT_STR); |
| 1186 | |
| 1187 | if (obj->via.str.size != strlen(str)) { |
| 1188 | is_equal = false; |
| 1189 | goto end; |
| 1190 | } |
| 1191 | |
| 1192 | if (strncmp(obj->via.str.ptr, str, obj->via.str.size) != 0) { |
| 1193 | is_equal = false; |
| 1194 | goto end; |
| 1195 | } |
| 1196 | |
| 1197 | end: |
| 1198 | return is_equal; |
| 1199 | } |
| 1200 | |
| 1201 | static |
| 1202 | const msgpack_object *get_msgpack_map_obj(const struct msgpack_object *map_obj, |
| 1203 | const char *name) |
| 1204 | { |
| 1205 | const msgpack_object *ret = NULL; |
| 1206 | size_t i; |
| 1207 | |
| 1208 | assert(map_obj->type == MSGPACK_OBJECT_MAP); |
| 1209 | |
| 1210 | for (i = 0; i < map_obj->via.map.size; i++) { |
| 1211 | const struct msgpack_object_kv *kv = &map_obj->via.map.ptr[i]; |
| 1212 | |
| 1213 | assert(kv->key.type == MSGPACK_OBJECT_STR); |
| 1214 | |
| 1215 | if (msgpack_str_is_equal(&kv->key, name)) { |
| 1216 | ret = &kv->val; |
| 1217 | goto end; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | end: |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | static void lttng_evaluation_on_event_destroy( |
| 1226 | struct lttng_evaluation *evaluation) |
| 1227 | { |
| 1228 | struct lttng_evaluation_on_event *hit; |
| 1229 | |
| 1230 | hit = container_of( |
| 1231 | evaluation, struct lttng_evaluation_on_event, parent); |
| 1232 | free(hit->name); |
| 1233 | lttng_dynamic_buffer_reset(&hit->capture_payload); |
| 1234 | lttng_event_field_value_destroy(hit->captured_values); |
| 1235 | free(hit); |
| 1236 | } |
| 1237 | |
| 1238 | static |
| 1239 | int event_field_value_from_obj(const msgpack_object *obj, |
| 1240 | struct lttng_event_field_value **field_val) |
| 1241 | { |
| 1242 | int ret = 0; |
| 1243 | |
| 1244 | assert(obj); |
| 1245 | assert(field_val); |
| 1246 | |
| 1247 | switch (obj->type) { |
| 1248 | case MSGPACK_OBJECT_NIL: |
| 1249 | /* Unavailable. */ |
| 1250 | *field_val = NULL; |
| 1251 | goto end; |
| 1252 | case MSGPACK_OBJECT_POSITIVE_INTEGER: |
| 1253 | *field_val = lttng_event_field_value_uint_create( |
| 1254 | obj->via.u64); |
| 1255 | break; |
| 1256 | case MSGPACK_OBJECT_NEGATIVE_INTEGER: |
| 1257 | *field_val = lttng_event_field_value_int_create( |
| 1258 | obj->via.i64); |
| 1259 | break; |
| 1260 | case MSGPACK_OBJECT_FLOAT32: |
| 1261 | case MSGPACK_OBJECT_FLOAT64: |
| 1262 | *field_val = lttng_event_field_value_real_create( |
| 1263 | obj->via.f64); |
| 1264 | break; |
| 1265 | case MSGPACK_OBJECT_STR: |
| 1266 | *field_val = lttng_event_field_value_string_create_with_size( |
| 1267 | obj->via.str.ptr, obj->via.str.size); |
| 1268 | break; |
| 1269 | case MSGPACK_OBJECT_ARRAY: |
| 1270 | { |
| 1271 | size_t i; |
| 1272 | |
| 1273 | *field_val = lttng_event_field_value_array_create(); |
| 1274 | if (!*field_val) { |
| 1275 | goto error; |
| 1276 | } |
| 1277 | |
| 1278 | for (i = 0; i < obj->via.array.size; i++) { |
| 1279 | const msgpack_object *elem_obj = &obj->via.array.ptr[i]; |
| 1280 | struct lttng_event_field_value *elem_field_val; |
| 1281 | |
| 1282 | ret = event_field_value_from_obj(elem_obj, |
| 1283 | &elem_field_val); |
| 1284 | if (ret) { |
| 1285 | goto error; |
| 1286 | } |
| 1287 | |
| 1288 | if (elem_field_val) { |
| 1289 | ret = lttng_event_field_value_array_append( |
| 1290 | *field_val, elem_field_val); |
| 1291 | } else { |
| 1292 | ret = lttng_event_field_value_array_append_unavailable( |
| 1293 | *field_val); |
| 1294 | } |
| 1295 | |
| 1296 | if (ret) { |
| 1297 | lttng_event_field_value_destroy(elem_field_val); |
| 1298 | goto error; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | break; |
| 1303 | } |
| 1304 | case MSGPACK_OBJECT_MAP: |
| 1305 | { |
| 1306 | /* |
| 1307 | * As of this version, the only valid map object is |
| 1308 | * for an enumeration value, for example: |
| 1309 | * |
| 1310 | * type: enum |
| 1311 | * value: 177 |
| 1312 | * labels: |
| 1313 | * - Labatt 50 |
| 1314 | * - Molson Dry |
| 1315 | * - Carling Black Label |
| 1316 | */ |
| 1317 | const msgpack_object *inner_obj; |
| 1318 | size_t label_i; |
| 1319 | |
| 1320 | inner_obj = get_msgpack_map_obj(obj, "type"); |
| 1321 | if (!inner_obj) { |
| 1322 | ERR("Missing `type` entry in map object"); |
| 1323 | goto error; |
| 1324 | } |
| 1325 | |
| 1326 | if (inner_obj->type != MSGPACK_OBJECT_STR) { |
| 1327 | ERR("Map object's `type` entry is not a string: type = %s", |
| 1328 | msgpack_object_type_str(inner_obj->type)); |
| 1329 | goto error; |
| 1330 | } |
| 1331 | |
| 1332 | if (!msgpack_str_is_equal(inner_obj, "enum")) { |
| 1333 | ERR("Map object's `type` entry: expecting `enum`"); |
| 1334 | goto error; |
| 1335 | } |
| 1336 | |
| 1337 | inner_obj = get_msgpack_map_obj(obj, "value"); |
| 1338 | if (!inner_obj) { |
| 1339 | ERR("Missing `value` entry in map object"); |
| 1340 | goto error; |
| 1341 | } |
| 1342 | |
| 1343 | if (inner_obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER) { |
| 1344 | *field_val = lttng_event_field_value_enum_uint_create( |
| 1345 | inner_obj->via.u64); |
| 1346 | } else if (inner_obj->type == MSGPACK_OBJECT_NEGATIVE_INTEGER) { |
| 1347 | *field_val = lttng_event_field_value_enum_int_create( |
| 1348 | inner_obj->via.i64); |
| 1349 | } else { |
| 1350 | ERR("Map object's `value` entry is not an integer: type = %s", |
| 1351 | msgpack_object_type_str(inner_obj->type)); |
| 1352 | goto error; |
| 1353 | } |
| 1354 | |
| 1355 | if (!*field_val) { |
| 1356 | goto error; |
| 1357 | } |
| 1358 | |
| 1359 | inner_obj = get_msgpack_map_obj(obj, "labels"); |
| 1360 | if (!inner_obj) { |
| 1361 | /* No labels */ |
| 1362 | goto end; |
| 1363 | } |
| 1364 | |
| 1365 | if (inner_obj->type != MSGPACK_OBJECT_ARRAY) { |
| 1366 | ERR("Map object's `labels` entry is not an array: type = %s", |
| 1367 | msgpack_object_type_str(inner_obj->type)); |
| 1368 | goto error; |
| 1369 | } |
| 1370 | |
| 1371 | for (label_i = 0; label_i < inner_obj->via.array.size; |
| 1372 | label_i++) { |
| 1373 | int iret; |
| 1374 | const msgpack_object *elem_obj = |
| 1375 | &inner_obj->via.array.ptr[label_i]; |
| 1376 | |
| 1377 | if (elem_obj->type != MSGPACK_OBJECT_STR) { |
| 1378 | ERR("Map object's `labels` entry's type is not a string: type = %s", |
| 1379 | msgpack_object_type_str(elem_obj->type)); |
| 1380 | goto error; |
| 1381 | } |
| 1382 | |
| 1383 | iret = lttng_event_field_value_enum_append_label_with_size( |
| 1384 | *field_val, elem_obj->via.str.ptr, |
| 1385 | elem_obj->via.str.size); |
| 1386 | if (iret) { |
| 1387 | goto error; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | break; |
| 1392 | } |
| 1393 | default: |
| 1394 | ERR("Unexpected object type: type = %s", |
| 1395 | msgpack_object_type_str(obj->type)); |
| 1396 | goto error; |
| 1397 | } |
| 1398 | |
| 1399 | if (!*field_val) { |
| 1400 | goto error; |
| 1401 | } |
| 1402 | |
| 1403 | goto end; |
| 1404 | |
| 1405 | error: |
| 1406 | lttng_event_field_value_destroy(*field_val); |
| 1407 | *field_val = NULL; |
| 1408 | ret = -1; |
| 1409 | |
| 1410 | end: |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
| 1414 | static |
| 1415 | struct lttng_event_field_value *event_field_value_from_capture_payload( |
| 1416 | const struct lttng_condition_on_event *condition, |
| 1417 | const char *capture_payload, size_t capture_payload_size) |
| 1418 | { |
| 1419 | struct lttng_event_field_value *ret = NULL; |
| 1420 | msgpack_unpacked unpacked; |
| 1421 | msgpack_unpack_return unpack_return; |
| 1422 | const msgpack_object *root_obj; |
| 1423 | const msgpack_object_array *root_array_obj; |
| 1424 | size_t i; |
| 1425 | size_t count; |
| 1426 | |
| 1427 | assert(condition); |
| 1428 | assert(capture_payload); |
| 1429 | |
| 1430 | /* Initialize value. */ |
| 1431 | msgpack_unpacked_init(&unpacked); |
| 1432 | |
| 1433 | /* Decode. */ |
| 1434 | unpack_return = msgpack_unpack_next(&unpacked, capture_payload, |
| 1435 | capture_payload_size, NULL); |
| 1436 | if (unpack_return != MSGPACK_UNPACK_SUCCESS) { |
| 1437 | ERR("msgpack_unpack_next() failed to decode the " |
| 1438 | "MessagePack-encoded capture payload: " |
| 1439 | "size = %zu, ret = %d", |
| 1440 | capture_payload_size, unpack_return); |
| 1441 | goto error; |
| 1442 | } |
| 1443 | |
| 1444 | /* Get root array. */ |
| 1445 | root_obj = &unpacked.data; |
| 1446 | |
| 1447 | if (root_obj->type != MSGPACK_OBJECT_ARRAY) { |
| 1448 | ERR("Expecting an array as the root object: type = %s", |
| 1449 | msgpack_object_type_str(root_obj->type)); |
| 1450 | goto error; |
| 1451 | } |
| 1452 | |
| 1453 | root_array_obj = &root_obj->via.array; |
| 1454 | |
| 1455 | /* Create an empty root array event field value. */ |
| 1456 | ret = lttng_event_field_value_array_create(); |
| 1457 | if (!ret) { |
| 1458 | goto error; |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * For each capture descriptor in the condition object: |
| 1463 | * |
| 1464 | * 1. Get its corresponding captured field value MessagePack |
| 1465 | * object. |
| 1466 | * |
| 1467 | * 2. Create a corresponding event field value. |
| 1468 | * |
| 1469 | * 3. Append it to `ret` (the root array event field value). |
| 1470 | */ |
| 1471 | count = lttng_dynamic_pointer_array_get_count( |
| 1472 | &condition->capture_descriptors); |
| 1473 | assert(count > 0); |
| 1474 | |
| 1475 | for (i = 0; i < count; i++) { |
| 1476 | const struct lttng_capture_descriptor *capture_descriptor = |
| 1477 | lttng_condition_on_event_get_internal_capture_descriptor_at_index( |
| 1478 | &condition->parent, i); |
| 1479 | const msgpack_object *elem_obj; |
| 1480 | struct lttng_event_field_value *elem_field_val; |
| 1481 | int iret; |
| 1482 | |
| 1483 | assert(capture_descriptor); |
| 1484 | |
| 1485 | elem_obj = &root_array_obj->ptr[i]; |
| 1486 | iret = event_field_value_from_obj(elem_obj, |
| 1487 | &elem_field_val); |
| 1488 | if (iret) { |
| 1489 | goto error; |
| 1490 | } |
| 1491 | |
| 1492 | if (elem_field_val) { |
| 1493 | iret = lttng_event_field_value_array_append(ret, |
| 1494 | elem_field_val); |
| 1495 | } else { |
| 1496 | iret = lttng_event_field_value_array_append_unavailable( |
| 1497 | ret); |
| 1498 | } |
| 1499 | |
| 1500 | if (iret) { |
| 1501 | lttng_event_field_value_destroy(elem_field_val); |
| 1502 | goto error; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | goto end; |
| 1507 | |
| 1508 | error: |
| 1509 | lttng_event_field_value_destroy(ret); |
| 1510 | ret = NULL; |
| 1511 | |
| 1512 | end: |
| 1513 | msgpack_unpacked_destroy(&unpacked); |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | LTTNG_HIDDEN |
| 1518 | struct lttng_evaluation *lttng_evaluation_on_event_create( |
| 1519 | const struct lttng_condition_on_event *condition, |
| 1520 | const char *trigger_name, |
| 1521 | const char *capture_payload, size_t capture_payload_size, |
| 1522 | bool decode_capture_payload) |
| 1523 | { |
| 1524 | struct lttng_evaluation_on_event *hit; |
| 1525 | struct lttng_evaluation *evaluation = NULL; |
| 1526 | |
| 1527 | hit = zmalloc(sizeof(struct lttng_evaluation_on_event)); |
| 1528 | if (!hit) { |
| 1529 | goto error; |
| 1530 | } |
| 1531 | |
| 1532 | hit->name = strdup(trigger_name); |
| 1533 | if (!hit->name) { |
| 1534 | goto error; |
| 1535 | } |
| 1536 | |
| 1537 | lttng_dynamic_buffer_init(&hit->capture_payload); |
| 1538 | |
| 1539 | if (capture_payload) { |
| 1540 | const int ret = lttng_dynamic_buffer_append( |
| 1541 | &hit->capture_payload, capture_payload, |
| 1542 | capture_payload_size); |
| 1543 | if (ret) { |
| 1544 | ERR("Failed to initialize capture payload of event rule evaluation"); |
| 1545 | goto error; |
| 1546 | } |
| 1547 | |
| 1548 | if (decode_capture_payload) { |
| 1549 | hit->captured_values = |
| 1550 | event_field_value_from_capture_payload( |
| 1551 | condition, |
| 1552 | capture_payload, |
| 1553 | capture_payload_size); |
| 1554 | if (!hit->captured_values) { |
| 1555 | ERR("Failed to decode the capture payload: size = %zu", |
| 1556 | capture_payload_size); |
| 1557 | goto error; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | hit->parent.type = LTTNG_CONDITION_TYPE_ON_EVENT; |
| 1563 | hit->parent.serialize = lttng_evaluation_on_event_serialize; |
| 1564 | hit->parent.destroy = lttng_evaluation_on_event_destroy; |
| 1565 | |
| 1566 | evaluation = &hit->parent; |
| 1567 | hit = NULL; |
| 1568 | |
| 1569 | error: |
| 1570 | if (hit) { |
| 1571 | lttng_evaluation_on_event_destroy(&hit->parent); |
| 1572 | } |
| 1573 | |
| 1574 | return evaluation; |
| 1575 | } |
| 1576 | |
| 1577 | enum lttng_evaluation_on_event_status |
| 1578 | lttng_evaluation_on_event_get_captured_values( |
| 1579 | const struct lttng_evaluation *evaluation, |
| 1580 | const struct lttng_event_field_value **field_val) |
| 1581 | { |
| 1582 | struct lttng_evaluation_on_event *hit; |
| 1583 | enum lttng_evaluation_on_event_status status = |
| 1584 | LTTNG_EVALUATION_ON_EVENT_STATUS_OK; |
| 1585 | |
| 1586 | if (!evaluation || !is_on_event_evaluation(evaluation) || |
| 1587 | !field_val) { |
| 1588 | status = LTTNG_EVALUATION_ON_EVENT_STATUS_INVALID; |
| 1589 | goto end; |
| 1590 | } |
| 1591 | |
| 1592 | hit = container_of(evaluation, struct lttng_evaluation_on_event, |
| 1593 | parent); |
| 1594 | if (!hit->captured_values) { |
| 1595 | status = LTTNG_EVALUATION_ON_EVENT_STATUS_NONE; |
| 1596 | goto end; |
| 1597 | } |
| 1598 | |
| 1599 | *field_val = hit->captured_values; |
| 1600 | |
| 1601 | end: |
| 1602 | return status; |
| 1603 | } |
| 1604 | |
| 1605 | enum lttng_evaluation_status lttng_evaluation_on_event_get_trigger_name( |
| 1606 | const struct lttng_evaluation *evaluation, const char **name) |
| 1607 | { |
| 1608 | struct lttng_evaluation_on_event *hit; |
| 1609 | enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK; |
| 1610 | |
| 1611 | if (!evaluation || !is_on_event_evaluation(evaluation) || !name) { |
| 1612 | status = LTTNG_EVALUATION_STATUS_INVALID; |
| 1613 | goto end; |
| 1614 | } |
| 1615 | |
| 1616 | hit = container_of( |
| 1617 | evaluation, struct lttng_evaluation_on_event, parent); |
| 1618 | *name = hit->name; |
| 1619 | end: |
| 1620 | return status; |
| 1621 | } |
| 1622 | |
| 1623 | LTTNG_HIDDEN |
| 1624 | enum lttng_error_code |
| 1625 | lttng_condition_on_event_generate_capture_descriptor_bytecode( |
| 1626 | struct lttng_condition *condition) |
| 1627 | { |
| 1628 | enum lttng_error_code ret; |
| 1629 | enum lttng_condition_status status; |
| 1630 | unsigned int capture_count, i; |
| 1631 | |
| 1632 | if (!condition || !IS_ON_EVENT_CONDITION(condition)) { |
| 1633 | ret = LTTNG_ERR_FATAL; |
| 1634 | goto end; |
| 1635 | } |
| 1636 | |
| 1637 | status = lttng_condition_on_event_get_capture_descriptor_count( |
| 1638 | condition, &capture_count); |
| 1639 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 1640 | ret = LTTNG_ERR_FATAL; |
| 1641 | goto end; |
| 1642 | } |
| 1643 | |
| 1644 | for (i = 0; i < capture_count; i++) { |
| 1645 | struct lttng_capture_descriptor *local_capture_desc = |
| 1646 | lttng_condition_on_event_get_internal_capture_descriptor_at_index( |
| 1647 | condition, i); |
| 1648 | |
| 1649 | if (local_capture_desc == NULL) { |
| 1650 | ret = LTTNG_ERR_FATAL; |
| 1651 | goto end; |
| 1652 | } |
| 1653 | |
| 1654 | /* Generate the bytecode. */ |
| 1655 | status = lttng_event_expr_to_bytecode( |
| 1656 | local_capture_desc->event_expression, |
| 1657 | &local_capture_desc->bytecode); |
| 1658 | if (status < 0 || local_capture_desc->bytecode == NULL) { |
| 1659 | ret = LTTNG_ERR_INVALID_CAPTURE_EXPRESSION; |
| 1660 | goto end; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /* Everything went better than expected */ |
| 1665 | ret = LTTNG_OK; |
| 1666 | |
| 1667 | end: |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | LTTNG_HIDDEN |
| 1672 | const struct lttng_bytecode * |
| 1673 | lttng_condition_on_event_get_capture_bytecode_at_index( |
| 1674 | const struct lttng_condition *condition, unsigned int index) |
| 1675 | { |
| 1676 | const struct lttng_condition_on_event *on_event_cond = |
| 1677 | container_of(condition, |
| 1678 | const struct lttng_condition_on_event, |
| 1679 | parent); |
| 1680 | struct lttng_capture_descriptor *desc = NULL; |
| 1681 | struct lttng_bytecode *bytecode = NULL; |
| 1682 | unsigned int count; |
| 1683 | enum lttng_condition_status status; |
| 1684 | |
| 1685 | if (!condition || !IS_ON_EVENT_CONDITION(condition)) { |
| 1686 | goto end; |
| 1687 | } |
| 1688 | |
| 1689 | status = lttng_condition_on_event_get_capture_descriptor_count( |
| 1690 | condition, &count); |
| 1691 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 1692 | goto end; |
| 1693 | } |
| 1694 | |
| 1695 | if (index >= count) { |
| 1696 | goto end; |
| 1697 | } |
| 1698 | |
| 1699 | desc = lttng_dynamic_pointer_array_get_pointer( |
| 1700 | &on_event_cond->capture_descriptors, index); |
| 1701 | if (desc == NULL) { |
| 1702 | goto end; |
| 1703 | } |
| 1704 | |
| 1705 | bytecode = desc->bytecode; |
| 1706 | end: |
| 1707 | return bytecode; |
| 1708 | } |