| 1 | /* |
| 2 | * clock-fields.c |
| 3 | * |
| 4 | * Babeltrace - Update clock fields to write uint64 values |
| 5 | * |
| 6 | * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com> |
| 7 | * |
| 8 | * Author: Julien Desfossez <jdesfossez@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "PLUGIN-CTFCOPYTRACE-LIB-CLOCK-FIELDS" |
| 30 | #include "logging.h" |
| 31 | |
| 32 | #include <babeltrace/babeltrace.h> |
| 33 | #include <babeltrace/assert-internal.h> |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | #include "clock-field.h" |
| 37 | |
| 38 | static |
| 39 | int find_update_struct_clock_fields(FILE *err, struct bt_field_type *type, |
| 40 | const struct bt_clock_class *writer_clock_class); |
| 41 | static |
| 42 | int find_update_array_clock_fields(FILE *err, struct bt_field_type *type, |
| 43 | const struct bt_clock_class *writer_clock_class); |
| 44 | static |
| 45 | int find_update_enum_clock_fields(FILE *err, struct bt_field_type *type, |
| 46 | const struct bt_clock_class *writer_clock_class); |
| 47 | static |
| 48 | int find_update_sequence_clock_fields(FILE *err, struct bt_field_type *type, |
| 49 | const struct bt_clock_class *writer_clock_class); |
| 50 | static |
| 51 | int find_update_variant_clock_fields(FILE *err, struct bt_field_type *type, |
| 52 | const struct bt_clock_class *writer_clock_class); |
| 53 | |
| 54 | static |
| 55 | int copy_find_clock_int_field(FILE *err, |
| 56 | const struct bt_event *event, const struct bt_event *writer_event, |
| 57 | const struct bt_field *field, struct bt_field_type *type, |
| 58 | const struct bt_field *copy_field); |
| 59 | static |
| 60 | int copy_find_clock_struct_field(FILE *err, |
| 61 | const struct bt_event *event, const struct bt_event *writer_event, |
| 62 | const struct bt_field *field, struct bt_field_type *type, |
| 63 | const struct bt_field *copy_field); |
| 64 | static |
| 65 | int copy_find_clock_array_field(FILE *err, |
| 66 | const struct bt_event *event, const struct bt_event *writer_event, |
| 67 | const struct bt_field *field, struct bt_field_type *type, |
| 68 | const struct bt_field *copy_field); |
| 69 | static |
| 70 | int copy_find_clock_sequence_field(FILE *err, |
| 71 | const struct bt_event *event, const struct bt_event *writer_event, |
| 72 | const struct bt_field *field, struct bt_field_type *type, |
| 73 | const struct bt_field *copy_field); |
| 74 | static |
| 75 | int copy_find_clock_variant_field(FILE *err, const struct bt_event *event, |
| 76 | const struct bt_event *writer_event, const struct bt_field *field, |
| 77 | struct bt_field_type *type, const struct bt_field *copy_field); |
| 78 | static |
| 79 | int copy_find_clock_enum_field(FILE *err, const struct bt_event *event, |
| 80 | const struct bt_event *writer_event, const struct bt_field *field, |
| 81 | struct bt_field_type *type, const struct bt_field *copy_field); |
| 82 | |
| 83 | static |
| 84 | int update_header_clock_int_field_type(FILE *err, struct bt_field_type *type, |
| 85 | const struct bt_clock_class *writer_clock_class) |
| 86 | { |
| 87 | const struct bt_clock_class *clock = NULL; |
| 88 | int ret; |
| 89 | |
| 90 | clock = bt_field_type_integer_get_mapped_clock_class(type); |
| 91 | if (!clock) { |
| 92 | return 0; |
| 93 | } |
| 94 | BT_OBJECT_PUT_REF_AND_RESET(clock); |
| 95 | |
| 96 | ret = bt_field_type_integer_set_size(type, 64); |
| 97 | if (ret) { |
| 98 | BT_LOGE_STR("Failed to set integer size to 64."); |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | ret = bt_field_type_integer_set_mapped_clock_class(type, |
| 103 | writer_clock_class); |
| 104 | if (ret) { |
| 105 | BT_LOGE_STR("Failed to map integer to clock_class."); |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | end: |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | static |
| 114 | int find_update_clock_fields(FILE *err, struct bt_field_type *type, |
| 115 | const struct bt_clock_class *writer_clock_class) |
| 116 | { |
| 117 | int ret; |
| 118 | |
| 119 | switch (bt_field_type_get_type_id(type)) { |
| 120 | case BT_FIELD_TYPE_ID_INTEGER: |
| 121 | return update_header_clock_int_field_type(err, type, |
| 122 | writer_clock_class); |
| 123 | case BT_FIELD_TYPE_ID_STRUCT: |
| 124 | return find_update_struct_clock_fields(err, type, |
| 125 | writer_clock_class); |
| 126 | case BT_FIELD_TYPE_ID_ARRAY: |
| 127 | return find_update_array_clock_fields(err, type, |
| 128 | writer_clock_class); |
| 129 | case BT_FIELD_TYPE_ID_SEQUENCE: |
| 130 | return find_update_sequence_clock_fields(err, type, |
| 131 | writer_clock_class); |
| 132 | case BT_FIELD_TYPE_ID_VARIANT: |
| 133 | return find_update_variant_clock_fields(err, type, |
| 134 | writer_clock_class); |
| 135 | case BT_FIELD_TYPE_ID_ENUM: |
| 136 | return find_update_enum_clock_fields(err, type, |
| 137 | writer_clock_class); |
| 138 | break; |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | ret = 0; |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static |
| 149 | int find_update_variant_clock_fields(FILE *err, struct bt_field_type *type, |
| 150 | const struct bt_clock_class *writer_clock_class) |
| 151 | { |
| 152 | int count, i, ret; |
| 153 | struct bt_field_type *entry_type = NULL; |
| 154 | |
| 155 | count = bt_field_type_variant_get_field_count(type); |
| 156 | for (i = 0; i < count; i++) { |
| 157 | const char *entry_name; |
| 158 | |
| 159 | ret = bt_field_type_variant_get_field_by_index(type, |
| 160 | &entry_name, &entry_type, i); |
| 161 | if (ret) { |
| 162 | BT_LOGE_STR("Failed to get variant field."); |
| 163 | goto error; |
| 164 | } |
| 165 | |
| 166 | ret = find_update_clock_fields(err, entry_type, |
| 167 | writer_clock_class); |
| 168 | if (ret) { |
| 169 | BT_LOGE_STR("Failed to find clock fields."); |
| 170 | goto error; |
| 171 | } |
| 172 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
| 173 | } |
| 174 | |
| 175 | ret = 0; |
| 176 | goto end; |
| 177 | |
| 178 | error: |
| 179 | ret = -1; |
| 180 | end: |
| 181 | bt_object_put_ref(entry_type); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static |
| 186 | int find_update_struct_clock_fields(FILE *err, struct bt_field_type *type, |
| 187 | const struct bt_clock_class *writer_clock_class) |
| 188 | { |
| 189 | int count, i, ret; |
| 190 | struct bt_field_type *entry_type = NULL; |
| 191 | |
| 192 | count = bt_field_type_structure_get_field_count(type); |
| 193 | for (i = 0; i < count; i++) { |
| 194 | const char *entry_name; |
| 195 | |
| 196 | ret = bt_field_type_structure_get_field_by_index(type, |
| 197 | &entry_name, &entry_type, i); |
| 198 | if (ret) { |
| 199 | BT_LOGE_STR("Failed to get struct field."); |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | ret = find_update_clock_fields(err, entry_type, |
| 204 | writer_clock_class); |
| 205 | if (ret) { |
| 206 | BT_LOGE_STR("Failed to find clock fields."); |
| 207 | goto error; |
| 208 | } |
| 209 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
| 210 | } |
| 211 | |
| 212 | ret = 0; |
| 213 | goto end; |
| 214 | |
| 215 | error: |
| 216 | bt_object_put_ref(entry_type); |
| 217 | end: |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static |
| 222 | int find_update_sequence_clock_fields(FILE *err, struct bt_field_type *type, |
| 223 | const struct bt_clock_class *writer_clock_class) |
| 224 | { |
| 225 | int ret; |
| 226 | struct bt_field_type *entry_type = NULL; |
| 227 | |
| 228 | entry_type = bt_field_type_sequence_get_element_type(type); |
| 229 | BT_ASSERT(entry_type); |
| 230 | |
| 231 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
| 232 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
| 233 | if (ret) { |
| 234 | BT_LOGE_STR("Failed to find clock fields."); |
| 235 | goto error; |
| 236 | } |
| 237 | |
| 238 | ret = 0; |
| 239 | goto end; |
| 240 | |
| 241 | error: |
| 242 | ret = -1; |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static |
| 248 | int find_update_array_clock_fields(FILE *err, struct bt_field_type *type, |
| 249 | const struct bt_clock_class *writer_clock_class) |
| 250 | { |
| 251 | int ret = 0; |
| 252 | struct bt_field_type *entry_type = NULL; |
| 253 | |
| 254 | entry_type = bt_field_type_array_get_element_type(type); |
| 255 | BT_ASSERT(entry_type); |
| 256 | |
| 257 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
| 258 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
| 259 | if (ret) { |
| 260 | BT_LOGE_STR("Failed to find clock fields."); |
| 261 | ret = -1; |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 265 | end: |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static |
| 270 | int find_update_enum_clock_fields(FILE *err, struct bt_field_type *type, |
| 271 | const struct bt_clock_class *writer_clock_class) |
| 272 | { |
| 273 | int ret; |
| 274 | struct bt_field_type *entry_type = NULL; |
| 275 | |
| 276 | entry_type = bt_field_type_enumeration_get_container_type(type); |
| 277 | BT_ASSERT(entry_type); |
| 278 | |
| 279 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
| 280 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
| 281 | if (ret) { |
| 282 | BT_LOGE_STR("Failed to find clock fields."); |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | ret = 0; |
| 287 | goto end; |
| 288 | |
| 289 | error: |
| 290 | ret = -1; |
| 291 | end: |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | BT_HIDDEN |
| 296 | struct bt_field_type *override_header_type(FILE *err, |
| 297 | struct bt_field_type *type, |
| 298 | const struct bt_trace *writer_trace) |
| 299 | { |
| 300 | struct bt_field_type *new_type = NULL; |
| 301 | const struct bt_clock_class *writer_clock_class = NULL; |
| 302 | int ret; |
| 303 | |
| 304 | /* FIXME multi-clock? */ |
| 305 | writer_clock_class = bt_trace_get_clock_class_by_index(writer_trace, 0); |
| 306 | BT_ASSERT(writer_clock_class); |
| 307 | |
| 308 | new_type = bt_field_type_copy(type); |
| 309 | if (!new_type) { |
| 310 | BT_LOGE_STR("Failed to copy field type."); |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | if (bt_field_type_get_type_id(new_type) != BT_FIELD_TYPE_ID_STRUCT) { |
| 315 | BT_LOGE("Expected header field type to be struct: type=%d", |
| 316 | bt_field_type_get_type_id(new_type)); |
| 317 | goto error; |
| 318 | } |
| 319 | |
| 320 | ret = find_update_struct_clock_fields(err, new_type, writer_clock_class); |
| 321 | if (ret) { |
| 322 | BT_LOGE_STR("Failed to find clock fields in struct."); |
| 323 | goto error; |
| 324 | } |
| 325 | BT_OBJECT_PUT_REF_AND_RESET(writer_clock_class); |
| 326 | |
| 327 | goto end; |
| 328 | |
| 329 | error: |
| 330 | bt_object_put_ref(writer_clock_class); |
| 331 | BT_OBJECT_PUT_REF_AND_RESET(new_type); |
| 332 | end: |
| 333 | return new_type; |
| 334 | } |
| 335 | |
| 336 | static |
| 337 | int copy_float_field(FILE *err, const struct bt_field *field, |
| 338 | struct bt_field_type *type, |
| 339 | const struct bt_field *copy_field) |
| 340 | { |
| 341 | double value; |
| 342 | int ret; |
| 343 | |
| 344 | ret = bt_field_floating_point_get_value(field, &value); |
| 345 | if (ret) { |
| 346 | BT_LOGE_STR("Failed to get value."); |
| 347 | goto error; |
| 348 | } |
| 349 | |
| 350 | ret = bt_field_floating_point_set_value(copy_field, value); |
| 351 | if (ret) { |
| 352 | ret = -1; |
| 353 | BT_LOGE_STR("Failed to set floating point value."); |
| 354 | goto end; |
| 355 | } |
| 356 | |
| 357 | ret = 0; |
| 358 | goto end; |
| 359 | |
| 360 | error: |
| 361 | ret = -1; |
| 362 | end: |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static |
| 367 | int copy_string_field(FILE *err, const struct bt_field *field, |
| 368 | struct bt_field_type *type, |
| 369 | const struct bt_field *copy_field) |
| 370 | { |
| 371 | const char *value; |
| 372 | int ret; |
| 373 | |
| 374 | value = bt_field_string_get_value(field); |
| 375 | if (!value) { |
| 376 | BT_LOGE_STR("Failed to get value."); |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | ret = bt_field_string_set_value(copy_field, value); |
| 381 | if (ret) { |
| 382 | ret = -1; |
| 383 | BT_LOGE_STR("Failed to set string value."); |
| 384 | goto end; |
| 385 | } |
| 386 | |
| 387 | ret = 0; |
| 388 | goto end; |
| 389 | |
| 390 | error: |
| 391 | ret = -1; |
| 392 | end: |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | BT_HIDDEN |
| 397 | int copy_override_field(FILE *err, const struct bt_event *event, |
| 398 | const struct bt_event *writer_event, const struct bt_field *field, |
| 399 | const struct bt_field *copy_field) |
| 400 | { |
| 401 | struct bt_field_type *type = NULL; |
| 402 | int ret = 0; |
| 403 | |
| 404 | type = bt_field_get_type(field); |
| 405 | BT_ASSERT(type); |
| 406 | |
| 407 | switch (bt_field_type_get_type_id(type)) { |
| 408 | case BT_FIELD_TYPE_ID_INTEGER: |
| 409 | ret = copy_find_clock_int_field(err, event, writer_event, |
| 410 | field, type, copy_field); |
| 411 | break; |
| 412 | case BT_FIELD_TYPE_ID_STRUCT: |
| 413 | ret = copy_find_clock_struct_field(err, event, writer_event, |
| 414 | field, type, copy_field); |
| 415 | break; |
| 416 | case BT_FIELD_TYPE_ID_FLOAT: |
| 417 | ret = copy_float_field(err, field, type, copy_field); |
| 418 | break; |
| 419 | case BT_FIELD_TYPE_ID_ENUM: |
| 420 | ret = copy_find_clock_enum_field(err, event, writer_event, |
| 421 | field, type, copy_field); |
| 422 | break; |
| 423 | case BT_FIELD_TYPE_ID_STRING: |
| 424 | ret = copy_string_field(err, field, type, copy_field); |
| 425 | break; |
| 426 | case BT_FIELD_TYPE_ID_ARRAY: |
| 427 | ret = copy_find_clock_array_field(err, event, writer_event, |
| 428 | field, type, copy_field); |
| 429 | break; |
| 430 | case BT_FIELD_TYPE_ID_SEQUENCE: |
| 431 | ret = copy_find_clock_sequence_field(err, event, writer_event, |
| 432 | field, type, copy_field); |
| 433 | break; |
| 434 | case BT_FIELD_TYPE_ID_VARIANT: |
| 435 | ret = copy_find_clock_variant_field(err, event, writer_event, |
| 436 | field, type, copy_field); |
| 437 | break; |
| 438 | /* No default, we want to catch missing field types. */ |
| 439 | case BT_FIELD_TYPE_ID_UNKNOWN: |
| 440 | case BT_FIELD_TYPE_ID_NR: |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | BT_OBJECT_PUT_REF_AND_RESET(type); |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | static |
| 450 | int copy_find_clock_enum_field(FILE *err, const struct bt_event *event, |
| 451 | const struct bt_event *writer_event, const struct bt_field *field, |
| 452 | struct bt_field_type *type, const struct bt_field *copy_field) |
| 453 | { |
| 454 | int ret; |
| 455 | const struct bt_field *container = NULL, *copy_container = NULL; |
| 456 | |
| 457 | container = bt_field_enumeration_get_container(field); |
| 458 | BT_ASSERT(container); |
| 459 | |
| 460 | copy_container = bt_field_enumeration_get_container(copy_field); |
| 461 | BT_ASSERT(copy_container); |
| 462 | |
| 463 | ret = copy_override_field(err, event, writer_event, container, |
| 464 | copy_container); |
| 465 | if (ret) { |
| 466 | ret = -1; |
| 467 | BT_LOGE_STR("Failed to override enum field."); |
| 468 | goto error; |
| 469 | } |
| 470 | |
| 471 | ret = 0; |
| 472 | goto end; |
| 473 | |
| 474 | error: |
| 475 | ret = -1; |
| 476 | end: |
| 477 | bt_object_put_ref(copy_container); |
| 478 | bt_object_put_ref(container); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static |
| 483 | int copy_find_clock_variant_field(FILE *err, const struct bt_event *event, |
| 484 | const struct bt_event *writer_event, const struct bt_field *field, |
| 485 | struct bt_field_type *type, const struct bt_field *copy_field) |
| 486 | { |
| 487 | int ret; |
| 488 | const struct bt_field *tag = NULL; |
| 489 | const struct bt_field *variant_field = NULL, *copy_variant_field = NULL; |
| 490 | |
| 491 | tag = bt_field_variant_get_tag(field); |
| 492 | BT_ASSERT(tag); |
| 493 | |
| 494 | variant_field = bt_field_variant_get_field(field, tag); |
| 495 | if (!variant_field) { |
| 496 | BT_LOGE_STR("Failed to get variant field."); |
| 497 | goto error; |
| 498 | } |
| 499 | |
| 500 | copy_variant_field = bt_field_variant_get_field(copy_field, tag); |
| 501 | BT_ASSERT(copy_variant_field); |
| 502 | |
| 503 | ret = copy_override_field(err, event, writer_event, variant_field, |
| 504 | copy_variant_field); |
| 505 | if (ret) { |
| 506 | BT_LOGE_STR("Failed to override variant field."); |
| 507 | goto error; |
| 508 | } |
| 509 | |
| 510 | ret = 0; |
| 511 | goto end; |
| 512 | |
| 513 | error: |
| 514 | ret = -1; |
| 515 | end: |
| 516 | bt_object_put_ref(copy_variant_field); |
| 517 | bt_object_put_ref(variant_field); |
| 518 | bt_object_put_ref(tag); |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | static |
| 523 | int copy_find_clock_sequence_field(FILE *err, |
| 524 | const struct bt_event *event, const struct bt_event *writer_event, |
| 525 | const struct bt_field *field, struct bt_field_type *type, |
| 526 | const struct bt_field *copy_field) |
| 527 | { |
| 528 | int ret; |
| 529 | uint64_t i, count; |
| 530 | const struct bt_field *length_field = NULL; |
| 531 | const struct bt_field *entry_field = NULL, *entry_copy = NULL; |
| 532 | |
| 533 | length_field = bt_field_sequence_get_length(field); |
| 534 | BT_ASSERT(length_field); |
| 535 | |
| 536 | ret = bt_field_unsigned_integer_get_value(length_field, &count); |
| 537 | if (ret) { |
| 538 | BT_LOGE("Failed to get value."); |
| 539 | goto error; |
| 540 | } |
| 541 | |
| 542 | ret = bt_field_sequence_set_length(copy_field, length_field); |
| 543 | if (ret) { |
| 544 | BT_LOGE_STR("Failed to set sequence length."); |
| 545 | goto error; |
| 546 | } |
| 547 | BT_OBJECT_PUT_REF_AND_RESET(length_field); |
| 548 | |
| 549 | for (i = 0; i < count; i++) { |
| 550 | entry_field = bt_field_sequence_get_field(field, i); |
| 551 | if (!entry_field) { |
| 552 | BT_LOGE_STR("Failed to get sequence field."); |
| 553 | goto error; |
| 554 | } |
| 555 | |
| 556 | entry_copy = bt_field_sequence_get_field(copy_field, i); |
| 557 | BT_ASSERT(entry_copy); |
| 558 | |
| 559 | ret = copy_override_field(err, event, writer_event, entry_field, |
| 560 | entry_copy); |
| 561 | if (ret) { |
| 562 | BT_LOGE_STR("Faield to override field in sequence."); |
| 563 | goto error; |
| 564 | } |
| 565 | BT_OBJECT_PUT_REF_AND_RESET(entry_field); |
| 566 | BT_OBJECT_PUT_REF_AND_RESET(entry_copy); |
| 567 | } |
| 568 | |
| 569 | ret = 0; |
| 570 | goto end; |
| 571 | |
| 572 | error: |
| 573 | bt_object_put_ref(length_field); |
| 574 | bt_object_put_ref(entry_field); |
| 575 | bt_object_put_ref(entry_copy); |
| 576 | ret = -1; |
| 577 | end: |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | static |
| 582 | int copy_find_clock_array_field(FILE *err, |
| 583 | const struct bt_event *event, const struct bt_event *writer_event, |
| 584 | const struct bt_field *field, struct bt_field_type *type, |
| 585 | const struct bt_field *copy_field) |
| 586 | { |
| 587 | int ret, count, i; |
| 588 | const struct bt_field *entry_field = NULL, *entry_copy = NULL; |
| 589 | |
| 590 | count = bt_field_type_array_get_length(type); |
| 591 | for (i = 0; i < count; i++) { |
| 592 | entry_field = bt_field_array_get_field(field, i); |
| 593 | if (!entry_field) { |
| 594 | ret = -1; |
| 595 | BT_LOGE_STR("Failed to get array field."); |
| 596 | goto error; |
| 597 | } |
| 598 | |
| 599 | entry_copy = bt_field_array_get_field(copy_field, i); |
| 600 | BT_ASSERT(entry_copy); |
| 601 | |
| 602 | ret = copy_override_field(err, event, writer_event, entry_field, |
| 603 | entry_copy); |
| 604 | if (ret) { |
| 605 | ret = -1; |
| 606 | BT_LOGE_STR("Failed to override field in array."); |
| 607 | goto error; |
| 608 | } |
| 609 | BT_OBJECT_PUT_REF_AND_RESET(entry_field); |
| 610 | BT_OBJECT_PUT_REF_AND_RESET(entry_copy); |
| 611 | } |
| 612 | |
| 613 | ret = 0; |
| 614 | goto end; |
| 615 | |
| 616 | error: |
| 617 | bt_object_put_ref(entry_field); |
| 618 | bt_object_put_ref(entry_copy); |
| 619 | |
| 620 | end: |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | static |
| 625 | int copy_find_clock_struct_field(FILE *err, |
| 626 | const struct bt_event *event, const struct bt_event *writer_event, |
| 627 | const struct bt_field *field, struct bt_field_type *type, |
| 628 | const struct bt_field *copy_field) |
| 629 | { |
| 630 | int count, i, ret; |
| 631 | struct bt_field_type *entry_type = NULL; |
| 632 | const struct bt_field *entry_field = NULL, *entry_copy = NULL; |
| 633 | |
| 634 | count = bt_field_type_structure_get_field_count(type); |
| 635 | for (i = 0; i < count; i++) { |
| 636 | const char *entry_name; |
| 637 | |
| 638 | entry_field = bt_field_structure_get_field_by_index(field, i); |
| 639 | if (!entry_field) { |
| 640 | BT_LOGE_STR("Failed to get struct field."); |
| 641 | goto error; |
| 642 | } |
| 643 | |
| 644 | ret = bt_field_type_structure_get_field_by_index(type, &entry_name, |
| 645 | &entry_type, i); |
| 646 | if (ret) { |
| 647 | BT_LOGE_STR("Failed to get struct field."); |
| 648 | goto error; |
| 649 | } |
| 650 | |
| 651 | entry_copy = bt_field_structure_get_field_by_index(copy_field, i); |
| 652 | BT_ASSERT(entry_copy); |
| 653 | |
| 654 | ret = copy_override_field(err, event, writer_event, entry_field, |
| 655 | entry_copy); |
| 656 | if (ret) { |
| 657 | BT_LOGE_STR("Failed to override field in struct."); |
| 658 | goto error; |
| 659 | } |
| 660 | BT_OBJECT_PUT_REF_AND_RESET(entry_copy); |
| 661 | BT_OBJECT_PUT_REF_AND_RESET(entry_field); |
| 662 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
| 663 | } |
| 664 | |
| 665 | ret = 0; |
| 666 | goto end; |
| 667 | |
| 668 | error: |
| 669 | bt_object_put_ref(entry_type); |
| 670 | bt_object_put_ref(entry_field); |
| 671 | bt_object_put_ref(entry_copy); |
| 672 | ret = -1; |
| 673 | end: |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static |
| 678 | int set_int_value(FILE *err, const struct bt_field *field, |
| 679 | const struct bt_field *copy_field, |
| 680 | struct bt_field_type *type) |
| 681 | { |
| 682 | uint64_t uvalue; |
| 683 | int64_t value; |
| 684 | int ret; |
| 685 | |
| 686 | if (bt_field_type_integer_is_signed(type)) { |
| 687 | ret = bt_field_signed_integer_get_value(field, &value); |
| 688 | if (ret) { |
| 689 | BT_LOGE("Failed to get value."); |
| 690 | goto error; |
| 691 | } |
| 692 | |
| 693 | ret = bt_field_signed_integer_set_value(copy_field, value); |
| 694 | if (ret) { |
| 695 | ret = -1; |
| 696 | BT_LOGE_STR("Failed to set signed integer value."); |
| 697 | goto end; |
| 698 | } |
| 699 | } else { |
| 700 | ret = bt_field_unsigned_integer_get_value(field, |
| 701 | &uvalue); |
| 702 | if (ret) { |
| 703 | BT_LOGE("Failed to get value."); |
| 704 | goto error; |
| 705 | } |
| 706 | |
| 707 | ret = bt_field_unsigned_integer_set_value(copy_field, uvalue); |
| 708 | if (ret) { |
| 709 | ret = -1; |
| 710 | BT_LOGE_STR("Failed to set unsigned integer value."); |
| 711 | goto end; |
| 712 | } |
| 713 | } |
| 714 | ret = 0; |
| 715 | goto end; |
| 716 | |
| 717 | error: |
| 718 | ret = -1; |
| 719 | end: |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | const struct bt_clock_class *stream_class_get_clock_class(FILE *err, |
| 724 | const struct bt_stream_class *stream_class) |
| 725 | { |
| 726 | const struct bt_trace *trace = NULL; |
| 727 | const struct bt_clock_class *clock_class = NULL; |
| 728 | |
| 729 | trace = bt_stream_class_get_trace(stream_class); |
| 730 | BT_ASSERT(trace); |
| 731 | |
| 732 | /* FIXME multi-clock? */ |
| 733 | clock_class = bt_trace_get_clock_class_by_index(trace, 0); |
| 734 | |
| 735 | bt_object_put_ref(trace); |
| 736 | |
| 737 | return clock_class; |
| 738 | } |
| 739 | |
| 740 | const struct bt_clock_class *event_get_clock_class(FILE *err, const struct bt_event *event) |
| 741 | { |
| 742 | const struct bt_event_class *event_class = NULL; |
| 743 | const struct bt_stream_class *stream_class = NULL; |
| 744 | const struct bt_clock_class *clock_class = NULL; |
| 745 | |
| 746 | event_class = bt_event_get_class(event); |
| 747 | BT_ASSERT(event_class); |
| 748 | |
| 749 | stream_class = bt_event_class_get_stream_class(event_class); |
| 750 | BT_OBJECT_PUT_REF_AND_RESET(event_class); |
| 751 | BT_ASSERT(stream_class); |
| 752 | |
| 753 | clock_class = stream_class_get_clock_class(err, stream_class); |
| 754 | bt_object_put_ref(stream_class); |
| 755 | |
| 756 | return clock_class; |
| 757 | } |
| 758 | |
| 759 | static |
| 760 | int copy_find_clock_int_field(FILE *err, |
| 761 | const struct bt_event *event, const struct bt_event *writer_event, |
| 762 | const struct bt_field *field, struct bt_field_type *type, |
| 763 | const struct bt_field *copy_field) |
| 764 | { |
| 765 | const struct bt_clock_class *clock_class = NULL, *writer_clock_class = NULL; |
| 766 | struct bt_clock_value *clock_value = NULL, *writer_clock_value = NULL; |
| 767 | uint64_t value; |
| 768 | int ret; |
| 769 | |
| 770 | clock_class = bt_field_type_integer_get_mapped_clock_class(type); |
| 771 | if (!clock_class) { |
| 772 | return set_int_value(err, field, copy_field, type); |
| 773 | } |
| 774 | |
| 775 | clock_value = bt_event_get_clock_value(event, clock_class); |
| 776 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
| 777 | BT_ASSERT(clock_value); |
| 778 | |
| 779 | ret = bt_clock_value_get_value(clock_value, &value); |
| 780 | BT_OBJECT_PUT_REF_AND_RESET(clock_value); |
| 781 | if (ret) { |
| 782 | BT_LOGE("Failed to get clock value."); |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | ret = bt_field_unsigned_integer_set_value(copy_field, value); |
| 787 | if (ret) { |
| 788 | BT_LOGE_STR("Failed to set unsigned integer value."); |
| 789 | goto error; |
| 790 | } |
| 791 | |
| 792 | writer_clock_class = event_get_clock_class(err, writer_event); |
| 793 | BT_ASSERT(writer_clock_class); |
| 794 | |
| 795 | writer_clock_value = bt_clock_value_create(writer_clock_class, value); |
| 796 | BT_OBJECT_PUT_REF_AND_RESET(writer_clock_class); |
| 797 | if (!writer_clock_value) { |
| 798 | BT_LOGE_STR("Failed to create clock value."); |
| 799 | goto error; |
| 800 | } |
| 801 | |
| 802 | ret = bt_event_set_clock_value(writer_event, writer_clock_value); |
| 803 | BT_OBJECT_PUT_REF_AND_RESET(writer_clock_value); |
| 804 | if (ret) { |
| 805 | BT_LOGE_STR("Failed to set clock value."); |
| 806 | goto error; |
| 807 | } |
| 808 | |
| 809 | ret = 0; |
| 810 | goto end; |
| 811 | |
| 812 | error: |
| 813 | ret = -1; |
| 814 | end: |
| 815 | return ret; |
| 816 | } |
| 817 | |