| 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 "plugins/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_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 inline |
| 102 | int cur_path_stack_push(struct ctx *ctx, |
| 103 | uint64_t index_in_parent, const char *ir_name, |
| 104 | const bt_field_class *ir_fc, |
| 105 | struct fs_sink_ctf_field_class *parent_fc) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | struct field_path_elem *field_path_elem; |
| 109 | |
| 110 | g_array_set_size(ctx->cur_path, ctx->cur_path->len + 1); |
| 111 | field_path_elem = cur_path_stack_top(ctx); |
| 112 | field_path_elem->index_in_parent = index_in_parent; |
| 113 | field_path_elem->name = g_string_new(ir_name); |
| 114 | |
| 115 | if (ir_name) { |
| 116 | if (ctx->cur_scope == BT_SCOPE_PACKET_CONTEXT) { |
| 117 | if (is_reserved_member_name(ir_name, "packet_size") || |
| 118 | is_reserved_member_name(ir_name, "content_size") || |
| 119 | is_reserved_member_name(ir_name, "timestamp_begin") || |
| 120 | is_reserved_member_name(ir_name, "timestamp_end") || |
| 121 | is_reserved_member_name(ir_name, "events_discarded") || |
| 122 | is_reserved_member_name(ir_name, "packet_seq_num")) { |
| 123 | BT_COMP_LOGE("Unsupported reserved TSDL structure field class member " |
| 124 | "or variant field class option name: name=\"%s\"", |
| 125 | ir_name); |
| 126 | ret = -1; |
| 127 | goto end; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | ret = fs_sink_ctf_protect_name(field_path_elem->name); |
| 132 | if (ret) { |
| 133 | BT_COMP_LOGE("Unsupported non-TSDL structure field class member " |
| 134 | "or variant field class option name: name=\"%s\"", |
| 135 | ir_name); |
| 136 | goto end; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | field_path_elem->ir_fc = ir_fc; |
| 141 | field_path_elem->parent_fc = parent_fc; |
| 142 | |
| 143 | end: |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | static inline |
| 148 | void cur_path_stack_pop(struct ctx *ctx) |
| 149 | { |
| 150 | struct field_path_elem *field_path_elem; |
| 151 | |
| 152 | BT_ASSERT(ctx->cur_path->len > 0); |
| 153 | field_path_elem = cur_path_stack_top(ctx); |
| 154 | |
| 155 | if (field_path_elem->name) { |
| 156 | g_string_free(field_path_elem->name, TRUE); |
| 157 | field_path_elem->name = NULL; |
| 158 | } |
| 159 | |
| 160 | g_array_set_size(ctx->cur_path, ctx->cur_path->len - 1); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Creates a relative field ref (a single name) from IR field path |
| 165 | * `tgt_ir_field_path`. |
| 166 | * |
| 167 | * This function tries to locate the target field class recursively from |
| 168 | * the top to the bottom of the context's current path using only the |
| 169 | * target field class's own name. This is because many CTF reading tools |
| 170 | * do not support a relative field ref with more than one element, for |
| 171 | * example `prev_struct.len`. |
| 172 | * |
| 173 | * Returns a negative value if this resolving operation failed. |
| 174 | */ |
| 175 | static |
| 176 | int create_relative_field_ref(struct ctx *ctx, |
| 177 | const bt_field_path *tgt_ir_field_path, GString *tgt_field_ref) |
| 178 | { |
| 179 | int ret = 0; |
| 180 | struct fs_sink_ctf_field_class *tgt_fc = NULL; |
| 181 | uint64_t i; |
| 182 | int64_t si; |
| 183 | const char *tgt_fc_name = NULL; |
| 184 | struct field_path_elem *field_path_elem; |
| 185 | |
| 186 | /* Get target field class's name */ |
| 187 | switch (bt_field_path_get_root_scope(tgt_ir_field_path)) { |
| 188 | case BT_SCOPE_PACKET_CONTEXT: |
| 189 | BT_ASSERT(ctx->cur_sc); |
| 190 | tgt_fc = ctx->cur_sc->packet_context_fc; |
| 191 | break; |
| 192 | case BT_SCOPE_EVENT_COMMON_CONTEXT: |
| 193 | BT_ASSERT(ctx->cur_sc); |
| 194 | tgt_fc = ctx->cur_sc->event_common_context_fc; |
| 195 | break; |
| 196 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 197 | BT_ASSERT(ctx->cur_ec); |
| 198 | tgt_fc = ctx->cur_ec->spec_context_fc; |
| 199 | break; |
| 200 | case BT_SCOPE_EVENT_PAYLOAD: |
| 201 | BT_ASSERT(ctx->cur_ec); |
| 202 | tgt_fc = ctx->cur_ec->payload_fc; |
| 203 | break; |
| 204 | default: |
| 205 | abort(); |
| 206 | } |
| 207 | |
| 208 | i = 0; |
| 209 | |
| 210 | while (i < bt_field_path_get_item_count(tgt_ir_field_path)) { |
| 211 | const bt_field_path_item *fp_item = |
| 212 | bt_field_path_borrow_item_by_index_const( |
| 213 | tgt_ir_field_path, i); |
| 214 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
| 215 | |
| 216 | BT_ASSERT(tgt_fc); |
| 217 | BT_ASSERT(fp_item); |
| 218 | |
| 219 | switch (tgt_fc->type) { |
| 220 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 221 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 222 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 223 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 224 | (void *) tgt_fc, |
| 225 | bt_field_path_item_index_get_index(fp_item)); |
| 226 | break; |
| 227 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 228 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 229 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 230 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 231 | (void *) tgt_fc, |
| 232 | bt_field_path_item_index_get_index(fp_item)); |
| 233 | break; |
| 234 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 235 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 236 | { |
| 237 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 238 | (void *) tgt_fc; |
| 239 | |
| 240 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 241 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); |
| 242 | tgt_fc = array_base_fc->elem_fc; |
| 243 | break; |
| 244 | } |
| 245 | default: |
| 246 | abort(); |
| 247 | } |
| 248 | |
| 249 | if (named_fc) { |
| 250 | tgt_fc = named_fc->fc; |
| 251 | tgt_fc_name = named_fc->name->str; |
| 252 | i++; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | BT_ASSERT(tgt_fc); |
| 257 | BT_ASSERT(tgt_fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT); |
| 258 | BT_ASSERT(tgt_fc_name); |
| 259 | |
| 260 | /* Find target field class having this name in current context */ |
| 261 | for (si = ctx->cur_path->len - 1; si >= 0; si--) { |
| 262 | struct fs_sink_ctf_field_class *fc; |
| 263 | struct fs_sink_ctf_field_class_struct *struct_fc; |
| 264 | struct fs_sink_ctf_field_class_variant *var_fc; |
| 265 | struct fs_sink_ctf_named_field_class *named_fc; |
| 266 | uint64_t len; |
| 267 | |
| 268 | field_path_elem = cur_path_stack_at(ctx, (uint64_t) si); |
| 269 | fc = field_path_elem->parent_fc; |
| 270 | if (!fc) { |
| 271 | /* Reached stack's bottom */ |
| 272 | ret = -1; |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | switch (fc->type) { |
| 277 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 278 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 279 | break; |
| 280 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 281 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 282 | continue; |
| 283 | default: |
| 284 | /* Not supported by TSDL 1.8 */ |
| 285 | ret = -1; |
| 286 | goto end; |
| 287 | } |
| 288 | |
| 289 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 290 | struct_fc = (void *) fc; |
| 291 | len = struct_fc->members->len; |
| 292 | } else { |
| 293 | var_fc = (void *) fc; |
| 294 | len = var_fc->options->len; |
| 295 | } |
| 296 | |
| 297 | for (i = 0; i < len; i++) { |
| 298 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 299 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 300 | struct_fc, i); |
| 301 | } else { |
| 302 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 303 | var_fc, i); |
| 304 | } |
| 305 | |
| 306 | if (strcmp(named_fc->name->str, tgt_fc_name) == 0) { |
| 307 | if (named_fc->fc == tgt_fc) { |
| 308 | g_string_assign(tgt_field_ref, |
| 309 | tgt_fc_name); |
| 310 | } else { |
| 311 | /* |
| 312 | * Using only the target field |
| 313 | * class's name, we're not |
| 314 | * reaching the target field |
| 315 | * class. This is not supported |
| 316 | * by TSDL 1.8. |
| 317 | */ |
| 318 | ret = -1; |
| 319 | } |
| 320 | |
| 321 | goto end; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | end: |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Creates an absolute field ref from IR field path `tgt_ir_field_path`. |
| 332 | * |
| 333 | * Returns a negative value if this resolving operation failed. |
| 334 | */ |
| 335 | static |
| 336 | int create_absolute_field_ref(struct ctx *ctx, |
| 337 | const bt_field_path *tgt_ir_field_path, GString *tgt_field_ref) |
| 338 | { |
| 339 | int ret = 0; |
| 340 | struct fs_sink_ctf_field_class *fc = NULL; |
| 341 | uint64_t i; |
| 342 | |
| 343 | switch (bt_field_path_get_root_scope(tgt_ir_field_path)) { |
| 344 | case BT_SCOPE_PACKET_CONTEXT: |
| 345 | BT_ASSERT(ctx->cur_sc); |
| 346 | fc = ctx->cur_sc->packet_context_fc; |
| 347 | g_string_assign(tgt_field_ref, "stream.packet.context"); |
| 348 | break; |
| 349 | case BT_SCOPE_EVENT_COMMON_CONTEXT: |
| 350 | BT_ASSERT(ctx->cur_sc); |
| 351 | fc = ctx->cur_sc->event_common_context_fc; |
| 352 | g_string_assign(tgt_field_ref, "stream.event.context"); |
| 353 | break; |
| 354 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 355 | BT_ASSERT(ctx->cur_ec); |
| 356 | fc = ctx->cur_ec->spec_context_fc; |
| 357 | g_string_assign(tgt_field_ref, "event.context"); |
| 358 | break; |
| 359 | case BT_SCOPE_EVENT_PAYLOAD: |
| 360 | BT_ASSERT(ctx->cur_ec); |
| 361 | fc = ctx->cur_ec->payload_fc; |
| 362 | g_string_assign(tgt_field_ref, "event.fields"); |
| 363 | break; |
| 364 | default: |
| 365 | abort(); |
| 366 | } |
| 367 | |
| 368 | BT_ASSERT(fc); |
| 369 | |
| 370 | for (i = 0; i < bt_field_path_get_item_count(tgt_ir_field_path); i++) { |
| 371 | const bt_field_path_item *fp_item = |
| 372 | bt_field_path_borrow_item_by_index_const( |
| 373 | tgt_ir_field_path, i); |
| 374 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
| 375 | |
| 376 | if (bt_field_path_item_get_type(fp_item) != |
| 377 | BT_FIELD_PATH_ITEM_TYPE_INDEX) { |
| 378 | /* Not supported by TSDL 1.8 */ |
| 379 | ret = -1; |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | switch (fc->type) { |
| 384 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 385 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 386 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 387 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 388 | (void *) fc, |
| 389 | bt_field_path_item_index_get_index(fp_item)); |
| 390 | break; |
| 391 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 392 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 393 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 394 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 395 | (void *) fc, |
| 396 | bt_field_path_item_index_get_index(fp_item)); |
| 397 | break; |
| 398 | default: |
| 399 | abort(); |
| 400 | } |
| 401 | |
| 402 | BT_ASSERT(named_fc); |
| 403 | g_string_append_c(tgt_field_ref, '.'); |
| 404 | g_string_append(tgt_field_ref, named_fc->name->str); |
| 405 | fc = named_fc->fc; |
| 406 | } |
| 407 | |
| 408 | end: |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Resolves a target field class located at `tgt_ir_field_path`, writing |
| 414 | * the resolved field ref to `tgt_field_ref` and setting |
| 415 | * `*create_before` according to whether or not the target field must be |
| 416 | * created immediately before (in which case `tgt_field_ref` is |
| 417 | * irrelevant). |
| 418 | */ |
| 419 | static |
| 420 | void resolve_field_class(struct ctx *ctx, |
| 421 | const bt_field_path *tgt_ir_field_path, |
| 422 | GString *tgt_field_ref, bool *create_before) |
| 423 | { |
| 424 | int ret; |
| 425 | bt_scope tgt_scope; |
| 426 | |
| 427 | *create_before = false; |
| 428 | |
| 429 | if (!tgt_ir_field_path) { |
| 430 | *create_before = true; |
| 431 | goto end; |
| 432 | } |
| 433 | |
| 434 | tgt_scope = bt_field_path_get_root_scope(tgt_ir_field_path); |
| 435 | |
| 436 | if (tgt_scope == ctx->cur_scope) { |
| 437 | /* |
| 438 | * Try, in this order: |
| 439 | * |
| 440 | * 1. Use a relative path, using only the target field |
| 441 | * class's name. This is what is the most commonly |
| 442 | * supported by popular CTF reading tools. |
| 443 | * |
| 444 | * 2. Use an absolute path. This could fail if there's |
| 445 | * an array field class from the current root's field |
| 446 | * class to the target field class. |
| 447 | * |
| 448 | * 3. Create the target field class before the |
| 449 | * requesting field class (fallback). |
| 450 | */ |
| 451 | ret = create_relative_field_ref(ctx, tgt_ir_field_path, |
| 452 | tgt_field_ref); |
| 453 | if (ret) { |
| 454 | ret = create_absolute_field_ref(ctx, tgt_ir_field_path, |
| 455 | tgt_field_ref); |
| 456 | if (ret) { |
| 457 | *create_before = true; |
| 458 | ret = 0; |
| 459 | goto end; |
| 460 | } |
| 461 | } |
| 462 | } else { |
| 463 | ret = create_absolute_field_ref(ctx, tgt_ir_field_path, |
| 464 | tgt_field_ref); |
| 465 | |
| 466 | /* It must always work in previous scopes */ |
| 467 | BT_ASSERT(ret == 0); |
| 468 | } |
| 469 | |
| 470 | end: |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | static |
| 475 | int translate_field_class(struct ctx *ctx); |
| 476 | |
| 477 | static inline |
| 478 | void append_to_parent_field_class(struct ctx *ctx, |
| 479 | struct fs_sink_ctf_field_class *fc) |
| 480 | { |
| 481 | struct fs_sink_ctf_field_class *parent_fc = |
| 482 | cur_path_stack_top(ctx)->parent_fc; |
| 483 | |
| 484 | switch (parent_fc->type) { |
| 485 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 486 | fs_sink_ctf_field_class_struct_append_member((void *) parent_fc, |
| 487 | cur_path_stack_top(ctx)->name->str, fc); |
| 488 | break; |
| 489 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 490 | fs_sink_ctf_field_class_variant_append_option((void *) parent_fc, |
| 491 | cur_path_stack_top(ctx)->name->str, fc); |
| 492 | break; |
| 493 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 494 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 495 | { |
| 496 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 497 | (void *) parent_fc; |
| 498 | |
| 499 | BT_ASSERT(!array_base_fc->elem_fc); |
| 500 | array_base_fc->elem_fc = fc; |
| 501 | array_base_fc->base.alignment = fc->alignment; |
| 502 | break; |
| 503 | } |
| 504 | default: |
| 505 | abort(); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static inline |
| 510 | void update_parent_field_class_alignment(struct ctx *ctx, |
| 511 | unsigned int alignment) |
| 512 | { |
| 513 | struct fs_sink_ctf_field_class *parent_fc = |
| 514 | cur_path_stack_top(ctx)->parent_fc; |
| 515 | |
| 516 | switch (parent_fc->type) { |
| 517 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 518 | fs_sink_ctf_field_class_struct_align_at_least( |
| 519 | (void *) parent_fc, alignment); |
| 520 | break; |
| 521 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 522 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 523 | { |
| 524 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 525 | (void *) parent_fc; |
| 526 | |
| 527 | array_base_fc->base.alignment = alignment; |
| 528 | break; |
| 529 | } |
| 530 | default: |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | static inline |
| 536 | int translate_structure_field_class_members(struct ctx *ctx, |
| 537 | struct fs_sink_ctf_field_class_struct *struct_fc, |
| 538 | const bt_field_class *ir_fc) |
| 539 | { |
| 540 | int ret = 0; |
| 541 | uint64_t i; |
| 542 | |
| 543 | for (i = 0; i < bt_field_class_structure_get_member_count(ir_fc); i++) { |
| 544 | const bt_field_class_structure_member *member; |
| 545 | const char *name; |
| 546 | const bt_field_class *memb_ir_fc; |
| 547 | |
| 548 | member = |
| 549 | bt_field_class_structure_borrow_member_by_index_const( |
| 550 | ir_fc, i); |
| 551 | name = bt_field_class_structure_member_get_name(member); |
| 552 | memb_ir_fc = bt_field_class_structure_member_borrow_field_class_const( |
| 553 | member); |
| 554 | ret = cur_path_stack_push(ctx, i, name, memb_ir_fc, |
| 555 | (void *) struct_fc); |
| 556 | if (ret) { |
| 557 | BT_COMP_LOGE("Cannot translate structure field class member: " |
| 558 | "name=\"%s\"", name); |
| 559 | goto end; |
| 560 | } |
| 561 | |
| 562 | ret = translate_field_class(ctx); |
| 563 | if (ret) { |
| 564 | BT_COMP_LOGE("Cannot translate structure field class member: " |
| 565 | "name=\"%s\"", name); |
| 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | cur_path_stack_pop(ctx); |
| 570 | } |
| 571 | |
| 572 | end: |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | static inline |
| 577 | int translate_structure_field_class(struct ctx *ctx) |
| 578 | { |
| 579 | int ret; |
| 580 | struct fs_sink_ctf_field_class_struct *fc = |
| 581 | fs_sink_ctf_field_class_struct_create_empty( |
| 582 | cur_path_stack_top(ctx)->ir_fc, |
| 583 | cur_path_stack_top(ctx)->index_in_parent); |
| 584 | |
| 585 | BT_ASSERT(fc); |
| 586 | append_to_parent_field_class(ctx, (void *) fc); |
| 587 | ret = translate_structure_field_class_members(ctx, fc, fc->base.ir_fc); |
| 588 | if (ret) { |
| 589 | goto end; |
| 590 | } |
| 591 | |
| 592 | update_parent_field_class_alignment(ctx, fc->base.alignment); |
| 593 | |
| 594 | end: |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | static inline |
| 599 | int translate_variant_field_class(struct ctx *ctx) |
| 600 | { |
| 601 | int ret = 0; |
| 602 | uint64_t i; |
| 603 | struct fs_sink_ctf_field_class_variant *fc = |
| 604 | fs_sink_ctf_field_class_variant_create_empty( |
| 605 | cur_path_stack_top(ctx)->ir_fc, |
| 606 | cur_path_stack_top(ctx)->index_in_parent); |
| 607 | |
| 608 | BT_ASSERT(fc); |
| 609 | |
| 610 | /* Resolve tag field class before appending to parent */ |
| 611 | resolve_field_class(ctx, |
| 612 | bt_field_class_variant_borrow_selector_field_path_const( |
| 613 | fc->base.ir_fc), fc->tag_ref, &fc->tag_is_before); |
| 614 | |
| 615 | append_to_parent_field_class(ctx, (void *) fc); |
| 616 | |
| 617 | for (i = 0; i < bt_field_class_variant_get_option_count(fc->base.ir_fc); |
| 618 | i++) { |
| 619 | const bt_field_class_variant_option *opt; |
| 620 | const char *name; |
| 621 | const bt_field_class *opt_ir_fc; |
| 622 | |
| 623 | opt = bt_field_class_variant_borrow_option_by_index_const( |
| 624 | fc->base.ir_fc, i); |
| 625 | name = bt_field_class_variant_option_get_name(opt); |
| 626 | opt_ir_fc = bt_field_class_variant_option_borrow_field_class_const( |
| 627 | opt); |
| 628 | ret = cur_path_stack_push(ctx, i, name, opt_ir_fc, (void *) fc); |
| 629 | if (ret) { |
| 630 | BT_COMP_LOGE("Cannot translate variant field class option: " |
| 631 | "name=\"%s\"", name); |
| 632 | goto end; |
| 633 | } |
| 634 | |
| 635 | ret = translate_field_class(ctx); |
| 636 | if (ret) { |
| 637 | BT_COMP_LOGE("Cannot translate variant field class option: " |
| 638 | "name=\"%s\"", name); |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | cur_path_stack_pop(ctx); |
| 643 | } |
| 644 | |
| 645 | end: |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | static inline |
| 650 | int translate_static_array_field_class(struct ctx *ctx) |
| 651 | { |
| 652 | struct fs_sink_ctf_field_class_array *fc = |
| 653 | fs_sink_ctf_field_class_array_create_empty( |
| 654 | cur_path_stack_top(ctx)->ir_fc, |
| 655 | cur_path_stack_top(ctx)->index_in_parent); |
| 656 | const bt_field_class *elem_ir_fc = |
| 657 | bt_field_class_array_borrow_element_field_class_const( |
| 658 | fc->base.base.ir_fc); |
| 659 | int ret; |
| 660 | |
| 661 | BT_ASSERT(fc); |
| 662 | append_to_parent_field_class(ctx, (void *) fc); |
| 663 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, elem_ir_fc, |
| 664 | (void *) fc); |
| 665 | if (ret) { |
| 666 | BT_COMP_LOGE_STR("Cannot translate static array field class element."); |
| 667 | goto end; |
| 668 | } |
| 669 | |
| 670 | ret = translate_field_class(ctx); |
| 671 | if (ret) { |
| 672 | BT_COMP_LOGE_STR("Cannot translate static array field class element."); |
| 673 | goto end; |
| 674 | } |
| 675 | |
| 676 | cur_path_stack_pop(ctx); |
| 677 | update_parent_field_class_alignment(ctx, fc->base.base.alignment); |
| 678 | |
| 679 | end: |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static inline |
| 684 | int translate_dynamic_array_field_class(struct ctx *ctx) |
| 685 | { |
| 686 | struct fs_sink_ctf_field_class_sequence *fc = |
| 687 | fs_sink_ctf_field_class_sequence_create_empty( |
| 688 | cur_path_stack_top(ctx)->ir_fc, |
| 689 | cur_path_stack_top(ctx)->index_in_parent); |
| 690 | const bt_field_class *elem_ir_fc = |
| 691 | bt_field_class_array_borrow_element_field_class_const( |
| 692 | fc->base.base.ir_fc); |
| 693 | int ret; |
| 694 | |
| 695 | BT_ASSERT(fc); |
| 696 | |
| 697 | /* Resolve length field class before appending to parent */ |
| 698 | resolve_field_class(ctx, |
| 699 | bt_field_class_dynamic_array_borrow_length_field_path_const( |
| 700 | fc->base.base.ir_fc), |
| 701 | fc->length_ref, &fc->length_is_before); |
| 702 | |
| 703 | append_to_parent_field_class(ctx, (void *) fc); |
| 704 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, elem_ir_fc, |
| 705 | (void *) fc); |
| 706 | if (ret) { |
| 707 | BT_COMP_LOGE_STR("Cannot translate dynamic array field class element."); |
| 708 | goto end; |
| 709 | } |
| 710 | |
| 711 | ret = translate_field_class(ctx); |
| 712 | if (ret) { |
| 713 | BT_COMP_LOGE_STR("Cannot translate dynamic array field class element."); |
| 714 | goto end; |
| 715 | } |
| 716 | |
| 717 | cur_path_stack_pop(ctx); |
| 718 | update_parent_field_class_alignment(ctx, fc->base.base.alignment); |
| 719 | |
| 720 | end: |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | static inline |
| 725 | int translate_integer_field_class(struct ctx *ctx) |
| 726 | { |
| 727 | struct fs_sink_ctf_field_class_int *fc = |
| 728 | fs_sink_ctf_field_class_int_create( |
| 729 | cur_path_stack_top(ctx)->ir_fc, |
| 730 | cur_path_stack_top(ctx)->index_in_parent); |
| 731 | |
| 732 | BT_ASSERT(fc); |
| 733 | append_to_parent_field_class(ctx, (void *) fc); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static inline |
| 738 | int translate_real_field_class(struct ctx *ctx) |
| 739 | { |
| 740 | struct fs_sink_ctf_field_class_float *fc = |
| 741 | fs_sink_ctf_field_class_float_create( |
| 742 | cur_path_stack_top(ctx)->ir_fc, |
| 743 | cur_path_stack_top(ctx)->index_in_parent); |
| 744 | |
| 745 | BT_ASSERT(fc); |
| 746 | append_to_parent_field_class(ctx, (void *) fc); |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static inline |
| 751 | int translate_string_field_class(struct ctx *ctx) |
| 752 | { |
| 753 | struct fs_sink_ctf_field_class_string *fc = |
| 754 | fs_sink_ctf_field_class_string_create( |
| 755 | cur_path_stack_top(ctx)->ir_fc, |
| 756 | cur_path_stack_top(ctx)->index_in_parent); |
| 757 | |
| 758 | BT_ASSERT(fc); |
| 759 | append_to_parent_field_class(ctx, (void *) fc); |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Translates a field class, recursively. |
| 765 | * |
| 766 | * The field class's IR field class, parent field class, and index |
| 767 | * within its parent are in the context's current path's top element |
| 768 | * (cur_path_stack_top()). |
| 769 | */ |
| 770 | static |
| 771 | int translate_field_class(struct ctx *ctx) |
| 772 | { |
| 773 | int ret; |
| 774 | |
| 775 | switch (bt_field_class_get_type(cur_path_stack_top(ctx)->ir_fc)) { |
| 776 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 777 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 778 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 779 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 780 | ret = translate_integer_field_class(ctx); |
| 781 | break; |
| 782 | case BT_FIELD_CLASS_TYPE_REAL: |
| 783 | ret = translate_real_field_class(ctx); |
| 784 | break; |
| 785 | case BT_FIELD_CLASS_TYPE_STRING: |
| 786 | ret = translate_string_field_class(ctx); |
| 787 | break; |
| 788 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 789 | ret = translate_structure_field_class(ctx); |
| 790 | break; |
| 791 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 792 | ret = translate_static_array_field_class(ctx); |
| 793 | break; |
| 794 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 795 | ret = translate_dynamic_array_field_class(ctx); |
| 796 | break; |
| 797 | case BT_FIELD_CLASS_TYPE_VARIANT: |
| 798 | ret = translate_variant_field_class(ctx); |
| 799 | break; |
| 800 | default: |
| 801 | abort(); |
| 802 | } |
| 803 | |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | static |
| 808 | int set_field_ref(struct fs_sink_ctf_field_class *fc, const char *fc_name, |
| 809 | struct fs_sink_ctf_field_class *parent_fc) |
| 810 | { |
| 811 | int ret = 0; |
| 812 | GString *field_ref = NULL; |
| 813 | bool is_before; |
| 814 | const char *tgt_type; |
| 815 | struct fs_sink_ctf_field_class_struct *parent_struct_fc = |
| 816 | (void *) parent_fc; |
| 817 | uint64_t i; |
| 818 | unsigned int suffix = 0; |
| 819 | |
| 820 | if (!fc_name || !parent_fc || |
| 821 | parent_fc->type != FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 822 | /* Not supported */ |
| 823 | ret = -1; |
| 824 | goto end; |
| 825 | } |
| 826 | |
| 827 | switch (fc->type) { |
| 828 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 829 | { |
| 830 | struct fs_sink_ctf_field_class_sequence *seq_fc = (void *) fc; |
| 831 | |
| 832 | field_ref = seq_fc->length_ref; |
| 833 | is_before = seq_fc->length_is_before; |
| 834 | tgt_type = "len"; |
| 835 | break; |
| 836 | } |
| 837 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 838 | { |
| 839 | struct fs_sink_ctf_field_class_variant *var_fc = (void *) fc; |
| 840 | |
| 841 | field_ref = var_fc->tag_ref; |
| 842 | is_before = var_fc->tag_is_before; |
| 843 | tgt_type = "tag"; |
| 844 | break; |
| 845 | } |
| 846 | default: |
| 847 | abort(); |
| 848 | } |
| 849 | |
| 850 | BT_ASSERT(field_ref); |
| 851 | |
| 852 | if (!is_before) { |
| 853 | goto end; |
| 854 | } |
| 855 | |
| 856 | /* Initial field ref */ |
| 857 | g_string_printf(field_ref, "__%s_%s", fc_name, tgt_type); |
| 858 | |
| 859 | /* |
| 860 | * Make sure field ref does not clash with an existing field |
| 861 | * class name within the same parent structure field class. |
| 862 | */ |
| 863 | while (true) { |
| 864 | bool name_ok = true; |
| 865 | |
| 866 | for (i = 0; i < parent_struct_fc->members->len; i++) { |
| 867 | struct fs_sink_ctf_named_field_class *named_fc = |
| 868 | fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 869 | parent_struct_fc, i); |
| 870 | |
| 871 | if (strcmp(field_ref->str, named_fc->name->str) == 0) { |
| 872 | /* Name clash */ |
| 873 | name_ok = false; |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if (name_ok) { |
| 879 | /* No clash: we're done */ |
| 880 | break; |
| 881 | } |
| 882 | |
| 883 | /* Append suffix and try again */ |
| 884 | g_string_printf(field_ref, "__%s_%s_%u", fc_name, tgt_type, |
| 885 | suffix); |
| 886 | suffix++; |
| 887 | } |
| 888 | |
| 889 | end: |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * This function recursively sets field refs of sequence and variant |
| 895 | * field classes when they are immediately before, avoiding name clashes |
| 896 | * with existing field class names. |
| 897 | * |
| 898 | * It can fail at this point if, for example, a sequence field class of |
| 899 | * which to set the length's field ref has something else than a |
| 900 | * structure field class as its parent: in this case, there's no |
| 901 | * location to place the length field class immediately before the |
| 902 | * sequence field class. |
| 903 | */ |
| 904 | static |
| 905 | int set_field_refs(struct fs_sink_ctf_field_class * const fc, |
| 906 | const char *fc_name, struct fs_sink_ctf_field_class *parent_fc) |
| 907 | { |
| 908 | int ret = 0; |
| 909 | enum fs_sink_ctf_field_class_type fc_type; |
| 910 | BT_ASSERT(fc); |
| 911 | |
| 912 | fc_type = fc->type; |
| 913 | |
| 914 | switch (fc_type) { |
| 915 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 916 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 917 | { |
| 918 | uint64_t i; |
| 919 | uint64_t len; |
| 920 | struct fs_sink_ctf_field_class_struct *struct_fc; |
| 921 | struct fs_sink_ctf_field_class_variant *var_fc = NULL; |
| 922 | struct fs_sink_ctf_named_field_class *named_fc; |
| 923 | |
| 924 | if (fc_type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 925 | struct_fc = (void *) fc; |
| 926 | len = struct_fc->members->len; |
| 927 | } else { |
| 928 | var_fc = (void *) fc; |
| 929 | len = var_fc->options->len; |
| 930 | ret = set_field_ref(fc, fc_name, parent_fc); |
| 931 | if (ret) { |
| 932 | goto end; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | for (i = 0; i < len; i++) { |
| 937 | if (fc_type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 938 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 939 | struct_fc, i); |
| 940 | } else { |
| 941 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
| 942 | var_fc, i); |
| 943 | } |
| 944 | |
| 945 | ret = set_field_refs(named_fc->fc, named_fc->name->str, |
| 946 | fc); |
| 947 | if (ret) { |
| 948 | goto end; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | break; |
| 953 | } |
| 954 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 955 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 956 | { |
| 957 | struct fs_sink_ctf_field_class_array_base *array_base_fc = |
| 958 | (void *) fc; |
| 959 | |
| 960 | if (fc_type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
| 961 | ret = set_field_ref(fc, fc_name, parent_fc); |
| 962 | if (ret) { |
| 963 | goto end; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | ret = set_field_refs(array_base_fc->elem_fc, NULL, fc); |
| 968 | if (ret) { |
| 969 | goto end; |
| 970 | } |
| 971 | |
| 972 | break; |
| 973 | } |
| 974 | default: |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | end: |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * This function translates a root scope trace IR field class to |
| 984 | * a CTF IR field class. |
| 985 | * |
| 986 | * The resulting CTF IR field class is written to `*fc` so that it |
| 987 | * exists as the parent object's (stream class or event class) true root |
| 988 | * field class during the recursive translation for resolving purposes. |
| 989 | * This is also why this function creates the empty structure field |
| 990 | * class and then calls translate_structure_field_class_members() to |
| 991 | * fill it. |
| 992 | */ |
| 993 | static |
| 994 | int translate_scope_field_class(struct ctx *ctx, bt_scope scope, |
| 995 | struct fs_sink_ctf_field_class **fc, |
| 996 | const bt_field_class *ir_fc) |
| 997 | { |
| 998 | int ret = 0; |
| 999 | |
| 1000 | if (!ir_fc) { |
| 1001 | goto end; |
| 1002 | } |
| 1003 | |
| 1004 | BT_ASSERT(bt_field_class_get_type(ir_fc) == |
| 1005 | BT_FIELD_CLASS_TYPE_STRUCTURE); |
| 1006 | BT_ASSERT(fc); |
| 1007 | *fc = (void *) fs_sink_ctf_field_class_struct_create_empty( |
| 1008 | ir_fc, UINT64_C(-1)); |
| 1009 | BT_ASSERT(*fc); |
| 1010 | ctx->cur_scope = scope; |
| 1011 | BT_ASSERT(ctx->cur_path->len == 0); |
| 1012 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, ir_fc, NULL); |
| 1013 | if (ret) { |
| 1014 | BT_COMP_LOGE("Cannot translate scope structure field class: " |
| 1015 | "scope=%d", scope); |
| 1016 | goto end; |
| 1017 | } |
| 1018 | |
| 1019 | ret = translate_structure_field_class_members(ctx, (void *) *fc, ir_fc); |
| 1020 | if (ret) { |
| 1021 | BT_COMP_LOGE("Cannot translate scope structure field class: " |
| 1022 | "scope=%d", scope); |
| 1023 | goto end; |
| 1024 | } |
| 1025 | |
| 1026 | cur_path_stack_pop(ctx); |
| 1027 | |
| 1028 | /* Set field refs for preceding targets */ |
| 1029 | ret = set_field_refs(*fc, NULL, NULL); |
| 1030 | |
| 1031 | end: |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | static inline |
| 1036 | void ctx_init(struct ctx *ctx, struct fs_sink_comp *fs_sink) |
| 1037 | { |
| 1038 | memset(ctx, 0, sizeof(struct ctx)); |
| 1039 | ctx->cur_path = g_array_new(FALSE, TRUE, |
| 1040 | sizeof(struct field_path_elem)); |
| 1041 | BT_ASSERT(ctx->cur_path); |
| 1042 | ctx->log_level = fs_sink->log_level; |
| 1043 | ctx->self_comp = fs_sink->self_comp; |
| 1044 | } |
| 1045 | |
| 1046 | static inline |
| 1047 | void ctx_fini(struct ctx *ctx) |
| 1048 | { |
| 1049 | if (ctx->cur_path) { |
| 1050 | g_array_free(ctx->cur_path, TRUE); |
| 1051 | ctx->cur_path = NULL; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | static |
| 1056 | int translate_event_class(struct fs_sink_comp *fs_sink, |
| 1057 | struct fs_sink_ctf_stream_class *sc, |
| 1058 | const bt_event_class *ir_ec, |
| 1059 | struct fs_sink_ctf_event_class **out_ec) |
| 1060 | { |
| 1061 | int ret = 0; |
| 1062 | struct ctx ctx; |
| 1063 | struct fs_sink_ctf_event_class *ec; |
| 1064 | |
| 1065 | BT_ASSERT(sc); |
| 1066 | BT_ASSERT(ir_ec); |
| 1067 | |
| 1068 | ctx_init(&ctx, fs_sink); |
| 1069 | ec = fs_sink_ctf_event_class_create(sc, ir_ec); |
| 1070 | BT_ASSERT(ec); |
| 1071 | ctx.cur_sc = sc; |
| 1072 | ctx.cur_ec = ec; |
| 1073 | ret = translate_scope_field_class(&ctx, BT_SCOPE_EVENT_SPECIFIC_CONTEXT, |
| 1074 | &ec->spec_context_fc, |
| 1075 | bt_event_class_borrow_specific_context_field_class_const( |
| 1076 | ir_ec)); |
| 1077 | if (ret) { |
| 1078 | goto end; |
| 1079 | } |
| 1080 | |
| 1081 | ret = translate_scope_field_class(&ctx, BT_SCOPE_EVENT_PAYLOAD, |
| 1082 | &ec->payload_fc, |
| 1083 | bt_event_class_borrow_payload_field_class_const(ir_ec)); |
| 1084 | if (ret) { |
| 1085 | goto end; |
| 1086 | } |
| 1087 | |
| 1088 | end: |
| 1089 | ctx_fini(&ctx); |
| 1090 | *out_ec = ec; |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | BT_HIDDEN |
| 1095 | int try_translate_event_class_trace_ir_to_ctf_ir( |
| 1096 | struct fs_sink_comp *fs_sink, |
| 1097 | struct fs_sink_ctf_stream_class *sc, |
| 1098 | const bt_event_class *ir_ec, |
| 1099 | struct fs_sink_ctf_event_class **out_ec) |
| 1100 | { |
| 1101 | int ret = 0; |
| 1102 | |
| 1103 | BT_ASSERT(sc); |
| 1104 | BT_ASSERT(ir_ec); |
| 1105 | |
| 1106 | /* Check in hash table first */ |
| 1107 | *out_ec = g_hash_table_lookup(sc->event_classes_from_ir, ir_ec); |
| 1108 | if (G_LIKELY(*out_ec)) { |
| 1109 | goto end; |
| 1110 | } |
| 1111 | |
| 1112 | ret = translate_event_class(fs_sink, sc, ir_ec, out_ec); |
| 1113 | |
| 1114 | end: |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
| 1118 | bool default_clock_class_name_exists(struct fs_sink_ctf_trace *trace, |
| 1119 | const char *name) |
| 1120 | { |
| 1121 | bool exists = false; |
| 1122 | uint64_t i; |
| 1123 | |
| 1124 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 1125 | struct fs_sink_ctf_stream_class *sc = |
| 1126 | trace->stream_classes->pdata[i]; |
| 1127 | |
| 1128 | if (sc->default_clock_class_name->len == 0) { |
| 1129 | /* No default clock class */ |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
| 1133 | if (strcmp(sc->default_clock_class_name->str, name) == 0) { |
| 1134 | exists = true; |
| 1135 | goto end; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | end: |
| 1140 | return exists; |
| 1141 | } |
| 1142 | |
| 1143 | static |
| 1144 | void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class *sc) |
| 1145 | { |
| 1146 | unsigned int suffix = 0; |
| 1147 | char buf[16]; |
| 1148 | |
| 1149 | g_string_assign(sc->default_clock_class_name, ""); |
| 1150 | sprintf(buf, "default"); |
| 1151 | |
| 1152 | while (default_clock_class_name_exists(sc->trace, buf)) { |
| 1153 | sprintf(buf, "default%u", suffix); |
| 1154 | suffix++; |
| 1155 | } |
| 1156 | |
| 1157 | g_string_assign(sc->default_clock_class_name, buf); |
| 1158 | } |
| 1159 | |
| 1160 | static |
| 1161 | int translate_stream_class(struct fs_sink_comp *fs_sink, |
| 1162 | struct fs_sink_ctf_trace *trace, |
| 1163 | const bt_stream_class *ir_sc, |
| 1164 | struct fs_sink_ctf_stream_class **out_sc) |
| 1165 | { |
| 1166 | int ret = 0; |
| 1167 | struct ctx ctx; |
| 1168 | |
| 1169 | BT_ASSERT(trace); |
| 1170 | BT_ASSERT(ir_sc); |
| 1171 | ctx_init(&ctx, fs_sink); |
| 1172 | *out_sc = fs_sink_ctf_stream_class_create(trace, ir_sc); |
| 1173 | BT_ASSERT(*out_sc); |
| 1174 | |
| 1175 | /* Set default clock class's protected name, if any */ |
| 1176 | if ((*out_sc)->default_clock_class) { |
| 1177 | const char *name = bt_clock_class_get_name( |
| 1178 | (*out_sc)->default_clock_class); |
| 1179 | |
| 1180 | if (name) { |
| 1181 | /* Try original name, protected */ |
| 1182 | g_string_assign((*out_sc)->default_clock_class_name, |
| 1183 | name); |
| 1184 | ret = fs_sink_ctf_protect_name( |
| 1185 | (*out_sc)->default_clock_class_name); |
| 1186 | if (ret) { |
| 1187 | /* Invalid: create a new name */ |
| 1188 | make_unique_default_clock_class_name(*out_sc); |
| 1189 | ret = 0; |
| 1190 | } |
| 1191 | } else { |
| 1192 | /* No name: create a name */ |
| 1193 | make_unique_default_clock_class_name(*out_sc); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | ctx.cur_sc = *out_sc; |
| 1198 | ret = translate_scope_field_class(&ctx, BT_SCOPE_PACKET_CONTEXT, |
| 1199 | &(*out_sc)->packet_context_fc, |
| 1200 | bt_stream_class_borrow_packet_context_field_class_const(ir_sc)); |
| 1201 | if (ret) { |
| 1202 | goto error; |
| 1203 | } |
| 1204 | |
| 1205 | if ((*out_sc)->packet_context_fc) { |
| 1206 | /* |
| 1207 | * Make sure the structure field class's alignment is |
| 1208 | * enough: 8 is what we use for our own special members |
| 1209 | * in the packet context. |
| 1210 | */ |
| 1211 | fs_sink_ctf_field_class_struct_align_at_least( |
| 1212 | (void *) (*out_sc)->packet_context_fc, 8); |
| 1213 | } |
| 1214 | |
| 1215 | ret = translate_scope_field_class(&ctx, BT_SCOPE_EVENT_COMMON_CONTEXT, |
| 1216 | &(*out_sc)->event_common_context_fc, |
| 1217 | bt_stream_class_borrow_event_common_context_field_class_const( |
| 1218 | ir_sc)); |
| 1219 | if (ret) { |
| 1220 | goto error; |
| 1221 | } |
| 1222 | |
| 1223 | goto end; |
| 1224 | |
| 1225 | error: |
| 1226 | fs_sink_ctf_stream_class_destroy(*out_sc); |
| 1227 | *out_sc = NULL; |
| 1228 | |
| 1229 | end: |
| 1230 | ctx_fini(&ctx); |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | BT_HIDDEN |
| 1235 | int try_translate_stream_class_trace_ir_to_ctf_ir( |
| 1236 | struct fs_sink_comp *fs_sink, |
| 1237 | struct fs_sink_ctf_trace *trace, |
| 1238 | const bt_stream_class *ir_sc, |
| 1239 | struct fs_sink_ctf_stream_class **out_sc) |
| 1240 | { |
| 1241 | int ret = 0; |
| 1242 | uint64_t i; |
| 1243 | |
| 1244 | BT_ASSERT(trace); |
| 1245 | BT_ASSERT(ir_sc); |
| 1246 | |
| 1247 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 1248 | *out_sc = trace->stream_classes->pdata[i]; |
| 1249 | |
| 1250 | if ((*out_sc)->ir_sc == ir_sc) { |
| 1251 | goto end; |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | ret = translate_stream_class(fs_sink, trace, ir_sc, out_sc); |
| 1256 | |
| 1257 | end: |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
| 1261 | BT_HIDDEN |
| 1262 | struct fs_sink_ctf_trace *translate_trace_trace_ir_to_ctf_ir( |
| 1263 | struct fs_sink_comp *fs_sink, const bt_trace *ir_trace) |
| 1264 | { |
| 1265 | uint64_t count; |
| 1266 | uint64_t i; |
| 1267 | struct fs_sink_ctf_trace *trace = NULL; |
| 1268 | |
| 1269 | /* Check that trace's environment is TSDL-compatible */ |
| 1270 | count = bt_trace_get_environment_entry_count(ir_trace); |
| 1271 | for (i = 0; i < count; i++) { |
| 1272 | const char *name; |
| 1273 | const bt_value *val; |
| 1274 | |
| 1275 | bt_trace_borrow_environment_entry_by_index_const( |
| 1276 | ir_trace, i, &name, &val); |
| 1277 | |
| 1278 | if (!fs_sink_ctf_ist_valid_identifier(name)) { |
| 1279 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, fs_sink->log_level, |
| 1280 | fs_sink->self_comp, |
| 1281 | "Unsupported trace class's environment entry name: " |
| 1282 | "name=\"%s\"", name); |
| 1283 | goto end; |
| 1284 | } |
| 1285 | |
| 1286 | switch (bt_value_get_type(val)) { |
| 1287 | case BT_VALUE_TYPE_SIGNED_INTEGER: |
| 1288 | case BT_VALUE_TYPE_STRING: |
| 1289 | break; |
| 1290 | default: |
| 1291 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, fs_sink->log_level, |
| 1292 | fs_sink->self_comp, |
| 1293 | "Unsupported trace class's environment entry value type: " |
| 1294 | "type=%s", |
| 1295 | bt_common_value_type_string( |
| 1296 | bt_value_get_type(val))); |
| 1297 | goto end; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | trace = fs_sink_ctf_trace_create(ir_trace); |
| 1302 | BT_ASSERT(trace); |
| 1303 | |
| 1304 | end: |
| 1305 | return trace; |
| 1306 | } |