| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define BT_COMP_LOG_SELF_COMP (ctx->self_comp) |
| 8 | #define BT_LOG_OUTPUT_LEVEL (ctx->log_level) |
| 9 | #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRANSLATE-TRACE-IR-TO-CTF-IR" |
| 10 | #include "logging/comp-logging.h" |
| 11 | |
| 12 | #include "translate-trace-ir-to-ctf-ir.h" |
| 13 | |
| 14 | #include <babeltrace2/babeltrace.h> |
| 15 | #include "common/macros.h" |
| 16 | #include "common/common.h" |
| 17 | #include "common/assert.h" |
| 18 | #include <stdio.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <string.h> |
| 21 | #include <glib.h> |
| 22 | |
| 23 | #include "fs-sink.h" |
| 24 | #include "fs-sink-ctf-meta.h" |
| 25 | |
| 26 | struct field_path_elem { |
| 27 | uint64_t index_in_parent; |
| 28 | GString *name; |
| 29 | |
| 30 | /* Weak */ |
| 31 | const bt_field_class *ir_fc; |
| 32 | |
| 33 | /* Weak */ |
| 34 | struct fs_sink_ctf_field_class *parent_fc; |
| 35 | }; |
| 36 | |
| 37 | struct ctx { |
| 38 | bt_logging_level log_level; |
| 39 | bt_self_component *self_comp; |
| 40 | |
| 41 | /* Weak */ |
| 42 | struct fs_sink_ctf_stream_class *cur_sc; |
| 43 | |
| 44 | /* Weak */ |
| 45 | struct fs_sink_ctf_event_class *cur_ec; |
| 46 | |
| 47 | bt_field_path_scope cur_scope; |
| 48 | |
| 49 | /* |
| 50 | * Array of `struct field_path_elem` */ |
| 51 | GArray *cur_path; |
| 52 | }; |
| 53 | |
| 54 | static inline |
| 55 | struct field_path_elem *cur_path_stack_at(struct ctx *ctx, uint64_t i) |
| 56 | { |
| 57 | BT_ASSERT(i < ctx->cur_path->len); |
| 58 | return &g_array_index(ctx->cur_path, struct field_path_elem, i); |
| 59 | } |
| 60 | |
| 61 | static inline |
| 62 | struct field_path_elem *cur_path_stack_top(struct ctx *ctx) |
| 63 | { |
| 64 | BT_ASSERT(ctx->cur_path->len > 0); |
| 65 | return cur_path_stack_at(ctx, ctx->cur_path->len - 1); |
| 66 | } |
| 67 | |
| 68 | static inline |
| 69 | bool is_reserved_member_name(const char *name, const char *reserved_name) |
| 70 | { |
| 71 | bool is_reserved = false; |
| 72 | |
| 73 | if (strcmp(name, reserved_name) == 0) { |
| 74 | is_reserved = true; |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | if (name[0] == '_' && strcmp(&name[1], reserved_name) == 0) { |
| 79 | is_reserved = true; |
| 80 | goto end; |
| 81 | } |
| 82 | |
| 83 | end: |
| 84 | return is_reserved; |
| 85 | } |
| 86 | |
| 87 | static const char *reserved_tsdl_keywords[] = { |
| 88 | "align", |
| 89 | "callsite", |
| 90 | "const", |
| 91 | "char", |
| 92 | "clock", |
| 93 | "double", |
| 94 | "enum", |
| 95 | "env", |
| 96 | "event", |
| 97 | "floating_point", |
| 98 | "float", |
| 99 | "integer", |
| 100 | "int", |
| 101 | "long", |
| 102 | "short", |
| 103 | "signed", |
| 104 | "stream", |
| 105 | "string", |
| 106 | "struct", |
| 107 | "trace", |
| 108 | "typealias", |
| 109 | "typedef", |
| 110 | "unsigned", |
| 111 | "variant", |
| 112 | "void", |
| 113 | "_Bool", |
| 114 | "_Complex", |
| 115 | "_Imaginary", |
| 116 | }; |
| 117 | |
| 118 | static inline |
| 119 | bool ist_valid_identifier(const char *name) |
| 120 | { |
| 121 | const char *at; |
| 122 | uint64_t i; |
| 123 | bool ist_valid = true; |
| 124 | |
| 125 | /* Make sure the name is not a reserved keyword */ |
| 126 | for (i = 0; i < sizeof(reserved_tsdl_keywords) / sizeof(*reserved_tsdl_keywords); |
| 127 | i++) { |
| 128 | if (strcmp(name, reserved_tsdl_keywords[i]) == 0) { |
| 129 | ist_valid = false; |
| 130 | goto end; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Make sure the name is not an empty string */ |
| 135 | if (strlen(name) == 0) { |
| 136 | ist_valid = false; |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | /* Make sure the name starts with a letter or `_` */ |
| 141 | if (!isalpha((unsigned char) name[0]) && name[0] != '_') { |
| 142 | ist_valid = false; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | /* Make sure the name only contains letters, digits, and `_` */ |
| 147 | for (at = name; *at != '\0'; at++) { |
| 148 | if (!isalnum((unsigned char) *at) && *at != '_') { |
| 149 | ist_valid = false; |
| 150 | goto end; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | end: |
| 155 | return ist_valid; |
| 156 | } |
| 157 | |
| 158 | static inline |
| 159 | bool must_protect_identifier(const char *name) |
| 160 | { |
| 161 | uint64_t i; |
| 162 | bool must_protect = false; |
| 163 | |
| 164 | /* Protect a reserved keyword */ |
| 165 | for (i = 0; i < sizeof(reserved_tsdl_keywords) / sizeof(*reserved_tsdl_keywords); |
| 166 | i++) { |
| 167 | if (strcmp(name, reserved_tsdl_keywords[i]) == 0) { |
| 168 | must_protect = true; |
| 169 | goto end; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Protect an identifier which already starts with `_` */ |
| 174 | if (name[0] == '_') { |
| 175 | must_protect = true; |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | end: |
| 180 | return must_protect; |
| 181 | } |
| 182 | |
| 183 | static inline |
| 184 | int cur_path_stack_push(struct ctx *ctx, |
| 185 | uint64_t index_in_parent, const char *name, |
| 186 | bool force_protect_name, const bt_field_class *ir_fc, |
| 187 | struct fs_sink_ctf_field_class *parent_fc) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | struct field_path_elem *field_path_elem; |
| 191 | |
| 192 | g_array_set_size(ctx->cur_path, ctx->cur_path->len + 1); |
| 193 | field_path_elem = cur_path_stack_top(ctx); |
| 194 | field_path_elem->index_in_parent = index_in_parent; |
| 195 | field_path_elem->name = g_string_new(NULL); |
| 196 | |
| 197 | if (name) { |
| 198 | if (force_protect_name) { |
| 199 | g_string_assign(field_path_elem->name, "_"); |
| 200 | } |
| 201 | |
| 202 | g_string_append(field_path_elem->name, name); |
| 203 | |
| 204 | if (ctx->cur_scope == BT_FIELD_PATH_SCOPE_PACKET_CONTEXT) { |
| 205 | if (is_reserved_member_name(name, "packet_size") || |
| 206 | is_reserved_member_name(name, "content_size") || |
| 207 | is_reserved_member_name(name, "timestamp_begin") || |
| 208 | is_reserved_member_name(name, "timestamp_end") || |
| 209 | is_reserved_member_name(name, "events_discarded") || |
| 210 | is_reserved_member_name(name, "packet_seq_num")) { |
| 211 | BT_COMP_LOGE("Unsupported reserved TSDL structure field class member " |
| 212 | "or variant field class option name: name=\"%s\"", |
| 213 | name); |
| 214 | ret = -1; |
| 215 | goto end; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (!ist_valid_identifier(field_path_elem->name->str)) { |
| 220 | ret = -1; |
| 221 | BT_COMP_LOGE("Unsupported non-TSDL structure field class member " |
| 222 | "or variant field class option name: name=\"%s\"", |
| 223 | field_path_elem->name->str); |
| 224 | goto end; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | field_path_elem->ir_fc = ir_fc; |
| 229 | field_path_elem->parent_fc = parent_fc; |
| 230 | |
| 231 | end: |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static inline |
| 236 | void cur_path_stack_pop(struct ctx *ctx) |
| 237 | { |
| 238 | struct field_path_elem *field_path_elem; |
| 239 | |
| 240 | BT_ASSERT(ctx->cur_path->len > 0); |
| 241 | field_path_elem = cur_path_stack_top(ctx); |
| 242 | |
| 243 | if (field_path_elem->name) { |
| 244 | g_string_free(field_path_elem->name, TRUE); |
| 245 | field_path_elem->name = NULL; |
| 246 | } |
| 247 | |
| 248 | g_array_set_size(ctx->cur_path, ctx->cur_path->len - 1); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Creates a relative field ref (a single name) from IR field path |
| 253 | * `tgt_ir_field_path`. |
| 254 | * |
| 255 | * This function tries to locate the target field class recursively from |
| 256 | * the top to the bottom of the context's current path using only the |
| 257 | * target field class's own name. This is because many CTF reading tools |
| 258 | * do not support a relative field ref with more than one element, for |
| 259 | * example `prev_struct.len`. |
| 260 | * |
| 261 | * Returns a negative value if this resolving operation failed. |
| 262 | */ |
| 263 | static |
| 264 | int create_relative_field_ref(struct ctx *ctx, |
| 265 | const bt_field_path *tgt_ir_field_path, GString *tgt_field_ref, |
| 266 | struct fs_sink_ctf_field_class **user_tgt_fc) |
| 267 | { |
| 268 | int ret = 0; |
| 269 | struct fs_sink_ctf_field_class *tgt_fc = NULL; |
| 270 | uint64_t i; |
| 271 | int64_t si; |
| 272 | const char *tgt_fc_name = NULL; |
| 273 | struct field_path_elem *field_path_elem; |
| 274 | |
| 275 | /* Get target field class's name */ |
| 276 | switch (bt_field_path_get_root_scope(tgt_ir_field_path)) { |
| 277 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
| 278 | BT_ASSERT(ctx->cur_sc); |
| 279 | tgt_fc = ctx->cur_sc->packet_context_fc; |
| 280 | break; |
| 281 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
| 282 | BT_ASSERT(ctx->cur_sc); |
| 283 | tgt_fc = ctx->cur_sc->event_common_context_fc; |
| 284 | break; |
| 285 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 286 | BT_ASSERT(ctx->cur_ec); |
| 287 | tgt_fc = ctx->cur_ec->spec_context_fc; |
| 288 | break; |
| 289 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
| 290 | BT_ASSERT(ctx->cur_ec); |
| 291 | tgt_fc = ctx->cur_ec->payload_fc; |
| 292 | break; |
| 293 | default: |
| 294 | bt_common_abort(); |
| 295 | } |
| 296 | |
| 297 | i = 0; |
| 298 | |
| 299 | while (i < bt_field_path_get_item_count(tgt_ir_field_path)) { |
| 300 | const bt_field_path_item *fp_item = |
| 301 | bt_field_path_borrow_item_by_index_const( |
| 302 | tgt_ir_field_path, i); |
| 303 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
| 304 | |
| 305 | BT_ASSERT(tgt_fc); |
| 306 | BT_ASSERT(fp_item); |
| 307 | |
| 308 | if (bt_field_path_item_get_type(fp_item) == |
| 309 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT) { |
| 310 | /* Not supported by CTF 1.8 */ |
| 311 | ret = -1; |
| 312 | goto end; |
| 313 | } |
| 314 | |
| 315 | switch (tgt_fc->type) { |
| 316 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 317 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 318 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 319 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 320 | (void *) tgt_fc, |
| 321 | bt_field_path_item_index_get_index(fp_item)); |
| 322 | break; |
| 323 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 324 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 325 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 326 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 327 | (void *) tgt_fc, |
| 328 | bt_field_path_item_index_get_index(fp_item)); |
| 329 | break; |
| 330 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 331 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 332 | { |
| 333 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 334 | (void *) tgt_fc; |
| 335 | |
| 336 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 337 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); |
| 338 | tgt_fc = array_base_fc->elem_fc; |
| 339 | break; |
| 340 | } |
| 341 | default: |
| 342 | bt_common_abort(); |
| 343 | } |
| 344 | |
| 345 | if (named_fc) { |
| 346 | tgt_fc = named_fc->fc; |
| 347 | tgt_fc_name = named_fc->name->str; |
| 348 | i++; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | BT_ASSERT(tgt_fc); |
| 353 | BT_ASSERT(tgt_fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT); |
| 354 | BT_ASSERT(tgt_fc_name); |
| 355 | |
| 356 | /* Find target field class having this name in current context */ |
| 357 | for (si = ctx->cur_path->len - 1; si >= 0; si--) { |
| 358 | struct fs_sink_ctf_field_class *fc; |
| 359 | struct fs_sink_ctf_field_class_struct *struct_fc = NULL; |
| 360 | struct fs_sink_ctf_field_class_variant *var_fc = NULL; |
| 361 | struct fs_sink_ctf_named_field_class *named_fc; |
| 362 | uint64_t len; |
| 363 | |
| 364 | field_path_elem = cur_path_stack_at(ctx, (uint64_t) si); |
| 365 | fc = field_path_elem->parent_fc; |
| 366 | if (!fc) { |
| 367 | /* Reached stack's bottom */ |
| 368 | ret = -1; |
| 369 | goto end; |
| 370 | } |
| 371 | |
| 372 | switch (fc->type) { |
| 373 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 374 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 375 | break; |
| 376 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 377 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 378 | continue; |
| 379 | default: |
| 380 | /* Not supported by TSDL 1.8 */ |
| 381 | ret = -1; |
| 382 | goto end; |
| 383 | } |
| 384 | |
| 385 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 386 | struct_fc = (void *) fc; |
| 387 | len = struct_fc->members->len; |
| 388 | } else { |
| 389 | var_fc = (void *) fc; |
| 390 | len = var_fc->options->len; |
| 391 | } |
| 392 | |
| 393 | for (i = 0; i < len; i++) { |
| 394 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 395 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 396 | struct_fc, i); |
| 397 | } else { |
| 398 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 399 | var_fc, i); |
| 400 | } |
| 401 | |
| 402 | if (strcmp(named_fc->name->str, tgt_fc_name) == 0) { |
| 403 | if (named_fc->fc == tgt_fc) { |
| 404 | g_string_assign(tgt_field_ref, |
| 405 | tgt_fc_name); |
| 406 | |
| 407 | if (user_tgt_fc) { |
| 408 | *user_tgt_fc = tgt_fc; |
| 409 | } |
| 410 | } else { |
| 411 | /* |
| 412 | * Using only the target field |
| 413 | * class's name, we're not |
| 414 | * reaching the target field |
| 415 | * class. This is not supported |
| 416 | * by TSDL 1.8. |
| 417 | */ |
| 418 | ret = -1; |
| 419 | } |
| 420 | |
| 421 | goto end; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | end: |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Creates an absolute field ref from IR field path `tgt_ir_field_path`. |
| 432 | * |
| 433 | * Returns a negative value if this resolving operation failed. |
| 434 | */ |
| 435 | static |
| 436 | int create_absolute_field_ref(struct ctx *ctx, |
| 437 | const bt_field_path *tgt_ir_field_path, GString *tgt_field_ref, |
| 438 | struct fs_sink_ctf_field_class **user_tgt_fc) |
| 439 | { |
| 440 | int ret = 0; |
| 441 | struct fs_sink_ctf_field_class *fc = NULL; |
| 442 | uint64_t i; |
| 443 | |
| 444 | switch (bt_field_path_get_root_scope(tgt_ir_field_path)) { |
| 445 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
| 446 | BT_ASSERT(ctx->cur_sc); |
| 447 | fc = ctx->cur_sc->packet_context_fc; |
| 448 | g_string_assign(tgt_field_ref, "stream.packet.context"); |
| 449 | break; |
| 450 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
| 451 | BT_ASSERT(ctx->cur_sc); |
| 452 | fc = ctx->cur_sc->event_common_context_fc; |
| 453 | g_string_assign(tgt_field_ref, "stream.event.context"); |
| 454 | break; |
| 455 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 456 | BT_ASSERT(ctx->cur_ec); |
| 457 | fc = ctx->cur_ec->spec_context_fc; |
| 458 | g_string_assign(tgt_field_ref, "event.context"); |
| 459 | break; |
| 460 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
| 461 | BT_ASSERT(ctx->cur_ec); |
| 462 | fc = ctx->cur_ec->payload_fc; |
| 463 | g_string_assign(tgt_field_ref, "event.fields"); |
| 464 | break; |
| 465 | default: |
| 466 | bt_common_abort(); |
| 467 | } |
| 468 | |
| 469 | BT_ASSERT(fc); |
| 470 | |
| 471 | for (i = 0; i < bt_field_path_get_item_count(tgt_ir_field_path); i++) { |
| 472 | const bt_field_path_item *fp_item = |
| 473 | bt_field_path_borrow_item_by_index_const( |
| 474 | tgt_ir_field_path, i); |
| 475 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
| 476 | |
| 477 | if (bt_field_path_item_get_type(fp_item) != |
| 478 | BT_FIELD_PATH_ITEM_TYPE_INDEX) { |
| 479 | /* Not supported by TSDL 1.8 */ |
| 480 | ret = -1; |
| 481 | goto end; |
| 482 | } |
| 483 | |
| 484 | switch (fc->type) { |
| 485 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 486 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 487 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 488 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 489 | (void *) fc, |
| 490 | bt_field_path_item_index_get_index(fp_item)); |
| 491 | break; |
| 492 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 493 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 494 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 495 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 496 | (void *) fc, |
| 497 | bt_field_path_item_index_get_index(fp_item)); |
| 498 | break; |
| 499 | default: |
| 500 | bt_common_abort(); |
| 501 | } |
| 502 | |
| 503 | BT_ASSERT(named_fc); |
| 504 | g_string_append_c(tgt_field_ref, '.'); |
| 505 | g_string_append(tgt_field_ref, named_fc->name->str); |
| 506 | fc = named_fc->fc; |
| 507 | } |
| 508 | |
| 509 | if (user_tgt_fc) { |
| 510 | *user_tgt_fc = fc; |
| 511 | } |
| 512 | |
| 513 | end: |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Resolves a target field class located at `tgt_ir_field_path`, writing |
| 519 | * the resolved field ref to `tgt_field_ref` and setting |
| 520 | * `*create_before` according to whether or not the target field must be |
| 521 | * created immediately before (in which case `tgt_field_ref` is |
| 522 | * irrelevant). |
| 523 | */ |
| 524 | static |
| 525 | void resolve_field_class(struct ctx *ctx, |
| 526 | const bt_field_path *tgt_ir_field_path, |
| 527 | GString *tgt_field_ref, bool *create_before, |
| 528 | struct fs_sink_ctf_field_class **user_tgt_fc) |
| 529 | { |
| 530 | int ret; |
| 531 | bt_field_path_scope tgt_scope; |
| 532 | |
| 533 | *create_before = false; |
| 534 | |
| 535 | if (!tgt_ir_field_path) { |
| 536 | *create_before = true; |
| 537 | goto end; |
| 538 | } |
| 539 | |
| 540 | tgt_scope = bt_field_path_get_root_scope(tgt_ir_field_path); |
| 541 | |
| 542 | if (tgt_scope == ctx->cur_scope) { |
| 543 | /* |
| 544 | * Try, in this order: |
| 545 | * |
| 546 | * 1. Use a relative path, using only the target field |
| 547 | * class's name. This is what is the most commonly |
| 548 | * supported by popular CTF reading tools. |
| 549 | * |
| 550 | * 2. Use an absolute path. This could fail if there's |
| 551 | * an array field class from the current root's field |
| 552 | * class to the target field class. |
| 553 | * |
| 554 | * 3. Create the target field class before the |
| 555 | * requesting field class (fallback). |
| 556 | */ |
| 557 | ret = create_relative_field_ref(ctx, tgt_ir_field_path, |
| 558 | tgt_field_ref, user_tgt_fc); |
| 559 | if (ret) { |
| 560 | ret = create_absolute_field_ref(ctx, tgt_ir_field_path, |
| 561 | tgt_field_ref, user_tgt_fc); |
| 562 | if (ret) { |
| 563 | *create_before = true; |
| 564 | goto end; |
| 565 | } |
| 566 | } |
| 567 | } else { |
| 568 | ret = create_absolute_field_ref(ctx, tgt_ir_field_path, |
| 569 | tgt_field_ref, user_tgt_fc); |
| 570 | |
| 571 | /* It must always work in previous scopes */ |
| 572 | BT_ASSERT(ret == 0); |
| 573 | } |
| 574 | |
| 575 | end: |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | static |
| 580 | int translate_field_class(struct ctx *ctx); |
| 581 | |
| 582 | static inline |
| 583 | void append_to_parent_field_class(struct ctx *ctx, |
| 584 | struct fs_sink_ctf_field_class *fc) |
| 585 | { |
| 586 | struct fs_sink_ctf_field_class *parent_fc = |
| 587 | cur_path_stack_top(ctx)->parent_fc; |
| 588 | |
| 589 | switch (parent_fc->type) { |
| 590 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 591 | fs_sink_ctf_field_class_struct_append_member((void *) parent_fc, |
| 592 | cur_path_stack_top(ctx)->name->str, fc); |
| 593 | break; |
| 594 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
| 595 | { |
| 596 | struct fs_sink_ctf_field_class_option *opt_fc = |
| 597 | (void *) parent_fc; |
| 598 | |
| 599 | BT_ASSERT(!opt_fc->content_fc); |
| 600 | opt_fc->content_fc = fc; |
| 601 | opt_fc->base.alignment = fc->alignment; |
| 602 | break; |
| 603 | } |
| 604 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 605 | fs_sink_ctf_field_class_variant_append_option((void *) parent_fc, |
| 606 | cur_path_stack_top(ctx)->name->str, fc); |
| 607 | break; |
| 608 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 609 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 610 | { |
| 611 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 612 | (void *) parent_fc; |
| 613 | |
| 614 | BT_ASSERT(!array_base_fc->elem_fc); |
| 615 | array_base_fc->elem_fc = fc; |
| 616 | array_base_fc->base.alignment = fc->alignment; |
| 617 | break; |
| 618 | } |
| 619 | default: |
| 620 | bt_common_abort(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static inline |
| 625 | void update_parent_field_class_alignment(struct ctx *ctx, |
| 626 | unsigned int alignment) |
| 627 | { |
| 628 | struct fs_sink_ctf_field_class *parent_fc = |
| 629 | cur_path_stack_top(ctx)->parent_fc; |
| 630 | |
| 631 | switch (parent_fc->type) { |
| 632 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 633 | fs_sink_ctf_field_class_struct_align_at_least( |
| 634 | (void *) parent_fc, alignment); |
| 635 | break; |
| 636 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 637 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 638 | { |
| 639 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 640 | (void *) parent_fc; |
| 641 | |
| 642 | array_base_fc->base.alignment = alignment; |
| 643 | break; |
| 644 | } |
| 645 | default: |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | static inline |
| 651 | int translate_structure_field_class_members(struct ctx *ctx, |
| 652 | struct fs_sink_ctf_field_class_struct *struct_fc, |
| 653 | const bt_field_class *ir_fc) |
| 654 | { |
| 655 | int ret = 0; |
| 656 | uint64_t i; |
| 657 | |
| 658 | for (i = 0; i < bt_field_class_structure_get_member_count(ir_fc); i++) { |
| 659 | const bt_field_class_structure_member *member; |
| 660 | const char *name; |
| 661 | const bt_field_class *memb_ir_fc; |
| 662 | |
| 663 | member = |
| 664 | bt_field_class_structure_borrow_member_by_index_const( |
| 665 | ir_fc, i); |
| 666 | name = bt_field_class_structure_member_get_name(member); |
| 667 | memb_ir_fc = bt_field_class_structure_member_borrow_field_class_const( |
| 668 | member); |
| 669 | ret = cur_path_stack_push(ctx, i, name, true, memb_ir_fc, |
| 670 | (void *) struct_fc); |
| 671 | if (ret) { |
| 672 | BT_COMP_LOGE("Cannot translate structure field class member: " |
| 673 | "name=\"%s\"", name); |
| 674 | goto end; |
| 675 | } |
| 676 | |
| 677 | ret = translate_field_class(ctx); |
| 678 | if (ret) { |
| 679 | BT_COMP_LOGE("Cannot translate structure field class member: " |
| 680 | "name=\"%s\"", name); |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | cur_path_stack_pop(ctx); |
| 685 | } |
| 686 | |
| 687 | end: |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static inline |
| 692 | int translate_structure_field_class(struct ctx *ctx) |
| 693 | { |
| 694 | int ret; |
| 695 | struct fs_sink_ctf_field_class_struct *fc = |
| 696 | fs_sink_ctf_field_class_struct_create_empty( |
| 697 | cur_path_stack_top(ctx)->ir_fc, |
| 698 | cur_path_stack_top(ctx)->index_in_parent); |
| 699 | |
| 700 | BT_ASSERT(fc); |
| 701 | append_to_parent_field_class(ctx, (void *) fc); |
| 702 | ret = translate_structure_field_class_members(ctx, fc, fc->base.ir_fc); |
| 703 | if (ret) { |
| 704 | goto end; |
| 705 | } |
| 706 | |
| 707 | update_parent_field_class_alignment(ctx, fc->base.alignment); |
| 708 | |
| 709 | end: |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * This function protects a given variant FC option name (with the `_` |
| 715 | * prefix) if required. On success, `name_buf->str` contains the variant |
| 716 | * FC option name to use (original option name or protected if |
| 717 | * required). |
| 718 | * |
| 719 | * One of the goals of `sink.ctf.fs` is to write a CTF trace which is as |
| 720 | * close as possible to an original CTF trace as decoded by |
| 721 | * `src.ctf.fs`. |
| 722 | * |
| 723 | * This scenario is valid in CTF 1.8: |
| 724 | * |
| 725 | * enum { |
| 726 | * HELLO, |
| 727 | * MEOW |
| 728 | * } tag; |
| 729 | * |
| 730 | * variant <tag> { |
| 731 | * int HELLO; |
| 732 | * string MEOW; |
| 733 | * }; |
| 734 | * |
| 735 | * Once in trace IR, the enumeration FC mapping names and variant FC |
| 736 | * option names are kept as is. For this reason, we don't want to |
| 737 | * protect the variant FC option names here (by prepending `_`): this |
| 738 | * would make the variant FC option name and the enumeration FC mapping |
| 739 | * name not match. |
| 740 | * |
| 741 | * This scenario is also valid in CTF 1.8: |
| 742 | * |
| 743 | * enum { |
| 744 | * _HELLO, |
| 745 | * MEOW |
| 746 | * } tag; |
| 747 | * |
| 748 | * variant <tag> { |
| 749 | * int _HELLO; |
| 750 | * string MEOW; |
| 751 | * }; |
| 752 | * |
| 753 | * Once in trace IR, the enumeration FC mapping names are kept as is, |
| 754 | * but the `_HELLO` variant FC option name becomes `HELLO` (unprotected |
| 755 | * for presentation, as recommended by CTF 1.8). When going back to |
| 756 | * TSDL, we need to protect `HELLO` so that it becomes `_HELLO` to match |
| 757 | * the corresponding enumeration FC mapping name. |
| 758 | * |
| 759 | * This scenario is also valid in CTF 1.8: |
| 760 | * |
| 761 | * enum { |
| 762 | * __HELLO, |
| 763 | * MEOW |
| 764 | * } tag; |
| 765 | * |
| 766 | * variant <tag> { |
| 767 | * int __HELLO; |
| 768 | * string MEOW; |
| 769 | * }; |
| 770 | * |
| 771 | * Once in trace IR, the enumeration FC mapping names are kept as is, |
| 772 | * but the `__HELLO` variant FC option name becomes `_HELLO` |
| 773 | * (unprotected). When going back to TSDL, we need to protect `_HELLO` |
| 774 | * so that it becomes `__HELLO` to match the corresponding enumeration |
| 775 | * FC mapping name. |
| 776 | * |
| 777 | * `src.ctf.fs` always uses the _same_ integer range sets for a selector |
| 778 | * FC mapping and a corresponding variant FC option. We can use that |
| 779 | * fact to find the original variant FC option names by matching variant |
| 780 | * FC options and enumeration FC mappings by range set. |
| 781 | */ |
| 782 | static |
| 783 | int maybe_protect_variant_option_name(const bt_field_class *ir_var_fc, |
| 784 | const bt_field_class *ir_tag_fc, uint64_t opt_i, |
| 785 | GString *name_buf) |
| 786 | { |
| 787 | int ret = 0; |
| 788 | uint64_t i; |
| 789 | bt_field_class_type ir_var_fc_type; |
| 790 | const void *opt_ranges = NULL; |
| 791 | const char *mapping_label = NULL; |
| 792 | const char *ir_opt_name; |
| 793 | const bt_field_class_variant_option *base_var_opt; |
| 794 | bool force_protect = false; |
| 795 | |
| 796 | ir_var_fc_type = bt_field_class_get_type(ir_var_fc); |
| 797 | base_var_opt = bt_field_class_variant_borrow_option_by_index_const( |
| 798 | ir_var_fc, opt_i); |
| 799 | BT_ASSERT(base_var_opt); |
| 800 | ir_opt_name = bt_field_class_variant_option_get_name(base_var_opt); |
| 801 | BT_ASSERT(ir_opt_name); |
| 802 | |
| 803 | /* |
| 804 | * Check if the variant FC option name is required to be |
| 805 | * protected (reserved TSDL keyword or starts with `_`). In that |
| 806 | * case, the name of the selector FC mapping we find must match |
| 807 | * exactly the protected name. |
| 808 | */ |
| 809 | force_protect = must_protect_identifier(ir_opt_name); |
| 810 | if (force_protect) { |
| 811 | g_string_assign(name_buf, "_"); |
| 812 | g_string_append(name_buf, ir_opt_name); |
| 813 | } else { |
| 814 | g_string_assign(name_buf, ir_opt_name); |
| 815 | } |
| 816 | |
| 817 | /* Borrow option's ranges */ |
| 818 | if (ir_var_fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD) { |
| 819 | /* No ranges: we're done */ |
| 820 | goto end; |
| 821 | } if (ir_var_fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD) { |
| 822 | const bt_field_class_variant_with_selector_field_integer_unsigned_option *var_opt = |
| 823 | bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const( |
| 824 | ir_var_fc, opt_i); |
| 825 | opt_ranges = |
| 826 | bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const( |
| 827 | var_opt); |
| 828 | } else { |
| 829 | const bt_field_class_variant_with_selector_field_integer_signed_option *var_opt = |
| 830 | bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const( |
| 831 | ir_var_fc, opt_i); |
| 832 | opt_ranges = |
| 833 | bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const( |
| 834 | var_opt); |
| 835 | } |
| 836 | |
| 837 | /* Find corresponding mapping by range set in selector FC */ |
| 838 | for (i = 0; i < bt_field_class_enumeration_get_mapping_count(ir_tag_fc); |
| 839 | i++) { |
| 840 | if (ir_var_fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD) { |
| 841 | const bt_field_class_enumeration_mapping *mapping_base; |
| 842 | const bt_field_class_enumeration_unsigned_mapping *mapping; |
| 843 | const bt_integer_range_set_unsigned *mapping_ranges; |
| 844 | |
| 845 | mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
| 846 | ir_tag_fc, i); |
| 847 | mapping_ranges = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
| 848 | mapping); |
| 849 | |
| 850 | if (bt_integer_range_set_unsigned_is_equal(opt_ranges, |
| 851 | mapping_ranges)) { |
| 852 | /* We have a winner */ |
| 853 | mapping_base = |
| 854 | bt_field_class_enumeration_unsigned_mapping_as_mapping_const( |
| 855 | mapping); |
| 856 | mapping_label = |
| 857 | bt_field_class_enumeration_mapping_get_label( |
| 858 | mapping_base); |
| 859 | break; |
| 860 | } |
| 861 | } else { |
| 862 | const bt_field_class_enumeration_mapping *mapping_base; |
| 863 | const bt_field_class_enumeration_signed_mapping *mapping; |
| 864 | const bt_integer_range_set_signed *mapping_ranges; |
| 865 | |
| 866 | mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const( |
| 867 | ir_tag_fc, i); |
| 868 | mapping_ranges = bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
| 869 | mapping); |
| 870 | |
| 871 | if (bt_integer_range_set_signed_is_equal(opt_ranges, |
| 872 | mapping_ranges)) { |
| 873 | /* We have a winner */ |
| 874 | mapping_base = |
| 875 | bt_field_class_enumeration_signed_mapping_as_mapping_const( |
| 876 | mapping); |
| 877 | mapping_label = |
| 878 | bt_field_class_enumeration_mapping_get_label( |
| 879 | mapping_base); |
| 880 | break; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if (!mapping_label) { |
| 886 | /* Range set not found: invalid selector for CTF 1.8 */ |
| 887 | ret = -1; |
| 888 | goto end; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * If the enumeration FC mapping name is not the same as the |
| 893 | * variant FC option name and we didn't protect already, try |
| 894 | * protecting the option name and check again. |
| 895 | */ |
| 896 | if (strcmp(mapping_label, name_buf->str) != 0) { |
| 897 | if (force_protect) { |
| 898 | ret = -1; |
| 899 | goto end; |
| 900 | } |
| 901 | |
| 902 | if (mapping_label[0] == '\0') { |
| 903 | ret = -1; |
| 904 | goto end; |
| 905 | } |
| 906 | |
| 907 | g_string_assign(name_buf, "_"); |
| 908 | g_string_append(name_buf, ir_opt_name); |
| 909 | |
| 910 | if (strcmp(mapping_label, name_buf->str) != 0) { |
| 911 | ret = -1; |
| 912 | goto end; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | end: |
| 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | static inline |
| 921 | int translate_option_field_class(struct ctx *ctx) |
| 922 | { |
| 923 | struct fs_sink_ctf_field_class_option *fc = |
| 924 | fs_sink_ctf_field_class_option_create_empty( |
| 925 | cur_path_stack_top(ctx)->ir_fc, |
| 926 | cur_path_stack_top(ctx)->index_in_parent); |
| 927 | const bt_field_class *content_ir_fc = |
| 928 | bt_field_class_option_borrow_field_class_const(fc->base.ir_fc); |
| 929 | int ret; |
| 930 | |
| 931 | BT_ASSERT(fc); |
| 932 | |
| 933 | /* |
| 934 | * CTF 1.8 does not support the option field class type. To |
| 935 | * write something anyway, this component translates this type |
| 936 | * to a variant field class where the options are: |
| 937 | * |
| 938 | * * An empty structure field class. |
| 939 | * * The optional field class itself. |
| 940 | * |
| 941 | * The "tag" is always generated/before in that case (an 8-bit |
| 942 | * unsigned enumeration field class). |
| 943 | */ |
| 944 | append_to_parent_field_class(ctx, (void *) fc); |
| 945 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, false, content_ir_fc, |
| 946 | (void *) fc); |
| 947 | if (ret) { |
| 948 | BT_COMP_LOGE_STR("Cannot translate option field class content."); |
| 949 | goto end; |
| 950 | } |
| 951 | |
| 952 | ret = translate_field_class(ctx); |
| 953 | if (ret) { |
| 954 | BT_COMP_LOGE_STR("Cannot translate option field class content."); |
| 955 | goto end; |
| 956 | } |
| 957 | |
| 958 | cur_path_stack_pop(ctx); |
| 959 | update_parent_field_class_alignment(ctx, fc->base.alignment); |
| 960 | |
| 961 | end: |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static inline |
| 966 | int translate_variant_field_class(struct ctx *ctx) |
| 967 | { |
| 968 | int ret = 0; |
| 969 | uint64_t i; |
| 970 | struct fs_sink_ctf_field_class_variant *fc = |
| 971 | fs_sink_ctf_field_class_variant_create_empty( |
| 972 | cur_path_stack_top(ctx)->ir_fc, |
| 973 | cur_path_stack_top(ctx)->index_in_parent); |
| 974 | bt_field_class_type ir_fc_type; |
| 975 | const bt_field_path *ir_selector_field_path = NULL; |
| 976 | struct fs_sink_ctf_field_class *tgt_fc = NULL; |
| 977 | GString *name_buf = g_string_new(NULL); |
| 978 | bt_value *prot_opt_names = bt_value_array_create(); |
| 979 | uint64_t opt_count; |
| 980 | |
| 981 | BT_ASSERT(fc); |
| 982 | BT_ASSERT(name_buf); |
| 983 | BT_ASSERT(prot_opt_names); |
| 984 | ir_fc_type = bt_field_class_get_type(fc->base.ir_fc); |
| 985 | opt_count = bt_field_class_variant_get_option_count(fc->base.ir_fc); |
| 986 | |
| 987 | if (bt_field_class_type_is(ir_fc_type, |
| 988 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD)) { |
| 989 | ir_selector_field_path = bt_field_class_variant_with_selector_field_borrow_selector_field_path_const( |
| 990 | fc->base.ir_fc); |
| 991 | BT_ASSERT(ir_selector_field_path); |
| 992 | } |
| 993 | |
| 994 | /* Resolve tag field class before appending to parent */ |
| 995 | resolve_field_class(ctx, ir_selector_field_path, fc->tag_ref, |
| 996 | &fc->tag_is_before, &tgt_fc); |
| 997 | |
| 998 | if (ir_selector_field_path && tgt_fc) { |
| 999 | uint64_t mapping_count; |
| 1000 | uint64_t option_count; |
| 1001 | |
| 1002 | /* CTF 1.8: selector FC must be an enumeration FC */ |
| 1003 | bt_field_class_type type = bt_field_class_get_type( |
| 1004 | tgt_fc->ir_fc); |
| 1005 | |
| 1006 | if (!bt_field_class_type_is(type, |
| 1007 | BT_FIELD_CLASS_TYPE_ENUMERATION)) { |
| 1008 | fc->tag_is_before = true; |
| 1009 | goto validate_opts; |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * Call maybe_protect_variant_option_name() for each |
| 1014 | * option below. In that case we also want selector FC |
| 1015 | * to contain as many mappings as the variant FC has |
| 1016 | * options. |
| 1017 | */ |
| 1018 | mapping_count = bt_field_class_enumeration_get_mapping_count( |
| 1019 | tgt_fc->ir_fc); |
| 1020 | option_count = bt_field_class_variant_get_option_count( |
| 1021 | fc->base.ir_fc); |
| 1022 | |
| 1023 | if (mapping_count != option_count) { |
| 1024 | fc->tag_is_before = true; |
| 1025 | goto validate_opts; |
| 1026 | } |
| 1027 | } else { |
| 1028 | /* |
| 1029 | * No compatible selector field class for CTF 1.8: |
| 1030 | * create the appropriate selector field class. |
| 1031 | */ |
| 1032 | fc->tag_is_before = true; |
| 1033 | goto validate_opts; |
| 1034 | } |
| 1035 | |
| 1036 | validate_opts: |
| 1037 | /* |
| 1038 | * First pass: detect any option name clash with option name |
| 1039 | * protection. In that case, we don't fail: just create the |
| 1040 | * selector field class before the variant field class. |
| 1041 | * |
| 1042 | * After this, `prot_opt_names` contains the final option names, |
| 1043 | * potentially protected if needed. They can still be invalid |
| 1044 | * TSDL identifiers however; this will be checked by |
| 1045 | * cur_path_stack_push(). |
| 1046 | */ |
| 1047 | for (i = 0; i < opt_count; i++) { |
| 1048 | if (!fc->tag_is_before) { |
| 1049 | BT_ASSERT(tgt_fc->ir_fc); |
| 1050 | ret = maybe_protect_variant_option_name(fc->base.ir_fc, |
| 1051 | tgt_fc->ir_fc, i, name_buf); |
| 1052 | if (ret) { |
| 1053 | fc->tag_is_before = true; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | ret = bt_value_array_append_string_element(prot_opt_names, |
| 1058 | name_buf->str); |
| 1059 | if (ret) { |
| 1060 | goto end; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | for (i = 0; i < opt_count; i++) { |
| 1065 | uint64_t j; |
| 1066 | const bt_value *opt_name_a = |
| 1067 | bt_value_array_borrow_element_by_index_const( |
| 1068 | prot_opt_names, i); |
| 1069 | |
| 1070 | for (j = 0; j < opt_count; j++) { |
| 1071 | const bt_value *opt_name_b; |
| 1072 | |
| 1073 | if (i == j) { |
| 1074 | continue; |
| 1075 | } |
| 1076 | |
| 1077 | opt_name_b = |
| 1078 | bt_value_array_borrow_element_by_index_const( |
| 1079 | prot_opt_names, j); |
| 1080 | if (bt_value_is_equal(opt_name_a, opt_name_b)) { |
| 1081 | /* |
| 1082 | * Variant FC option names are not |
| 1083 | * unique when protected. |
| 1084 | */ |
| 1085 | fc->tag_is_before = true; |
| 1086 | goto append_to_parent; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | append_to_parent: |
| 1092 | append_to_parent_field_class(ctx, (void *) fc); |
| 1093 | |
| 1094 | for (i = 0; i < opt_count; i++) { |
| 1095 | const bt_field_class_variant_option *opt; |
| 1096 | const bt_field_class *opt_ir_fc; |
| 1097 | const bt_value *prot_opt_name_val = |
| 1098 | bt_value_array_borrow_element_by_index_const( |
| 1099 | prot_opt_names, i); |
| 1100 | const char *prot_opt_name = bt_value_string_get( |
| 1101 | prot_opt_name_val); |
| 1102 | |
| 1103 | BT_ASSERT(prot_opt_name); |
| 1104 | opt = bt_field_class_variant_borrow_option_by_index_const( |
| 1105 | fc->base.ir_fc, i); |
| 1106 | opt_ir_fc = bt_field_class_variant_option_borrow_field_class_const( |
| 1107 | opt); |
| 1108 | |
| 1109 | /* |
| 1110 | * We don't ask cur_path_stack_push() to protect the |
| 1111 | * option name because it's already protected at this |
| 1112 | * point. |
| 1113 | */ |
| 1114 | ret = cur_path_stack_push(ctx, i, prot_opt_name, false, |
| 1115 | opt_ir_fc, (void *) fc); |
| 1116 | if (ret) { |
| 1117 | BT_COMP_LOGE("Cannot translate variant field class option: " |
| 1118 | "name=\"%s\"", prot_opt_name); |
| 1119 | goto end; |
| 1120 | } |
| 1121 | |
| 1122 | ret = translate_field_class(ctx); |
| 1123 | if (ret) { |
| 1124 | BT_COMP_LOGE("Cannot translate variant field class option: " |
| 1125 | "name=\"%s\"", prot_opt_name); |
| 1126 | goto end; |
| 1127 | } |
| 1128 | |
| 1129 | cur_path_stack_pop(ctx); |
| 1130 | } |
| 1131 | |
| 1132 | end: |
| 1133 | if (name_buf) { |
| 1134 | g_string_free(name_buf, TRUE); |
| 1135 | } |
| 1136 | |
| 1137 | bt_value_put_ref(prot_opt_names); |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
| 1141 | static inline |
| 1142 | int translate_static_array_field_class(struct ctx *ctx) |
| 1143 | { |
| 1144 | struct fs_sink_ctf_field_class_array *fc = |
| 1145 | fs_sink_ctf_field_class_array_create_empty( |
| 1146 | cur_path_stack_top(ctx)->ir_fc, |
| 1147 | cur_path_stack_top(ctx)->index_in_parent); |
| 1148 | const bt_field_class *elem_ir_fc = |
| 1149 | bt_field_class_array_borrow_element_field_class_const( |
| 1150 | fc->base.base.ir_fc); |
| 1151 | int ret; |
| 1152 | |
| 1153 | BT_ASSERT(fc); |
| 1154 | append_to_parent_field_class(ctx, (void *) fc); |
| 1155 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, false, elem_ir_fc, |
| 1156 | (void *) fc); |
| 1157 | if (ret) { |
| 1158 | BT_COMP_LOGE_STR("Cannot translate static array field class element."); |
| 1159 | goto end; |
| 1160 | } |
| 1161 | |
| 1162 | ret = translate_field_class(ctx); |
| 1163 | if (ret) { |
| 1164 | BT_COMP_LOGE_STR("Cannot translate static array field class element."); |
| 1165 | goto end; |
| 1166 | } |
| 1167 | |
| 1168 | cur_path_stack_pop(ctx); |
| 1169 | update_parent_field_class_alignment(ctx, fc->base.base.alignment); |
| 1170 | |
| 1171 | end: |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
| 1175 | static inline |
| 1176 | int translate_dynamic_array_field_class(struct ctx *ctx) |
| 1177 | { |
| 1178 | struct fs_sink_ctf_field_class_sequence *fc = |
| 1179 | fs_sink_ctf_field_class_sequence_create_empty( |
| 1180 | cur_path_stack_top(ctx)->ir_fc, |
| 1181 | cur_path_stack_top(ctx)->index_in_parent); |
| 1182 | const bt_field_class *elem_ir_fc = |
| 1183 | bt_field_class_array_borrow_element_field_class_const( |
| 1184 | fc->base.base.ir_fc); |
| 1185 | int ret; |
| 1186 | |
| 1187 | BT_ASSERT(fc); |
| 1188 | |
| 1189 | /* Resolve length field class before appending to parent */ |
| 1190 | if (bt_field_class_get_type(cur_path_stack_top(ctx)->ir_fc) == |
| 1191 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD) { |
| 1192 | resolve_field_class(ctx, |
| 1193 | bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const( |
| 1194 | fc->base.base.ir_fc), |
| 1195 | fc->length_ref, &fc->length_is_before, NULL); |
| 1196 | } |
| 1197 | |
| 1198 | append_to_parent_field_class(ctx, (void *) fc); |
| 1199 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, false, elem_ir_fc, |
| 1200 | (void *) fc); |
| 1201 | if (ret) { |
| 1202 | BT_COMP_LOGE_STR("Cannot translate dynamic array field class element."); |
| 1203 | goto end; |
| 1204 | } |
| 1205 | |
| 1206 | ret = translate_field_class(ctx); |
| 1207 | if (ret) { |
| 1208 | BT_COMP_LOGE_STR("Cannot translate dynamic array field class element."); |
| 1209 | goto end; |
| 1210 | } |
| 1211 | |
| 1212 | cur_path_stack_pop(ctx); |
| 1213 | update_parent_field_class_alignment(ctx, fc->base.base.alignment); |
| 1214 | |
| 1215 | end: |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | static inline |
| 1220 | int translate_bool_field_class(struct ctx *ctx) |
| 1221 | { |
| 1222 | struct fs_sink_ctf_field_class_bool *fc = |
| 1223 | fs_sink_ctf_field_class_bool_create( |
| 1224 | cur_path_stack_top(ctx)->ir_fc, |
| 1225 | cur_path_stack_top(ctx)->index_in_parent); |
| 1226 | |
| 1227 | BT_ASSERT(fc); |
| 1228 | append_to_parent_field_class(ctx, (void *) fc); |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static inline |
| 1233 | int translate_bit_array_field_class(struct ctx *ctx) |
| 1234 | { |
| 1235 | struct fs_sink_ctf_field_class_bit_array *fc = |
| 1236 | fs_sink_ctf_field_class_bit_array_create( |
| 1237 | cur_path_stack_top(ctx)->ir_fc, |
| 1238 | cur_path_stack_top(ctx)->index_in_parent); |
| 1239 | |
| 1240 | BT_ASSERT(fc); |
| 1241 | append_to_parent_field_class(ctx, (void *) fc); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static inline |
| 1246 | int translate_integer_field_class(struct ctx *ctx) |
| 1247 | { |
| 1248 | struct fs_sink_ctf_field_class_int *fc = |
| 1249 | fs_sink_ctf_field_class_int_create( |
| 1250 | cur_path_stack_top(ctx)->ir_fc, |
| 1251 | cur_path_stack_top(ctx)->index_in_parent); |
| 1252 | |
| 1253 | BT_ASSERT(fc); |
| 1254 | append_to_parent_field_class(ctx, (void *) fc); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | static inline |
| 1259 | int translate_real_field_class(struct ctx *ctx) |
| 1260 | { |
| 1261 | struct fs_sink_ctf_field_class_float *fc = |
| 1262 | fs_sink_ctf_field_class_float_create( |
| 1263 | cur_path_stack_top(ctx)->ir_fc, |
| 1264 | cur_path_stack_top(ctx)->index_in_parent); |
| 1265 | |
| 1266 | BT_ASSERT(fc); |
| 1267 | append_to_parent_field_class(ctx, (void *) fc); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | static inline |
| 1272 | int translate_string_field_class(struct ctx *ctx) |
| 1273 | { |
| 1274 | struct fs_sink_ctf_field_class_string *fc = |
| 1275 | fs_sink_ctf_field_class_string_create( |
| 1276 | cur_path_stack_top(ctx)->ir_fc, |
| 1277 | cur_path_stack_top(ctx)->index_in_parent); |
| 1278 | |
| 1279 | BT_ASSERT(fc); |
| 1280 | append_to_parent_field_class(ctx, (void *) fc); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Translates a field class, recursively. |
| 1286 | * |
| 1287 | * The field class's IR field class, parent field class, and index |
| 1288 | * within its parent are in the context's current path's top element |
| 1289 | * (cur_path_stack_top()). |
| 1290 | */ |
| 1291 | static |
| 1292 | int translate_field_class(struct ctx *ctx) |
| 1293 | { |
| 1294 | int ret; |
| 1295 | bt_field_class_type ir_fc_type = |
| 1296 | bt_field_class_get_type(cur_path_stack_top(ctx)->ir_fc); |
| 1297 | |
| 1298 | if (ir_fc_type == BT_FIELD_CLASS_TYPE_BOOL) { |
| 1299 | ret = translate_bool_field_class(ctx); |
| 1300 | } else if (ir_fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) { |
| 1301 | ret = translate_bit_array_field_class(ctx); |
| 1302 | } else if (bt_field_class_type_is(ir_fc_type, |
| 1303 | BT_FIELD_CLASS_TYPE_INTEGER)) { |
| 1304 | ret = translate_integer_field_class(ctx); |
| 1305 | } else if (bt_field_class_type_is(ir_fc_type, |
| 1306 | BT_FIELD_CLASS_TYPE_REAL)) { |
| 1307 | ret = translate_real_field_class(ctx); |
| 1308 | } else if (ir_fc_type == BT_FIELD_CLASS_TYPE_STRING) { |
| 1309 | ret = translate_string_field_class(ctx); |
| 1310 | } else if (ir_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) { |
| 1311 | ret = translate_structure_field_class(ctx); |
| 1312 | } else if (ir_fc_type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) { |
| 1313 | ret = translate_static_array_field_class(ctx); |
| 1314 | } else if (bt_field_class_type_is(ir_fc_type, |
| 1315 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) { |
| 1316 | ret = translate_dynamic_array_field_class(ctx); |
| 1317 | } else if (bt_field_class_type_is(ir_fc_type, |
| 1318 | BT_FIELD_CLASS_TYPE_OPTION)) { |
| 1319 | ret = translate_option_field_class(ctx); |
| 1320 | } else if (bt_field_class_type_is(ir_fc_type, |
| 1321 | BT_FIELD_CLASS_TYPE_VARIANT)) { |
| 1322 | ret = translate_variant_field_class(ctx); |
| 1323 | } else { |
| 1324 | bt_common_abort(); |
| 1325 | } |
| 1326 | |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | static |
| 1331 | int set_field_ref(struct fs_sink_ctf_field_class *fc, const char *fc_name, |
| 1332 | struct fs_sink_ctf_field_class *parent_fc) |
| 1333 | { |
| 1334 | int ret = 0; |
| 1335 | GString *field_ref = NULL; |
| 1336 | bool is_before; |
| 1337 | const char *tgt_type; |
| 1338 | struct fs_sink_ctf_field_class_struct *parent_struct_fc = |
| 1339 | (void *) parent_fc; |
| 1340 | uint64_t i; |
| 1341 | unsigned int suffix = 0; |
| 1342 | |
| 1343 | if (!fc_name || !parent_fc || |
| 1344 | parent_fc->type != FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 1345 | /* Not supported */ |
| 1346 | ret = -1; |
| 1347 | goto end; |
| 1348 | } |
| 1349 | |
| 1350 | switch (fc->type) { |
| 1351 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
| 1352 | { |
| 1353 | /* |
| 1354 | * CTF 1.8 does not support the option field class type. |
| 1355 | * To write something anyway, this component translates |
| 1356 | * this type to a variant field class where the options |
| 1357 | * are: |
| 1358 | * |
| 1359 | * * An empty structure field class. |
| 1360 | * * The optional field class itself. |
| 1361 | * |
| 1362 | * Because the option field class becomes a CTF variant |
| 1363 | * field class, we use the term "tag" too here. |
| 1364 | * |
| 1365 | * The "tag" is always generated/before in that case (an |
| 1366 | * 8-bit unsigned enumeration field class). |
| 1367 | */ |
| 1368 | struct fs_sink_ctf_field_class_option *opt_fc = (void *) fc; |
| 1369 | |
| 1370 | field_ref = opt_fc->tag_ref; |
| 1371 | is_before = true; |
| 1372 | tgt_type = "tag"; |
| 1373 | break; |
| 1374 | } |
| 1375 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1376 | { |
| 1377 | struct fs_sink_ctf_field_class_sequence *seq_fc = (void *) fc; |
| 1378 | |
| 1379 | field_ref = seq_fc->length_ref; |
| 1380 | is_before = seq_fc->length_is_before; |
| 1381 | tgt_type = "len"; |
| 1382 | break; |
| 1383 | } |
| 1384 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1385 | { |
| 1386 | struct fs_sink_ctf_field_class_variant *var_fc = (void *) fc; |
| 1387 | |
| 1388 | field_ref = var_fc->tag_ref; |
| 1389 | is_before = var_fc->tag_is_before; |
| 1390 | tgt_type = "tag"; |
| 1391 | break; |
| 1392 | } |
| 1393 | default: |
| 1394 | bt_common_abort(); |
| 1395 | } |
| 1396 | |
| 1397 | BT_ASSERT(field_ref); |
| 1398 | |
| 1399 | if (!is_before) { |
| 1400 | goto end; |
| 1401 | } |
| 1402 | |
| 1403 | /* Initial field ref */ |
| 1404 | g_string_printf(field_ref, "__%s_%s", fc_name, tgt_type); |
| 1405 | |
| 1406 | /* |
| 1407 | * Make sure field ref does not clash with an existing field |
| 1408 | * class name within the same parent structure field class. |
| 1409 | */ |
| 1410 | while (true) { |
| 1411 | bool name_ok = true; |
| 1412 | |
| 1413 | for (i = 0; i < parent_struct_fc->members->len; i++) { |
| 1414 | struct fs_sink_ctf_named_field_class *named_fc = |
| 1415 | fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 1416 | parent_struct_fc, i); |
| 1417 | |
| 1418 | if (strcmp(field_ref->str, named_fc->name->str) == 0) { |
| 1419 | /* Name clash */ |
| 1420 | name_ok = false; |
| 1421 | break; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | if (name_ok) { |
| 1426 | /* No clash: we're done */ |
| 1427 | break; |
| 1428 | } |
| 1429 | |
| 1430 | /* Append suffix and try again */ |
| 1431 | g_string_printf(field_ref, "__%s_%s_%u", fc_name, tgt_type, |
| 1432 | suffix); |
| 1433 | suffix++; |
| 1434 | } |
| 1435 | |
| 1436 | end: |
| 1437 | return ret; |
| 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * This function recursively sets field refs of sequence and variant |
| 1442 | * field classes when they are immediately before, avoiding name clashes |
| 1443 | * with existing field class names. |
| 1444 | * |
| 1445 | * It can fail at this point if, for example, a sequence field class of |
| 1446 | * which to set the length's field ref has something else than a |
| 1447 | * structure field class as its parent: in this case, there's no |
| 1448 | * location to place the length field class immediately before the |
| 1449 | * sequence field class. |
| 1450 | */ |
| 1451 | static |
| 1452 | int set_field_refs(struct fs_sink_ctf_field_class * const fc, |
| 1453 | const char *fc_name, struct fs_sink_ctf_field_class *parent_fc) |
| 1454 | { |
| 1455 | int ret = 0; |
| 1456 | enum fs_sink_ctf_field_class_type fc_type; |
| 1457 | BT_ASSERT(fc); |
| 1458 | |
| 1459 | fc_type = fc->type; |
| 1460 | |
| 1461 | switch (fc_type) { |
| 1462 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
| 1463 | { |
| 1464 | struct fs_sink_ctf_field_class_option *opt_fc = (void *) fc; |
| 1465 | |
| 1466 | ret = set_field_ref(fc, fc_name, parent_fc); |
| 1467 | if (ret) { |
| 1468 | goto end; |
| 1469 | } |
| 1470 | |
| 1471 | ret = set_field_refs(opt_fc->content_fc, NULL, fc); |
| 1472 | if (ret) { |
| 1473 | goto end; |
| 1474 | } |
| 1475 | |
| 1476 | break; |
| 1477 | } |
| 1478 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 1479 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1480 | { |
| 1481 | uint64_t i; |
| 1482 | uint64_t len; |
| 1483 | struct fs_sink_ctf_field_class_struct *struct_fc = NULL; |
| 1484 | struct fs_sink_ctf_field_class_variant *var_fc = NULL; |
| 1485 | struct fs_sink_ctf_named_field_class *named_fc; |
| 1486 | |
| 1487 | if (fc_type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 1488 | struct_fc = (void *) fc; |
| 1489 | len = struct_fc->members->len; |
| 1490 | } else { |
| 1491 | var_fc = (void *) fc; |
| 1492 | len = var_fc->options->len; |
| 1493 | ret = set_field_ref(fc, fc_name, parent_fc); |
| 1494 | if (ret) { |
| 1495 | goto end; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | for (i = 0; i < len; i++) { |
| 1500 | if (fc_type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 1501 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 1502 | struct_fc, i); |
| 1503 | } else { |
| 1504 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 1505 | var_fc, i); |
| 1506 | } |
| 1507 | |
| 1508 | ret = set_field_refs(named_fc->fc, named_fc->name->str, |
| 1509 | fc); |
| 1510 | if (ret) { |
| 1511 | goto end; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | break; |
| 1516 | } |
| 1517 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 1518 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1519 | { |
| 1520 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 1521 | (void *) fc; |
| 1522 | |
| 1523 | if (fc_type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
| 1524 | ret = set_field_ref(fc, fc_name, parent_fc); |
| 1525 | if (ret) { |
| 1526 | goto end; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | ret = set_field_refs(array_base_fc->elem_fc, NULL, fc); |
| 1531 | if (ret) { |
| 1532 | goto end; |
| 1533 | } |
| 1534 | |
| 1535 | break; |
| 1536 | } |
| 1537 | default: |
| 1538 | break; |
| 1539 | } |
| 1540 | |
| 1541 | end: |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * This function translates a root scope trace IR field class to |
| 1547 | * a CTF IR field class. |
| 1548 | * |
| 1549 | * The resulting CTF IR field class is written to `*fc` so that it |
| 1550 | * exists as the parent object's (stream class or event class) true root |
| 1551 | * field class during the recursive translation for resolving purposes. |
| 1552 | * This is also why this function creates the empty structure field |
| 1553 | * class and then calls translate_structure_field_class_members() to |
| 1554 | * fill it. |
| 1555 | */ |
| 1556 | static |
| 1557 | int translate_scope_field_class(struct ctx *ctx, bt_field_path_scope scope, |
| 1558 | struct fs_sink_ctf_field_class **fc, |
| 1559 | const bt_field_class *ir_fc) |
| 1560 | { |
| 1561 | int ret = 0; |
| 1562 | |
| 1563 | if (!ir_fc) { |
| 1564 | goto end; |
| 1565 | } |
| 1566 | |
| 1567 | BT_ASSERT(bt_field_class_get_type(ir_fc) == |
| 1568 | BT_FIELD_CLASS_TYPE_STRUCTURE); |
| 1569 | BT_ASSERT(fc); |
| 1570 | *fc = (void *) fs_sink_ctf_field_class_struct_create_empty( |
| 1571 | ir_fc, UINT64_C(-1)); |
| 1572 | BT_ASSERT(*fc); |
| 1573 | ctx->cur_scope = scope; |
| 1574 | BT_ASSERT(ctx->cur_path->len == 0); |
| 1575 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, false, ir_fc, NULL); |
| 1576 | if (ret) { |
| 1577 | BT_COMP_LOGE("Cannot translate scope structure field class: " |
| 1578 | "scope=%d", scope); |
| 1579 | goto end; |
| 1580 | } |
| 1581 | |
| 1582 | ret = translate_structure_field_class_members(ctx, (void *) *fc, ir_fc); |
| 1583 | if (ret) { |
| 1584 | BT_COMP_LOGE("Cannot translate scope structure field class: " |
| 1585 | "scope=%d", scope); |
| 1586 | goto end; |
| 1587 | } |
| 1588 | |
| 1589 | cur_path_stack_pop(ctx); |
| 1590 | |
| 1591 | /* Set field refs for preceding targets */ |
| 1592 | ret = set_field_refs(*fc, NULL, NULL); |
| 1593 | |
| 1594 | end: |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | static inline |
| 1599 | void ctx_init(struct ctx *ctx, struct fs_sink_comp *fs_sink) |
| 1600 | { |
| 1601 | memset(ctx, 0, sizeof(struct ctx)); |
| 1602 | ctx->cur_path = g_array_new(FALSE, TRUE, |
| 1603 | sizeof(struct field_path_elem)); |
| 1604 | BT_ASSERT(ctx->cur_path); |
| 1605 | ctx->log_level = fs_sink->log_level; |
| 1606 | ctx->self_comp = fs_sink->self_comp; |
| 1607 | } |
| 1608 | |
| 1609 | static inline |
| 1610 | void ctx_fini(struct ctx *ctx) |
| 1611 | { |
| 1612 | if (ctx->cur_path) { |
| 1613 | g_array_free(ctx->cur_path, TRUE); |
| 1614 | ctx->cur_path = NULL; |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | static |
| 1619 | int translate_event_class(struct fs_sink_comp *fs_sink, |
| 1620 | struct fs_sink_ctf_stream_class *sc, |
| 1621 | const bt_event_class *ir_ec, |
| 1622 | struct fs_sink_ctf_event_class **out_ec) |
| 1623 | { |
| 1624 | int ret = 0; |
| 1625 | struct ctx ctx; |
| 1626 | struct fs_sink_ctf_event_class *ec; |
| 1627 | |
| 1628 | BT_ASSERT(sc); |
| 1629 | BT_ASSERT(ir_ec); |
| 1630 | |
| 1631 | ctx_init(&ctx, fs_sink); |
| 1632 | ec = fs_sink_ctf_event_class_create(sc, ir_ec); |
| 1633 | BT_ASSERT(ec); |
| 1634 | ctx.cur_sc = sc; |
| 1635 | ctx.cur_ec = ec; |
| 1636 | ret = translate_scope_field_class(&ctx, BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT, |
| 1637 | &ec->spec_context_fc, |
| 1638 | bt_event_class_borrow_specific_context_field_class_const( |
| 1639 | ir_ec)); |
| 1640 | if (ret) { |
| 1641 | goto end; |
| 1642 | } |
| 1643 | |
| 1644 | ret = translate_scope_field_class(&ctx, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD, |
| 1645 | &ec->payload_fc, |
| 1646 | bt_event_class_borrow_payload_field_class_const(ir_ec)); |
| 1647 | if (ret) { |
| 1648 | goto end; |
| 1649 | } |
| 1650 | |
| 1651 | end: |
| 1652 | ctx_fini(&ctx); |
| 1653 | *out_ec = ec; |
| 1654 | return ret; |
| 1655 | } |
| 1656 | |
| 1657 | BT_HIDDEN |
| 1658 | int try_translate_event_class_trace_ir_to_ctf_ir( |
| 1659 | struct fs_sink_comp *fs_sink, |
| 1660 | struct fs_sink_ctf_stream_class *sc, |
| 1661 | const bt_event_class *ir_ec, |
| 1662 | struct fs_sink_ctf_event_class **out_ec) |
| 1663 | { |
| 1664 | int ret = 0; |
| 1665 | |
| 1666 | BT_ASSERT(sc); |
| 1667 | BT_ASSERT(ir_ec); |
| 1668 | |
| 1669 | /* Check in hash table first */ |
| 1670 | *out_ec = g_hash_table_lookup(sc->event_classes_from_ir, ir_ec); |
| 1671 | if (G_LIKELY(*out_ec)) { |
| 1672 | goto end; |
| 1673 | } |
| 1674 | |
| 1675 | ret = translate_event_class(fs_sink, sc, ir_ec, out_ec); |
| 1676 | |
| 1677 | end: |
| 1678 | return ret; |
| 1679 | } |
| 1680 | |
| 1681 | static |
| 1682 | bool default_clock_class_name_exists(struct fs_sink_ctf_trace *trace, |
| 1683 | const char *name) |
| 1684 | { |
| 1685 | bool exists = false; |
| 1686 | uint64_t i; |
| 1687 | |
| 1688 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 1689 | struct fs_sink_ctf_stream_class *sc = |
| 1690 | trace->stream_classes->pdata[i]; |
| 1691 | |
| 1692 | if (sc->default_clock_class_name->len == 0) { |
| 1693 | /* No default clock class */ |
| 1694 | continue; |
| 1695 | } |
| 1696 | |
| 1697 | if (strcmp(sc->default_clock_class_name->str, name) == 0) { |
| 1698 | exists = true; |
| 1699 | goto end; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | end: |
| 1704 | return exists; |
| 1705 | } |
| 1706 | |
| 1707 | static |
| 1708 | void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class *sc) |
| 1709 | { |
| 1710 | unsigned int suffix = 0; |
| 1711 | char buf[16]; |
| 1712 | |
| 1713 | g_string_assign(sc->default_clock_class_name, ""); |
| 1714 | sprintf(buf, "default"); |
| 1715 | |
| 1716 | while (default_clock_class_name_exists(sc->trace, buf)) { |
| 1717 | sprintf(buf, "default%u", suffix); |
| 1718 | suffix++; |
| 1719 | } |
| 1720 | |
| 1721 | g_string_assign(sc->default_clock_class_name, buf); |
| 1722 | } |
| 1723 | |
| 1724 | static |
| 1725 | int translate_stream_class(struct fs_sink_comp *fs_sink, |
| 1726 | struct fs_sink_ctf_trace *trace, |
| 1727 | const bt_stream_class *ir_sc, |
| 1728 | struct fs_sink_ctf_stream_class **out_sc) |
| 1729 | { |
| 1730 | int ret = 0; |
| 1731 | struct ctx ctx; |
| 1732 | |
| 1733 | BT_ASSERT(trace); |
| 1734 | BT_ASSERT(ir_sc); |
| 1735 | ctx_init(&ctx, fs_sink); |
| 1736 | *out_sc = fs_sink_ctf_stream_class_create(trace, ir_sc); |
| 1737 | BT_ASSERT(*out_sc); |
| 1738 | |
| 1739 | /* Set default clock class's protected name, if any */ |
| 1740 | if ((*out_sc)->default_clock_class) { |
| 1741 | const char *name = bt_clock_class_get_name( |
| 1742 | (*out_sc)->default_clock_class); |
| 1743 | |
| 1744 | if (name) { |
| 1745 | /* Try original name, protected */ |
| 1746 | g_string_assign((*out_sc)->default_clock_class_name, |
| 1747 | ""); |
| 1748 | |
| 1749 | if (must_protect_identifier(name)) { |
| 1750 | g_string_assign( |
| 1751 | (*out_sc)->default_clock_class_name, |
| 1752 | "_"); |
| 1753 | } |
| 1754 | |
| 1755 | g_string_assign((*out_sc)->default_clock_class_name, |
| 1756 | name); |
| 1757 | if (!ist_valid_identifier( |
| 1758 | (*out_sc)->default_clock_class_name->str)) { |
| 1759 | /* Invalid: create a new name */ |
| 1760 | make_unique_default_clock_class_name(*out_sc); |
| 1761 | } |
| 1762 | } else { |
| 1763 | /* No name: create a name */ |
| 1764 | make_unique_default_clock_class_name(*out_sc); |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | ctx.cur_sc = *out_sc; |
| 1769 | ret = translate_scope_field_class(&ctx, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT, |
| 1770 | &(*out_sc)->packet_context_fc, |
| 1771 | bt_stream_class_borrow_packet_context_field_class_const(ir_sc)); |
| 1772 | if (ret) { |
| 1773 | goto error; |
| 1774 | } |
| 1775 | |
| 1776 | if ((*out_sc)->packet_context_fc) { |
| 1777 | /* |
| 1778 | * Make sure the structure field class's alignment is |
| 1779 | * enough: 8 is what we use for our own special members |
| 1780 | * in the packet context. |
| 1781 | */ |
| 1782 | fs_sink_ctf_field_class_struct_align_at_least( |
| 1783 | (void *) (*out_sc)->packet_context_fc, 8); |
| 1784 | } |
| 1785 | |
| 1786 | ret = translate_scope_field_class(&ctx, BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT, |
| 1787 | &(*out_sc)->event_common_context_fc, |
| 1788 | bt_stream_class_borrow_event_common_context_field_class_const( |
| 1789 | ir_sc)); |
| 1790 | if (ret) { |
| 1791 | goto error; |
| 1792 | } |
| 1793 | |
| 1794 | goto end; |
| 1795 | |
| 1796 | error: |
| 1797 | fs_sink_ctf_stream_class_destroy(*out_sc); |
| 1798 | *out_sc = NULL; |
| 1799 | |
| 1800 | end: |
| 1801 | ctx_fini(&ctx); |
| 1802 | return ret; |
| 1803 | } |
| 1804 | |
| 1805 | BT_HIDDEN |
| 1806 | int try_translate_stream_class_trace_ir_to_ctf_ir( |
| 1807 | struct fs_sink_comp *fs_sink, |
| 1808 | struct fs_sink_ctf_trace *trace, |
| 1809 | const bt_stream_class *ir_sc, |
| 1810 | struct fs_sink_ctf_stream_class **out_sc) |
| 1811 | { |
| 1812 | int ret = 0; |
| 1813 | uint64_t i; |
| 1814 | |
| 1815 | BT_ASSERT(trace); |
| 1816 | BT_ASSERT(ir_sc); |
| 1817 | |
| 1818 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 1819 | *out_sc = trace->stream_classes->pdata[i]; |
| 1820 | |
| 1821 | if ((*out_sc)->ir_sc == ir_sc) { |
| 1822 | goto end; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | ret = translate_stream_class(fs_sink, trace, ir_sc, out_sc); |
| 1827 | |
| 1828 | end: |
| 1829 | return ret; |
| 1830 | } |
| 1831 | |
| 1832 | BT_HIDDEN |
| 1833 | struct fs_sink_ctf_trace *translate_trace_trace_ir_to_ctf_ir( |
| 1834 | struct fs_sink_comp *fs_sink, const bt_trace *ir_trace) |
| 1835 | { |
| 1836 | uint64_t count; |
| 1837 | uint64_t i; |
| 1838 | struct fs_sink_ctf_trace *trace = NULL; |
| 1839 | |
| 1840 | /* Check that trace's environment is TSDL-compatible */ |
| 1841 | count = bt_trace_get_environment_entry_count(ir_trace); |
| 1842 | for (i = 0; i < count; i++) { |
| 1843 | const char *name; |
| 1844 | const bt_value *val; |
| 1845 | |
| 1846 | bt_trace_borrow_environment_entry_by_index_const( |
| 1847 | ir_trace, i, &name, &val); |
| 1848 | |
| 1849 | if (!ist_valid_identifier(name)) { |
| 1850 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, fs_sink->log_level, |
| 1851 | fs_sink->self_comp, |
| 1852 | "Unsupported trace class's environment entry name: " |
| 1853 | "name=\"%s\"", name); |
| 1854 | goto end; |
| 1855 | } |
| 1856 | |
| 1857 | switch (bt_value_get_type(val)) { |
| 1858 | case BT_VALUE_TYPE_SIGNED_INTEGER: |
| 1859 | case BT_VALUE_TYPE_STRING: |
| 1860 | break; |
| 1861 | default: |
| 1862 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, fs_sink->log_level, |
| 1863 | fs_sink->self_comp, |
| 1864 | "Unsupported trace class's environment entry value type: " |
| 1865 | "type=%s", |
| 1866 | bt_common_value_type_string( |
| 1867 | bt_value_get_type(val))); |
| 1868 | goto end; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | trace = fs_sink_ctf_trace_create(ir_trace); |
| 1873 | BT_ASSERT(trace); |
| 1874 | |
| 1875 | end: |
| 1876 | return trace; |
| 1877 | } |