| 1 | /* |
| 2 | * event-expr.c |
| 3 | * |
| 4 | * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: LGPL-2.1-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <common/bytecode/bytecode.hpp> |
| 12 | #include <common/error.hpp> |
| 13 | #include <common/macros.hpp> |
| 14 | #include <common/mi-lttng.hpp> |
| 15 | |
| 16 | #include <lttng/event-expr-internal.hpp> |
| 17 | #include <lttng/event-expr.h> |
| 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | enum lttng_event_expr_type lttng_event_expr_get_type(const struct lttng_event_expr *expr) |
| 23 | { |
| 24 | enum lttng_event_expr_type type; |
| 25 | |
| 26 | if (!expr) { |
| 27 | type = LTTNG_EVENT_EXPR_TYPE_INVALID; |
| 28 | goto end; |
| 29 | } |
| 30 | |
| 31 | type = expr->type; |
| 32 | |
| 33 | end: |
| 34 | return type; |
| 35 | } |
| 36 | |
| 37 | static struct lttng_event_expr *create_empty_expr(enum lttng_event_expr_type type, size_t size) |
| 38 | { |
| 39 | struct lttng_event_expr *expr; |
| 40 | |
| 41 | expr = zmalloc<lttng_event_expr>(size); |
| 42 | if (!expr) { |
| 43 | goto end; |
| 44 | } |
| 45 | |
| 46 | expr->type = type; |
| 47 | |
| 48 | end: |
| 49 | return expr; |
| 50 | } |
| 51 | |
| 52 | static struct lttng_event_expr_field *create_field_event_expr(enum lttng_event_expr_type type, |
| 53 | const char *name) |
| 54 | { |
| 55 | struct lttng_event_expr_field *expr = lttng::utils::container_of( |
| 56 | create_empty_expr(type, sizeof(*expr)), <tng_event_expr_field::parent); |
| 57 | |
| 58 | if (!expr) { |
| 59 | goto error; |
| 60 | } |
| 61 | |
| 62 | LTTNG_ASSERT(name); |
| 63 | expr->name = strdup(name); |
| 64 | if (!expr->name) { |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | goto end; |
| 69 | |
| 70 | error: |
| 71 | if (expr) { |
| 72 | lttng_event_expr_destroy(&expr->parent); |
| 73 | } |
| 74 | expr = nullptr; |
| 75 | |
| 76 | end: |
| 77 | return expr; |
| 78 | } |
| 79 | |
| 80 | struct lttng_event_expr *lttng_event_expr_event_payload_field_create(const char *field_name) |
| 81 | { |
| 82 | struct lttng_event_expr *expr = nullptr; |
| 83 | |
| 84 | if (!field_name) { |
| 85 | goto end; |
| 86 | } |
| 87 | |
| 88 | expr = &create_field_event_expr(LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD, field_name) |
| 89 | ->parent; |
| 90 | |
| 91 | end: |
| 92 | return expr; |
| 93 | } |
| 94 | |
| 95 | struct lttng_event_expr *lttng_event_expr_channel_context_field_create(const char *field_name) |
| 96 | { |
| 97 | struct lttng_event_expr *expr = nullptr; |
| 98 | |
| 99 | if (!field_name) { |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | expr = &create_field_event_expr(LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD, field_name) |
| 104 | ->parent; |
| 105 | |
| 106 | end: |
| 107 | return expr; |
| 108 | } |
| 109 | |
| 110 | struct lttng_event_expr * |
| 111 | lttng_event_expr_app_specific_context_field_create(const char *provider_name, const char *type_name) |
| 112 | { |
| 113 | struct lttng_event_expr_app_specific_context_field *expr = nullptr; |
| 114 | struct lttng_event_expr *ret_parent_expr; |
| 115 | |
| 116 | if (!type_name || !provider_name) { |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | expr = lttng::utils::container_of( |
| 121 | create_empty_expr(LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD, sizeof(*expr)), |
| 122 | <tng_event_expr_app_specific_context_field::parent); |
| 123 | if (!expr) { |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | expr->provider_name = strdup(provider_name); |
| 128 | if (!expr->provider_name) { |
| 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | expr->type_name = strdup(type_name); |
| 133 | if (!expr->type_name) { |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | ret_parent_expr = &expr->parent; |
| 138 | goto end; |
| 139 | |
| 140 | error: |
| 141 | if (expr) { |
| 142 | lttng_event_expr_destroy(&expr->parent); |
| 143 | } |
| 144 | ret_parent_expr = nullptr; |
| 145 | |
| 146 | end: |
| 147 | return ret_parent_expr; |
| 148 | } |
| 149 | |
| 150 | struct lttng_event_expr * |
| 151 | lttng_event_expr_array_field_element_create(struct lttng_event_expr *array_field_expr, |
| 152 | unsigned int index) |
| 153 | { |
| 154 | struct lttng_event_expr_array_field_element *expr = nullptr; |
| 155 | struct lttng_event_expr *ret_parent_expr; |
| 156 | |
| 157 | /* The parent array field expression must be an l-value */ |
| 158 | if (!array_field_expr || !lttng_event_expr_is_lvalue(array_field_expr)) { |
| 159 | goto error; |
| 160 | } |
| 161 | |
| 162 | expr = lttng::utils::container_of( |
| 163 | create_empty_expr(LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT, sizeof(*expr)), |
| 164 | <tng_event_expr_array_field_element::parent); |
| 165 | if (!expr) { |
| 166 | goto error; |
| 167 | } |
| 168 | |
| 169 | expr->array_field_expr = array_field_expr; |
| 170 | expr->index = index; |
| 171 | ret_parent_expr = &expr->parent; |
| 172 | goto end; |
| 173 | |
| 174 | error: |
| 175 | ret_parent_expr = nullptr; |
| 176 | |
| 177 | end: |
| 178 | return ret_parent_expr; |
| 179 | } |
| 180 | |
| 181 | const char *lttng_event_expr_event_payload_field_get_name(const struct lttng_event_expr *expr) |
| 182 | { |
| 183 | const char *ret = nullptr; |
| 184 | |
| 185 | if (!expr || expr->type != LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD) { |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | ret = lttng::utils::container_of(expr, <tng_event_expr_field::parent)->name; |
| 190 | |
| 191 | end: |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | const char *lttng_event_expr_channel_context_field_get_name(const struct lttng_event_expr *expr) |
| 196 | { |
| 197 | const char *ret = nullptr; |
| 198 | |
| 199 | if (!expr || expr->type != LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD) { |
| 200 | goto end; |
| 201 | } |
| 202 | |
| 203 | ret = lttng::utils::container_of(expr, <tng_event_expr_field::parent)->name; |
| 204 | |
| 205 | end: |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | const char * |
| 210 | lttng_event_expr_app_specific_context_field_get_provider_name(const struct lttng_event_expr *expr) |
| 211 | { |
| 212 | const char *ret = nullptr; |
| 213 | |
| 214 | if (!expr || expr->type != LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD) { |
| 215 | goto end; |
| 216 | } |
| 217 | |
| 218 | ret = lttng::utils::container_of(expr, <tng_event_expr_app_specific_context_field::parent) |
| 219 | ->provider_name; |
| 220 | |
| 221 | end: |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | const char * |
| 226 | lttng_event_expr_app_specific_context_field_get_type_name(const struct lttng_event_expr *expr) |
| 227 | { |
| 228 | const char *ret = nullptr; |
| 229 | |
| 230 | if (!expr || expr->type != LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD) { |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | ret = lttng::utils::container_of(expr, <tng_event_expr_app_specific_context_field::parent) |
| 235 | ->type_name; |
| 236 | |
| 237 | end: |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | const struct lttng_event_expr * |
| 242 | lttng_event_expr_array_field_element_get_parent_expr(const struct lttng_event_expr *expr) |
| 243 | { |
| 244 | const struct lttng_event_expr *ret = nullptr; |
| 245 | |
| 246 | if (!expr || expr->type != LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT) { |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 250 | ret = lttng::utils::container_of(expr, <tng_event_expr_array_field_element::parent) |
| 251 | ->array_field_expr; |
| 252 | |
| 253 | end: |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | enum lttng_event_expr_status |
| 258 | lttng_event_expr_array_field_element_get_index(const struct lttng_event_expr *expr, |
| 259 | unsigned int *index) |
| 260 | { |
| 261 | enum lttng_event_expr_status ret = LTTNG_EVENT_EXPR_STATUS_OK; |
| 262 | |
| 263 | if (!expr || expr->type != LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT || !index) { |
| 264 | ret = LTTNG_EVENT_EXPR_STATUS_INVALID; |
| 265 | goto end; |
| 266 | } |
| 267 | |
| 268 | *index = lttng::utils::container_of(expr, <tng_event_expr_array_field_element::parent) |
| 269 | ->index; |
| 270 | |
| 271 | end: |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | bool lttng_event_expr_is_equal(const struct lttng_event_expr *expr_a, |
| 276 | const struct lttng_event_expr *expr_b) |
| 277 | { |
| 278 | bool is_equal = true; |
| 279 | |
| 280 | if (!expr_a && !expr_b) { |
| 281 | /* Both `NULL`: equal */ |
| 282 | goto end; |
| 283 | } |
| 284 | |
| 285 | if (!expr_a || !expr_b) { |
| 286 | /* Only one `NULL`: not equal */ |
| 287 | goto not_equal; |
| 288 | } |
| 289 | |
| 290 | if (expr_a->type != expr_b->type) { |
| 291 | /* Different types: not equal */ |
| 292 | goto not_equal; |
| 293 | } |
| 294 | |
| 295 | switch (expr_a->type) { |
| 296 | case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: |
| 297 | case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: |
| 298 | { |
| 299 | const struct lttng_event_expr_field *field_expr_a = |
| 300 | lttng::utils::container_of(expr_a, <tng_event_expr_field::parent); |
| 301 | const struct lttng_event_expr_field *field_expr_b = |
| 302 | lttng::utils::container_of(expr_b, <tng_event_expr_field::parent); |
| 303 | |
| 304 | if (strcmp(field_expr_a->name, field_expr_b->name) != 0) { |
| 305 | goto not_equal; |
| 306 | } |
| 307 | |
| 308 | break; |
| 309 | } |
| 310 | case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: |
| 311 | { |
| 312 | const struct lttng_event_expr_app_specific_context_field *field_expr_a = |
| 313 | lttng::utils::container_of( |
| 314 | expr_a, <tng_event_expr_app_specific_context_field::parent); |
| 315 | const struct lttng_event_expr_app_specific_context_field *field_expr_b = |
| 316 | lttng::utils::container_of( |
| 317 | expr_b, <tng_event_expr_app_specific_context_field::parent); |
| 318 | |
| 319 | if (strcmp(field_expr_a->provider_name, field_expr_b->provider_name) != 0) { |
| 320 | goto not_equal; |
| 321 | } |
| 322 | |
| 323 | if (strcmp(field_expr_a->type_name, field_expr_b->type_name) != 0) { |
| 324 | goto not_equal; |
| 325 | } |
| 326 | |
| 327 | break; |
| 328 | } |
| 329 | case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: |
| 330 | { |
| 331 | const struct lttng_event_expr_array_field_element *elem_expr_a = |
| 332 | lttng::utils::container_of(expr_a, |
| 333 | <tng_event_expr_array_field_element::parent); |
| 334 | const struct lttng_event_expr_array_field_element *elem_expr_b = |
| 335 | lttng::utils::container_of(expr_b, |
| 336 | <tng_event_expr_array_field_element::parent); |
| 337 | |
| 338 | if (!lttng_event_expr_is_equal(elem_expr_a->array_field_expr, |
| 339 | elem_expr_b->array_field_expr)) { |
| 340 | goto not_equal; |
| 341 | } |
| 342 | |
| 343 | if (elem_expr_a->index != elem_expr_b->index) { |
| 344 | goto not_equal; |
| 345 | } |
| 346 | |
| 347 | break; |
| 348 | } |
| 349 | default: |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | goto end; |
| 354 | |
| 355 | not_equal: |
| 356 | is_equal = false; |
| 357 | |
| 358 | end: |
| 359 | return is_equal; |
| 360 | } |
| 361 | |
| 362 | void lttng_event_expr_destroy(struct lttng_event_expr *expr) |
| 363 | { |
| 364 | if (!expr) { |
| 365 | goto end; |
| 366 | } |
| 367 | |
| 368 | switch (expr->type) { |
| 369 | case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: |
| 370 | case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: |
| 371 | { |
| 372 | struct lttng_event_expr_field *field_expr = |
| 373 | lttng::utils::container_of(expr, <tng_event_expr_field::parent); |
| 374 | |
| 375 | free(field_expr->name); |
| 376 | break; |
| 377 | } |
| 378 | case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: |
| 379 | { |
| 380 | struct lttng_event_expr_app_specific_context_field *field_expr = |
| 381 | lttng::utils::container_of( |
| 382 | expr, <tng_event_expr_app_specific_context_field::parent); |
| 383 | |
| 384 | free(field_expr->provider_name); |
| 385 | free(field_expr->type_name); |
| 386 | break; |
| 387 | } |
| 388 | case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: |
| 389 | { |
| 390 | struct lttng_event_expr_array_field_element *elem_expr = lttng::utils::container_of( |
| 391 | expr, <tng_event_expr_array_field_element::parent); |
| 392 | |
| 393 | lttng_event_expr_destroy(elem_expr->array_field_expr); |
| 394 | break; |
| 395 | } |
| 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | free(expr); |
| 401 | |
| 402 | end: |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | static int event_expr_to_bytecode_recursive(const struct lttng_event_expr *expr, |
| 407 | struct lttng_bytecode_alloc **bytecode, |
| 408 | struct lttng_bytecode_alloc **bytecode_reloc) |
| 409 | { |
| 410 | int status; |
| 411 | enum lttng_event_expr_status event_expr_status; |
| 412 | |
| 413 | switch (lttng_event_expr_get_type(expr)) { |
| 414 | case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: |
| 415 | { |
| 416 | const char *name; |
| 417 | |
| 418 | status = bytecode_push_get_payload_root(bytecode); |
| 419 | if (status) { |
| 420 | ERR("Failed to get payload root from bytecode"); |
| 421 | goto end; |
| 422 | } |
| 423 | |
| 424 | name = lttng_event_expr_event_payload_field_get_name(expr); |
| 425 | if (!name) { |
| 426 | ERR("Failed to get payload field name from event expression"); |
| 427 | status = -1; |
| 428 | goto end; |
| 429 | } |
| 430 | |
| 431 | status = bytecode_push_get_symbol(bytecode, bytecode_reloc, name); |
| 432 | if (status) { |
| 433 | ERR("Failed to push 'get symbol %s' in bytecode", name); |
| 434 | goto end; |
| 435 | } |
| 436 | |
| 437 | break; |
| 438 | } |
| 439 | case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: |
| 440 | { |
| 441 | const char *name; |
| 442 | |
| 443 | status = bytecode_push_get_context_root(bytecode); |
| 444 | if (status) { |
| 445 | ERR("Failed to get context root from bytecode"); |
| 446 | goto end; |
| 447 | } |
| 448 | |
| 449 | name = lttng_event_expr_channel_context_field_get_name(expr); |
| 450 | if (!name) { |
| 451 | ERR("Failed to get channel context field name from event expression"); |
| 452 | status = -1; |
| 453 | goto end; |
| 454 | } |
| 455 | |
| 456 | status = bytecode_push_get_symbol(bytecode, bytecode_reloc, name); |
| 457 | if (status) { |
| 458 | ERR("Failed to push 'get symbol %s' in bytecode", name); |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | break; |
| 463 | } |
| 464 | case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: |
| 465 | { |
| 466 | int ret; |
| 467 | char *name = nullptr; |
| 468 | const char *provider_name, *type_name; |
| 469 | |
| 470 | status = bytecode_push_get_app_context_root(bytecode); |
| 471 | if (status) { |
| 472 | ERR("Failed to get application context root from bytecode"); |
| 473 | goto end; |
| 474 | } |
| 475 | |
| 476 | provider_name = lttng_event_expr_app_specific_context_field_get_provider_name(expr); |
| 477 | if (!provider_name) { |
| 478 | ERR("Failed to get application context provider name from event expression"); |
| 479 | status = -1; |
| 480 | goto end; |
| 481 | } |
| 482 | |
| 483 | type_name = lttng_event_expr_app_specific_context_field_get_type_name(expr); |
| 484 | if (!type_name) { |
| 485 | ERR("Failed to get application context type name from event expression"); |
| 486 | status = -1; |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Reconstitute the app context field name from its two parts. |
| 492 | */ |
| 493 | ret = asprintf(&name, "%s:%s", provider_name, type_name); |
| 494 | if (ret < 0) { |
| 495 | PERROR("Failed to format application specific context: provider_name = '%s', type_name = '%s'", |
| 496 | provider_name, |
| 497 | type_name); |
| 498 | status = -1; |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | status = bytecode_push_get_symbol(bytecode, bytecode_reloc, name); |
| 503 | free(name); |
| 504 | if (status) { |
| 505 | ERR("Failed to push 'get symbol %s:%s' in bytecode", |
| 506 | provider_name, |
| 507 | type_name); |
| 508 | goto end; |
| 509 | } |
| 510 | |
| 511 | break; |
| 512 | } |
| 513 | case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: |
| 514 | { |
| 515 | unsigned int index; |
| 516 | const struct lttng_event_expr *parent; |
| 517 | |
| 518 | parent = lttng_event_expr_array_field_element_get_parent_expr(expr); |
| 519 | if (!parent) { |
| 520 | ERR("Failed to get parent expression from array event expression"); |
| 521 | status = -1; |
| 522 | goto end; |
| 523 | } |
| 524 | |
| 525 | status = event_expr_to_bytecode_recursive(parent, bytecode, bytecode_reloc); |
| 526 | if (status) { |
| 527 | goto end; |
| 528 | } |
| 529 | |
| 530 | event_expr_status = lttng_event_expr_array_field_element_get_index(expr, &index); |
| 531 | if (event_expr_status != LTTNG_EVENT_EXPR_STATUS_OK) { |
| 532 | ERR("Failed to get array field element index from event expression"); |
| 533 | status = -1; |
| 534 | goto end; |
| 535 | } |
| 536 | |
| 537 | status = bytecode_push_get_index_u64(bytecode, index); |
| 538 | if (status) { |
| 539 | ERR("Failed to push 'get index %u' in bytecode", index); |
| 540 | goto end; |
| 541 | } |
| 542 | |
| 543 | break; |
| 544 | } |
| 545 | default: |
| 546 | abort(); |
| 547 | } |
| 548 | |
| 549 | status = 0; |
| 550 | end: |
| 551 | return status; |
| 552 | } |
| 553 | |
| 554 | int lttng_event_expr_to_bytecode(const struct lttng_event_expr *expr, |
| 555 | struct lttng_bytecode **bytecode_out) |
| 556 | { |
| 557 | int status; |
| 558 | struct return_op ret_insn; |
| 559 | struct lttng_bytecode_alloc *bytecode = nullptr; |
| 560 | struct lttng_bytecode_alloc *bytecode_reloc = nullptr; |
| 561 | |
| 562 | status = bytecode_init(&bytecode); |
| 563 | if (status) { |
| 564 | ERR("Failed to initialize bytecode"); |
| 565 | goto end; |
| 566 | } |
| 567 | |
| 568 | status = bytecode_init(&bytecode_reloc); |
| 569 | if (status) { |
| 570 | ERR("Failed to initialize relocation bytecode"); |
| 571 | goto end; |
| 572 | } |
| 573 | |
| 574 | status = event_expr_to_bytecode_recursive(expr, &bytecode, &bytecode_reloc); |
| 575 | if (status) { |
| 576 | /* Errors already logged. */ |
| 577 | goto end; |
| 578 | } |
| 579 | |
| 580 | ret_insn.op = BYTECODE_OP_RETURN; |
| 581 | bytecode_push(&bytecode, &ret_insn, 1, sizeof(ret_insn)); |
| 582 | |
| 583 | /* Append symbol table to bytecode. */ |
| 584 | bytecode->b.reloc_table_offset = bytecode_get_len(&bytecode->b); |
| 585 | status = bytecode_push( |
| 586 | &bytecode, bytecode_reloc->b.data, 1, bytecode_get_len(&bytecode_reloc->b)); |
| 587 | if (status) { |
| 588 | ERR("Failed to push symbol table to bytecode"); |
| 589 | goto end; |
| 590 | } |
| 591 | |
| 592 | /* Copy the `lttng_bytecode` out of the `lttng_bytecode_alloc`. */ |
| 593 | *bytecode_out = lttng_bytecode_copy(&bytecode->b); |
| 594 | if (!*bytecode_out) { |
| 595 | status = -1; |
| 596 | goto end; |
| 597 | } |
| 598 | |
| 599 | end: |
| 600 | if (bytecode) { |
| 601 | free(bytecode); |
| 602 | } |
| 603 | |
| 604 | if (bytecode_reloc) { |
| 605 | free(bytecode_reloc); |
| 606 | } |
| 607 | |
| 608 | return status; |
| 609 | } |
| 610 | |
| 611 | static enum lttng_error_code |
| 612 | lttng_event_expr_event_payload_field_mi_serialize(const struct lttng_event_expr *expression, |
| 613 | struct mi_writer *writer) |
| 614 | { |
| 615 | int ret; |
| 616 | enum lttng_error_code ret_code; |
| 617 | const char *name = nullptr; |
| 618 | |
| 619 | LTTNG_ASSERT(expression); |
| 620 | LTTNG_ASSERT(writer); |
| 621 | LTTNG_ASSERT(expression->type == LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD); |
| 622 | |
| 623 | name = lttng_event_expr_event_payload_field_get_name(expression); |
| 624 | LTTNG_ASSERT(name); |
| 625 | |
| 626 | /* Open event expr payload field element. */ |
| 627 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_expr_payload_field); |
| 628 | if (ret) { |
| 629 | goto mi_error; |
| 630 | } |
| 631 | |
| 632 | /* Name. */ |
| 633 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, name); |
| 634 | if (ret) { |
| 635 | goto mi_error; |
| 636 | } |
| 637 | |
| 638 | /* Close event expr payload field element. */ |
| 639 | ret = mi_lttng_writer_close_element(writer); |
| 640 | if (ret) { |
| 641 | goto mi_error; |
| 642 | } |
| 643 | |
| 644 | ret_code = LTTNG_OK; |
| 645 | goto end; |
| 646 | |
| 647 | mi_error: |
| 648 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 649 | end: |
| 650 | return ret_code; |
| 651 | } |
| 652 | |
| 653 | static enum lttng_error_code |
| 654 | lttng_event_expr_channel_context_field_mi_serialize(const struct lttng_event_expr *expression, |
| 655 | struct mi_writer *writer) |
| 656 | { |
| 657 | int ret; |
| 658 | enum lttng_error_code ret_code; |
| 659 | const char *name = nullptr; |
| 660 | |
| 661 | LTTNG_ASSERT(expression); |
| 662 | LTTNG_ASSERT(writer); |
| 663 | LTTNG_ASSERT(expression->type == LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD); |
| 664 | |
| 665 | name = lttng_event_expr_channel_context_field_get_name(expression); |
| 666 | LTTNG_ASSERT(name); |
| 667 | |
| 668 | /* Open event expr channel context field element. */ |
| 669 | ret = mi_lttng_writer_open_element(writer, |
| 670 | mi_lttng_element_event_expr_channel_context_field); |
| 671 | if (ret) { |
| 672 | goto mi_error; |
| 673 | } |
| 674 | |
| 675 | /* Name. */ |
| 676 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, name); |
| 677 | if (ret) { |
| 678 | goto mi_error; |
| 679 | } |
| 680 | |
| 681 | /* Close event expr channel context field element. */ |
| 682 | ret = mi_lttng_writer_close_element(writer); |
| 683 | if (ret) { |
| 684 | goto mi_error; |
| 685 | } |
| 686 | |
| 687 | ret_code = LTTNG_OK; |
| 688 | goto end; |
| 689 | |
| 690 | mi_error: |
| 691 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 692 | end: |
| 693 | return ret_code; |
| 694 | } |
| 695 | |
| 696 | static enum lttng_error_code |
| 697 | lttng_event_expr_app_specific_context_field_mi_serialize(const struct lttng_event_expr *expression, |
| 698 | struct mi_writer *writer) |
| 699 | { |
| 700 | int ret; |
| 701 | enum lttng_error_code ret_code; |
| 702 | const char *provider_name = nullptr; |
| 703 | const char *type_name = nullptr; |
| 704 | |
| 705 | LTTNG_ASSERT(expression); |
| 706 | LTTNG_ASSERT(writer); |
| 707 | LTTNG_ASSERT(expression->type == LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD); |
| 708 | |
| 709 | provider_name = lttng_event_expr_app_specific_context_field_get_provider_name(expression); |
| 710 | LTTNG_ASSERT(provider_name); |
| 711 | |
| 712 | type_name = lttng_event_expr_app_specific_context_field_get_type_name(expression); |
| 713 | LTTNG_ASSERT(provider_name); |
| 714 | |
| 715 | /* Open event expr app specific context field element. */ |
| 716 | ret = mi_lttng_writer_open_element(writer, |
| 717 | mi_lttng_element_event_expr_app_specific_context_field); |
| 718 | if (ret) { |
| 719 | goto mi_error; |
| 720 | } |
| 721 | |
| 722 | /* Provider name. */ |
| 723 | ret = mi_lttng_writer_write_element_string( |
| 724 | writer, mi_lttng_element_event_expr_provider_name, provider_name); |
| 725 | if (ret) { |
| 726 | goto mi_error; |
| 727 | } |
| 728 | |
| 729 | /* Type name. */ |
| 730 | ret = mi_lttng_writer_write_element_string( |
| 731 | writer, mi_lttng_element_event_expr_type_name, type_name); |
| 732 | if (ret) { |
| 733 | goto mi_error; |
| 734 | } |
| 735 | |
| 736 | /* Close event expr app specific context field element. */ |
| 737 | ret = mi_lttng_writer_close_element(writer); |
| 738 | if (ret) { |
| 739 | goto mi_error; |
| 740 | } |
| 741 | |
| 742 | ret_code = LTTNG_OK; |
| 743 | goto end; |
| 744 | |
| 745 | mi_error: |
| 746 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 747 | end: |
| 748 | return ret_code; |
| 749 | } |
| 750 | |
| 751 | static enum lttng_error_code |
| 752 | lttng_event_expr_array_field_element_mi_serialize(const struct lttng_event_expr *expression, |
| 753 | struct mi_writer *writer) |
| 754 | { |
| 755 | int ret; |
| 756 | enum lttng_error_code ret_code; |
| 757 | enum lttng_event_expr_status status; |
| 758 | const struct lttng_event_expr *parent_expr = nullptr; |
| 759 | unsigned int index; |
| 760 | |
| 761 | LTTNG_ASSERT(expression); |
| 762 | LTTNG_ASSERT(writer); |
| 763 | LTTNG_ASSERT(expression->type == LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT); |
| 764 | |
| 765 | status = lttng_event_expr_array_field_element_get_index(expression, &index); |
| 766 | LTTNG_ASSERT(status == LTTNG_EVENT_EXPR_STATUS_OK); |
| 767 | |
| 768 | parent_expr = lttng_event_expr_array_field_element_get_parent_expr(expression); |
| 769 | LTTNG_ASSERT(parent_expr != nullptr); |
| 770 | |
| 771 | /* Open event expr array field element. */ |
| 772 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_expr_array_field_element); |
| 773 | if (ret) { |
| 774 | goto mi_error; |
| 775 | } |
| 776 | |
| 777 | /* Index. */ |
| 778 | ret = mi_lttng_writer_write_element_unsigned_int( |
| 779 | writer, mi_lttng_element_event_expr_index, index); |
| 780 | if (ret) { |
| 781 | goto mi_error; |
| 782 | } |
| 783 | |
| 784 | /* Parent expression. */ |
| 785 | ret_code = lttng_event_expr_mi_serialize(parent_expr, writer); |
| 786 | if (ret_code != LTTNG_OK) { |
| 787 | goto end; |
| 788 | } |
| 789 | |
| 790 | /* Close event expr array field element. */ |
| 791 | ret = mi_lttng_writer_close_element(writer); |
| 792 | if (ret) { |
| 793 | goto mi_error; |
| 794 | } |
| 795 | |
| 796 | ret_code = LTTNG_OK; |
| 797 | goto end; |
| 798 | |
| 799 | mi_error: |
| 800 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 801 | end: |
| 802 | return ret_code; |
| 803 | } |
| 804 | |
| 805 | enum lttng_error_code lttng_event_expr_mi_serialize(const struct lttng_event_expr *expression, |
| 806 | struct mi_writer *writer) |
| 807 | { |
| 808 | int ret; |
| 809 | enum lttng_error_code ret_code; |
| 810 | |
| 811 | LTTNG_ASSERT(expression); |
| 812 | LTTNG_ASSERT(writer); |
| 813 | |
| 814 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_expr); |
| 815 | if (ret) { |
| 816 | goto mi_error; |
| 817 | } |
| 818 | |
| 819 | switch (expression->type) { |
| 820 | case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: |
| 821 | ret_code = lttng_event_expr_event_payload_field_mi_serialize(expression, writer); |
| 822 | break; |
| 823 | case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: |
| 824 | ret_code = lttng_event_expr_channel_context_field_mi_serialize(expression, writer); |
| 825 | break; |
| 826 | case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: |
| 827 | ret_code = lttng_event_expr_app_specific_context_field_mi_serialize(expression, |
| 828 | writer); |
| 829 | break; |
| 830 | case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: |
| 831 | ret_code = lttng_event_expr_array_field_element_mi_serialize(expression, writer); |
| 832 | break; |
| 833 | default: |
| 834 | abort(); |
| 835 | } |
| 836 | |
| 837 | if (ret_code != LTTNG_OK) { |
| 838 | goto end; |
| 839 | } |
| 840 | |
| 841 | ret = mi_lttng_writer_close_element(writer); |
| 842 | if (ret) { |
| 843 | goto mi_error; |
| 844 | } |
| 845 | |
| 846 | ret_code = LTTNG_OK; |
| 847 | goto end; |
| 848 | |
| 849 | mi_error: |
| 850 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 851 | |
| 852 | end: |
| 853 | return ret_code; |
| 854 | } |