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