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