| 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 | #include <babeltrace/ctf-ir/event.h> |
| 30 | #include <babeltrace/ctf-ir/packet.h> |
| 31 | #include <babeltrace/ctf-ir/event-class.h> |
| 32 | #include <babeltrace/ctf-ir/stream.h> |
| 33 | #include <babeltrace/ctf-ir/stream-class.h> |
| 34 | #include <babeltrace/ctf-ir/clock-class.h> |
| 35 | #include <babeltrace/ctf-ir/fields.h> |
| 36 | #include <babeltrace/ctf-writer/stream.h> |
| 37 | #include <babeltrace/ctf-ir/field-types.h> |
| 38 | #include <assert.h> |
| 39 | #include <stdio.h> |
| 40 | |
| 41 | #include "clock-fields.h" |
| 42 | |
| 43 | static |
| 44 | int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 45 | struct bt_ctf_clock_class *writer_clock_class); |
| 46 | static |
| 47 | int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 48 | struct bt_ctf_clock_class *writer_clock_class); |
| 49 | static |
| 50 | int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 51 | struct bt_ctf_clock_class *writer_clock_class); |
| 52 | static |
| 53 | int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 54 | struct bt_ctf_clock_class *writer_clock_class); |
| 55 | static |
| 56 | int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 57 | struct bt_ctf_clock_class *writer_clock_class); |
| 58 | |
| 59 | static |
| 60 | int copy_find_clock_int_field(FILE *err, |
| 61 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 62 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 63 | struct bt_ctf_field *copy_field); |
| 64 | static |
| 65 | int copy_find_clock_struct_field(FILE *err, |
| 66 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 67 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 68 | struct bt_ctf_field *copy_field); |
| 69 | static |
| 70 | int copy_find_clock_array_field(FILE *err, |
| 71 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 72 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 73 | struct bt_ctf_field *copy_field); |
| 74 | static |
| 75 | int copy_find_clock_sequence_field(FILE *err, |
| 76 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 77 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 78 | struct bt_ctf_field *copy_field); |
| 79 | static |
| 80 | int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event, |
| 81 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, |
| 82 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field); |
| 83 | static |
| 84 | int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event, |
| 85 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, |
| 86 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field); |
| 87 | |
| 88 | static |
| 89 | int update_header_clock_int_field_type(FILE *err, struct bt_ctf_field_type *type, |
| 90 | struct bt_ctf_clock_class *writer_clock_class) |
| 91 | { |
| 92 | struct bt_ctf_clock_class *clock = NULL; |
| 93 | int ret; |
| 94 | |
| 95 | clock = bt_ctf_field_type_integer_get_mapped_clock_class(type); |
| 96 | if (!clock) { |
| 97 | return 0; |
| 98 | } |
| 99 | BT_PUT(clock); |
| 100 | |
| 101 | ret = bt_ctf_field_type_integer_set_size(type, 64); |
| 102 | if (ret) { |
| 103 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 104 | __LINE__); |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | ret = bt_ctf_field_type_integer_set_mapped_clock_class(type, |
| 109 | writer_clock_class); |
| 110 | if (ret) { |
| 111 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 112 | __LINE__); |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | end: |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static |
| 121 | int find_update_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 122 | struct bt_ctf_clock_class *writer_clock_class) |
| 123 | { |
| 124 | int ret; |
| 125 | |
| 126 | switch (bt_ctf_field_type_get_type_id(type)) { |
| 127 | case BT_CTF_TYPE_ID_INTEGER: |
| 128 | return update_header_clock_int_field_type(err, type, |
| 129 | writer_clock_class); |
| 130 | case BT_CTF_TYPE_ID_STRUCT: |
| 131 | return find_update_struct_clock_fields(err, type, |
| 132 | writer_clock_class); |
| 133 | case BT_CTF_TYPE_ID_ARRAY: |
| 134 | return find_update_array_clock_fields(err, type, |
| 135 | writer_clock_class); |
| 136 | case BT_CTF_TYPE_ID_SEQUENCE: |
| 137 | return find_update_sequence_clock_fields(err, type, |
| 138 | writer_clock_class); |
| 139 | case BT_CTF_TYPE_ID_UNTAGGED_VARIANT: |
| 140 | case BT_CTF_TYPE_ID_VARIANT: |
| 141 | return find_update_variant_clock_fields(err, type, |
| 142 | writer_clock_class); |
| 143 | case BT_CTF_TYPE_ID_ENUM: |
| 144 | return find_update_enum_clock_fields(err, type, |
| 145 | writer_clock_class); |
| 146 | break; |
| 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | ret = 0; |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static |
| 157 | int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 158 | struct bt_ctf_clock_class *writer_clock_class) |
| 159 | { |
| 160 | int count, i, ret; |
| 161 | struct bt_ctf_field_type *entry_type = NULL; |
| 162 | |
| 163 | count = bt_ctf_field_type_variant_get_field_count(type); |
| 164 | for (i = 0; i < count; i++) { |
| 165 | const char *entry_name; |
| 166 | |
| 167 | ret = bt_ctf_field_type_variant_get_field(type, |
| 168 | &entry_name, &entry_type, i); |
| 169 | if (ret) { |
| 170 | fprintf(err, "[error] %s in %s:%d\n", |
| 171 | __func__, __FILE__, __LINE__); |
| 172 | goto error; |
| 173 | } |
| 174 | ret = find_update_clock_fields(err, entry_type, |
| 175 | writer_clock_class); |
| 176 | if (ret) { |
| 177 | fprintf(err, "[error] %s in %s:%d\n", |
| 178 | __func__, __FILE__, __LINE__); |
| 179 | goto error; |
| 180 | } |
| 181 | BT_PUT(entry_type); |
| 182 | } |
| 183 | |
| 184 | ret = 0; |
| 185 | goto end; |
| 186 | |
| 187 | error: |
| 188 | ret = -1; |
| 189 | end: |
| 190 | bt_put(entry_type); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static |
| 195 | int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 196 | struct bt_ctf_clock_class *writer_clock_class) |
| 197 | { |
| 198 | int count, i, ret; |
| 199 | struct bt_ctf_field_type *entry_type = NULL; |
| 200 | |
| 201 | count = bt_ctf_field_type_structure_get_field_count(type); |
| 202 | for (i = 0; i < count; i++) { |
| 203 | const char *entry_name; |
| 204 | |
| 205 | ret = bt_ctf_field_type_structure_get_field(type, |
| 206 | &entry_name, &entry_type, i); |
| 207 | if (ret) { |
| 208 | fprintf(err, "[error] %s in %s:%d\n", |
| 209 | __func__, __FILE__, __LINE__); |
| 210 | goto error; |
| 211 | } |
| 212 | ret = find_update_clock_fields(err, entry_type, |
| 213 | writer_clock_class); |
| 214 | if (ret) { |
| 215 | fprintf(err, "[error] %s in %s:%d\n", |
| 216 | __func__, __FILE__, __LINE__); |
| 217 | goto error; |
| 218 | } |
| 219 | BT_PUT(entry_type); |
| 220 | } |
| 221 | |
| 222 | ret = 0; |
| 223 | goto end; |
| 224 | |
| 225 | error: |
| 226 | bt_put(entry_type); |
| 227 | end: |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static |
| 232 | int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 233 | struct bt_ctf_clock_class *writer_clock_class) |
| 234 | { |
| 235 | int ret; |
| 236 | struct bt_ctf_field_type *entry_type = NULL; |
| 237 | |
| 238 | entry_type = bt_ctf_field_type_sequence_get_element_type(type); |
| 239 | if (!entry_type) { |
| 240 | fprintf(err, "[error] %s in %s:%d\n", |
| 241 | __func__, __FILE__, __LINE__); |
| 242 | goto error; |
| 243 | } |
| 244 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
| 245 | BT_PUT(entry_type); |
| 246 | if (ret) { |
| 247 | fprintf(err, "[error] %s in %s:%d\n", |
| 248 | __func__, __FILE__, __LINE__); |
| 249 | goto error; |
| 250 | } |
| 251 | |
| 252 | ret = 0; |
| 253 | goto end; |
| 254 | |
| 255 | error: |
| 256 | ret = -1; |
| 257 | end: |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | static |
| 262 | int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 263 | struct bt_ctf_clock_class *writer_clock_class) |
| 264 | { |
| 265 | int ret; |
| 266 | struct bt_ctf_field_type *entry_type = NULL; |
| 267 | |
| 268 | entry_type = bt_ctf_field_type_array_get_element_type(type); |
| 269 | if (!entry_type) { |
| 270 | fprintf(err, "[error] %s in %s:%d\n", |
| 271 | __func__, __FILE__, __LINE__); |
| 272 | goto error; |
| 273 | } |
| 274 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
| 275 | BT_PUT(entry_type); |
| 276 | if (ret) { |
| 277 | fprintf(err, "[error] %s in %s:%d\n", |
| 278 | __func__, __FILE__, __LINE__); |
| 279 | ret = -1; |
| 280 | goto end; |
| 281 | } |
| 282 | |
| 283 | ret = 0; |
| 284 | goto end; |
| 285 | |
| 286 | error: |
| 287 | ret = -1; |
| 288 | end: |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static |
| 293 | int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type, |
| 294 | struct bt_ctf_clock_class *writer_clock_class) |
| 295 | { |
| 296 | int ret; |
| 297 | struct bt_ctf_field_type *entry_type = NULL; |
| 298 | |
| 299 | entry_type = bt_ctf_field_type_enumeration_get_container_type(type); |
| 300 | if (!entry_type) { |
| 301 | fprintf(err, "[error] %s in %s:%d\n", |
| 302 | __func__, __FILE__, __LINE__); |
| 303 | goto error; |
| 304 | } |
| 305 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
| 306 | BT_PUT(entry_type); |
| 307 | if (ret) { |
| 308 | fprintf(err, "[error] %s in %s:%d\n", |
| 309 | __func__, __FILE__, __LINE__); |
| 310 | goto error; |
| 311 | } |
| 312 | |
| 313 | ret = 0; |
| 314 | goto end; |
| 315 | |
| 316 | error: |
| 317 | ret = -1; |
| 318 | end: |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | BT_HIDDEN |
| 323 | struct bt_ctf_field_type *override_header_type(FILE *err, |
| 324 | struct bt_ctf_field_type *type, |
| 325 | struct bt_ctf_trace *writer_trace) |
| 326 | { |
| 327 | struct bt_ctf_field_type *new_type = NULL; |
| 328 | struct bt_ctf_clock_class *writer_clock_class = NULL; |
| 329 | int ret; |
| 330 | |
| 331 | /* FIXME multi-clock? */ |
| 332 | writer_clock_class = bt_ctf_trace_get_clock_class(writer_trace, 0); |
| 333 | if (!writer_clock_class) { |
| 334 | fprintf(err, "[error] %s in %s:%d\n", |
| 335 | __func__, __FILE__, __LINE__); |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | new_type = bt_ctf_field_type_copy(type); |
| 340 | if (!new_type) { |
| 341 | fprintf(err, "[error] %s in %s:%d\n", |
| 342 | __func__, __FILE__, __LINE__); |
| 343 | goto error; |
| 344 | } |
| 345 | |
| 346 | if (bt_ctf_field_type_get_type_id(new_type) != BT_CTF_TYPE_ID_STRUCT) { |
| 347 | fprintf(err, "[error] Unexpected header field type\n"); |
| 348 | goto error; |
| 349 | } |
| 350 | |
| 351 | ret = find_update_struct_clock_fields(err, new_type, writer_clock_class); |
| 352 | if (ret) { |
| 353 | fprintf(err, "[error] %s in %s:%d\n", |
| 354 | __func__, __FILE__, __LINE__); |
| 355 | goto error; |
| 356 | } |
| 357 | BT_PUT(writer_clock_class); |
| 358 | |
| 359 | goto end; |
| 360 | |
| 361 | error: |
| 362 | bt_put(writer_clock_class); |
| 363 | BT_PUT(new_type); |
| 364 | end: |
| 365 | return new_type; |
| 366 | } |
| 367 | |
| 368 | static |
| 369 | int copy_float_field(FILE *err, struct bt_ctf_field *field, |
| 370 | struct bt_ctf_field_type *type, |
| 371 | struct bt_ctf_field *copy_field) |
| 372 | { |
| 373 | double value; |
| 374 | int ret; |
| 375 | |
| 376 | ret = bt_ctf_field_floating_point_get_value(field, &value); |
| 377 | if (ret) { |
| 378 | ret = -1; |
| 379 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 380 | __FILE__, __LINE__); |
| 381 | goto end; |
| 382 | } |
| 383 | ret = bt_ctf_field_floating_point_set_value(copy_field, value); |
| 384 | if (ret) { |
| 385 | ret = -1; |
| 386 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 387 | __FILE__, __LINE__); |
| 388 | goto end; |
| 389 | } |
| 390 | |
| 391 | ret = 0; |
| 392 | |
| 393 | end: |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | static |
| 398 | int copy_string_field(FILE *err, struct bt_ctf_field *field, |
| 399 | struct bt_ctf_field_type *type, |
| 400 | struct bt_ctf_field *copy_field) |
| 401 | { |
| 402 | const char *value; |
| 403 | int ret; |
| 404 | |
| 405 | value = bt_ctf_field_string_get_value(field); |
| 406 | if (!value) { |
| 407 | ret = -1; |
| 408 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 409 | __FILE__, __LINE__); |
| 410 | goto end; |
| 411 | } |
| 412 | ret = bt_ctf_field_string_set_value(copy_field, value); |
| 413 | if (ret) { |
| 414 | ret = -1; |
| 415 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 416 | __FILE__, __LINE__); |
| 417 | goto end; |
| 418 | } |
| 419 | |
| 420 | ret = 0; |
| 421 | |
| 422 | end: |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | BT_HIDDEN |
| 427 | int copy_override_field(FILE *err, struct bt_ctf_event *event, |
| 428 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, |
| 429 | struct bt_ctf_field *copy_field) |
| 430 | { |
| 431 | struct bt_ctf_field_type *type = NULL; |
| 432 | int ret; |
| 433 | |
| 434 | type = bt_ctf_field_get_type(field); |
| 435 | if (!type) { |
| 436 | ret = -1; |
| 437 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 438 | __LINE__); |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | switch (bt_ctf_field_type_get_type_id(type)) { |
| 443 | case BT_CTF_TYPE_ID_INTEGER: |
| 444 | ret = copy_find_clock_int_field(err, event, writer_event, |
| 445 | field, type, copy_field); |
| 446 | break; |
| 447 | case BT_CTF_TYPE_ID_STRUCT: |
| 448 | ret = copy_find_clock_struct_field(err, event, writer_event, |
| 449 | field, type, copy_field); |
| 450 | break; |
| 451 | case BT_CTF_TYPE_ID_FLOAT: |
| 452 | ret = copy_float_field(err, field, type, copy_field); |
| 453 | break; |
| 454 | case BT_CTF_TYPE_ID_ENUM: |
| 455 | ret = copy_find_clock_enum_field(err, event, writer_event, |
| 456 | field, type, copy_field); |
| 457 | break; |
| 458 | case BT_CTF_TYPE_ID_STRING: |
| 459 | ret = copy_string_field(err, field, type, copy_field); |
| 460 | break; |
| 461 | case BT_CTF_TYPE_ID_ARRAY: |
| 462 | ret = copy_find_clock_array_field(err, event, writer_event, |
| 463 | field, type, copy_field); |
| 464 | break; |
| 465 | case BT_CTF_TYPE_ID_SEQUENCE: |
| 466 | ret = copy_find_clock_sequence_field(err, event, writer_event, |
| 467 | field, type, copy_field); |
| 468 | break; |
| 469 | case BT_CTF_TYPE_ID_UNTAGGED_VARIANT: |
| 470 | case BT_CTF_TYPE_ID_VARIANT: |
| 471 | ret = copy_find_clock_variant_field(err, event, writer_event, |
| 472 | field, type, copy_field); |
| 473 | break; |
| 474 | /* No default, we want to catch missing field types. */ |
| 475 | case BT_CTF_TYPE_ID_UNKNOWN: |
| 476 | case BT_CTF_NR_TYPE_IDS: |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | ret = 0; |
| 481 | BT_PUT(type); |
| 482 | |
| 483 | end: |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | static |
| 488 | int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event, |
| 489 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, |
| 490 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field) |
| 491 | { |
| 492 | int ret; |
| 493 | struct bt_ctf_field *container = NULL, *copy_container = NULL; |
| 494 | |
| 495 | container = bt_ctf_field_enumeration_get_container(field); |
| 496 | if (!container) { |
| 497 | ret = -1; |
| 498 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 499 | __LINE__); |
| 500 | goto end; |
| 501 | } |
| 502 | |
| 503 | copy_container = bt_ctf_field_enumeration_get_container(copy_field); |
| 504 | if (!copy_container) { |
| 505 | ret = -1; |
| 506 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 507 | __LINE__); |
| 508 | goto error; |
| 509 | } |
| 510 | |
| 511 | ret = copy_override_field(err, event, writer_event, container, |
| 512 | copy_container); |
| 513 | if (ret) { |
| 514 | ret = -1; |
| 515 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 516 | __LINE__); |
| 517 | goto error; |
| 518 | } |
| 519 | |
| 520 | ret = 0; |
| 521 | goto end; |
| 522 | |
| 523 | error: |
| 524 | ret = -1; |
| 525 | end: |
| 526 | bt_put(copy_container); |
| 527 | bt_put(container); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | static |
| 532 | int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event, |
| 533 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, |
| 534 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field) |
| 535 | { |
| 536 | int ret; |
| 537 | struct bt_ctf_field *tag = NULL; |
| 538 | struct bt_ctf_field *variant_field = NULL, *copy_variant_field = NULL; |
| 539 | |
| 540 | tag = bt_ctf_field_variant_get_tag(field); |
| 541 | if (!tag) { |
| 542 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 543 | __LINE__); |
| 544 | goto error; |
| 545 | } |
| 546 | |
| 547 | variant_field = bt_ctf_field_variant_get_field(field, tag); |
| 548 | if (!variant_field) { |
| 549 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 550 | __LINE__); |
| 551 | goto error; |
| 552 | } |
| 553 | |
| 554 | copy_variant_field = bt_ctf_field_variant_get_field(copy_field, tag); |
| 555 | if (!copy_variant_field) { |
| 556 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 557 | __LINE__); |
| 558 | goto error; |
| 559 | } |
| 560 | |
| 561 | ret = copy_override_field(err, event, writer_event, variant_field, |
| 562 | copy_variant_field); |
| 563 | if (ret) { |
| 564 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 565 | __LINE__); |
| 566 | goto error; |
| 567 | } |
| 568 | |
| 569 | ret = 0; |
| 570 | goto end; |
| 571 | |
| 572 | error: |
| 573 | ret = -1; |
| 574 | end: |
| 575 | bt_put(copy_variant_field); |
| 576 | bt_put(variant_field); |
| 577 | bt_put(tag); |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | static |
| 582 | int copy_find_clock_sequence_field(FILE *err, |
| 583 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 584 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 585 | struct bt_ctf_field *copy_field) |
| 586 | { |
| 587 | int ret; |
| 588 | uint64_t i, count; |
| 589 | struct bt_ctf_field *length_field = NULL; |
| 590 | struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL; |
| 591 | |
| 592 | length_field = bt_ctf_field_sequence_get_length(field); |
| 593 | if (!length_field) { |
| 594 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 595 | __LINE__); |
| 596 | goto error; |
| 597 | } |
| 598 | |
| 599 | ret = bt_ctf_field_unsigned_integer_get_value(length_field, &count); |
| 600 | if (ret) { |
| 601 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 602 | __LINE__); |
| 603 | goto error; |
| 604 | } |
| 605 | |
| 606 | ret = bt_ctf_field_sequence_set_length(copy_field, length_field); |
| 607 | if (ret) { |
| 608 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 609 | __LINE__); |
| 610 | goto error; |
| 611 | } |
| 612 | BT_PUT(length_field); |
| 613 | |
| 614 | for (i = 0; i < count; i++) { |
| 615 | entry_field = bt_ctf_field_sequence_get_field(field, i); |
| 616 | if (!entry_field) { |
| 617 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 618 | __LINE__); |
| 619 | goto error; |
| 620 | } |
| 621 | |
| 622 | entry_copy = bt_ctf_field_sequence_get_field(copy_field, i); |
| 623 | if (!entry_copy) { |
| 624 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 625 | __LINE__); |
| 626 | goto error; |
| 627 | } |
| 628 | |
| 629 | ret = copy_override_field(err, event, writer_event, entry_field, |
| 630 | entry_copy); |
| 631 | if (ret) { |
| 632 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 633 | __LINE__); |
| 634 | goto error; |
| 635 | } |
| 636 | BT_PUT(entry_field); |
| 637 | BT_PUT(entry_copy); |
| 638 | } |
| 639 | |
| 640 | ret = 0; |
| 641 | goto end; |
| 642 | |
| 643 | error: |
| 644 | bt_put(length_field); |
| 645 | bt_put(entry_field); |
| 646 | bt_put(entry_copy); |
| 647 | ret = -1; |
| 648 | end: |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | static |
| 653 | int copy_find_clock_array_field(FILE *err, |
| 654 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 655 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 656 | struct bt_ctf_field *copy_field) |
| 657 | { |
| 658 | int ret, count, i; |
| 659 | struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL; |
| 660 | |
| 661 | count = bt_ctf_field_type_array_get_length(type); |
| 662 | for (i = 0; i < count; i++) { |
| 663 | entry_field = bt_ctf_field_array_get_field(field, i); |
| 664 | if (!entry_field) { |
| 665 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 666 | __LINE__); |
| 667 | goto error; |
| 668 | } |
| 669 | |
| 670 | entry_copy = bt_ctf_field_array_get_field(copy_field, i); |
| 671 | if (!entry_copy) { |
| 672 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 673 | __LINE__); |
| 674 | goto error; |
| 675 | } |
| 676 | |
| 677 | ret = copy_override_field(err, event, writer_event, entry_field, |
| 678 | entry_copy); |
| 679 | if (ret) { |
| 680 | ret = -1; |
| 681 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 682 | __LINE__); |
| 683 | goto error; |
| 684 | } |
| 685 | BT_PUT(entry_field); |
| 686 | BT_PUT(entry_copy); |
| 687 | } |
| 688 | |
| 689 | ret = 0; |
| 690 | goto end; |
| 691 | |
| 692 | error: |
| 693 | bt_put(entry_field); |
| 694 | bt_put(entry_copy); |
| 695 | |
| 696 | end: |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | static |
| 701 | int copy_find_clock_struct_field(FILE *err, |
| 702 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 703 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 704 | struct bt_ctf_field *copy_field) |
| 705 | { |
| 706 | int count, i, ret; |
| 707 | struct bt_ctf_field_type *entry_type = NULL; |
| 708 | struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL; |
| 709 | |
| 710 | count = bt_ctf_field_type_structure_get_field_count(type); |
| 711 | for (i = 0; i < count; i++) { |
| 712 | const char *entry_name; |
| 713 | |
| 714 | entry_field = bt_ctf_field_structure_get_field_by_index(field, i); |
| 715 | if (!entry_field) { |
| 716 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 717 | __LINE__); |
| 718 | goto error; |
| 719 | } |
| 720 | |
| 721 | ret = bt_ctf_field_type_structure_get_field(type, &entry_name, |
| 722 | &entry_type, i); |
| 723 | if (ret) { |
| 724 | fprintf(err, "[error] %s in %s:%d\n", |
| 725 | __func__, __FILE__, __LINE__); |
| 726 | goto error; |
| 727 | } |
| 728 | |
| 729 | entry_copy = bt_ctf_field_structure_get_field_by_index(copy_field, i); |
| 730 | if (!entry_copy) { |
| 731 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 732 | __LINE__); |
| 733 | goto error; |
| 734 | } |
| 735 | |
| 736 | ret = copy_override_field(err, event, writer_event, entry_field, |
| 737 | entry_copy); |
| 738 | if (ret) { |
| 739 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 740 | __LINE__); |
| 741 | goto error; |
| 742 | } |
| 743 | BT_PUT(entry_copy); |
| 744 | BT_PUT(entry_field); |
| 745 | BT_PUT(entry_type); |
| 746 | } |
| 747 | |
| 748 | ret = 0; |
| 749 | goto end; |
| 750 | |
| 751 | error: |
| 752 | bt_put(entry_type); |
| 753 | bt_put(entry_field); |
| 754 | bt_put(entry_copy); |
| 755 | ret = -1; |
| 756 | end: |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | static |
| 761 | int set_int_value(FILE *err, struct bt_ctf_field *field, |
| 762 | struct bt_ctf_field *copy_field, |
| 763 | struct bt_ctf_field_type *type) |
| 764 | { |
| 765 | uint64_t uvalue; |
| 766 | int64_t value; |
| 767 | int ret; |
| 768 | |
| 769 | if (bt_ctf_field_type_integer_get_signed(type)) { |
| 770 | ret = bt_ctf_field_signed_integer_get_value(field, &value); |
| 771 | if (ret) { |
| 772 | ret = -1; |
| 773 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 774 | __FILE__, __LINE__); |
| 775 | goto end; |
| 776 | } |
| 777 | ret = bt_ctf_field_signed_integer_set_value(copy_field, value); |
| 778 | if (ret) { |
| 779 | ret = -1; |
| 780 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 781 | __FILE__, __LINE__); |
| 782 | goto end; |
| 783 | } |
| 784 | } else { |
| 785 | ret = bt_ctf_field_unsigned_integer_get_value(field, |
| 786 | &uvalue); |
| 787 | if (ret) { |
| 788 | ret = -1; |
| 789 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 790 | __FILE__, __LINE__); |
| 791 | goto end; |
| 792 | } |
| 793 | ret = bt_ctf_field_unsigned_integer_set_value(copy_field, uvalue); |
| 794 | if (ret) { |
| 795 | ret = -1; |
| 796 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 797 | __FILE__, __LINE__); |
| 798 | goto end; |
| 799 | } |
| 800 | } |
| 801 | ret = 0; |
| 802 | |
| 803 | end: |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err, |
| 808 | struct bt_ctf_stream_class *stream_class) |
| 809 | { |
| 810 | struct bt_ctf_trace *trace = NULL; |
| 811 | struct bt_ctf_clock_class *clock_class = NULL; |
| 812 | |
| 813 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 814 | if (!trace) { |
| 815 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 816 | __LINE__); |
| 817 | goto end; |
| 818 | } |
| 819 | |
| 820 | /* FIXME multi-clock? */ |
| 821 | clock_class = bt_ctf_trace_get_clock_class(trace, 0); |
| 822 | |
| 823 | bt_put(trace); |
| 824 | end: |
| 825 | return clock_class; |
| 826 | } |
| 827 | |
| 828 | struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event) |
| 829 | { |
| 830 | struct bt_ctf_event_class *event_class = NULL; |
| 831 | struct bt_ctf_stream_class *stream_class = NULL; |
| 832 | struct bt_ctf_clock_class *clock_class = NULL; |
| 833 | |
| 834 | event_class = bt_ctf_event_get_class(event); |
| 835 | if (!event_class) { |
| 836 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 837 | __LINE__); |
| 838 | goto end; |
| 839 | } |
| 840 | |
| 841 | stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 842 | BT_PUT(event_class); |
| 843 | if (!stream_class) { |
| 844 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 845 | __LINE__); |
| 846 | goto end; |
| 847 | } |
| 848 | |
| 849 | clock_class = stream_class_get_clock_class(err, stream_class); |
| 850 | bt_put(stream_class); |
| 851 | |
| 852 | end: |
| 853 | return clock_class; |
| 854 | } |
| 855 | |
| 856 | static |
| 857 | int copy_find_clock_int_field(FILE *err, |
| 858 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, |
| 859 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, |
| 860 | struct bt_ctf_field *copy_field) |
| 861 | { |
| 862 | struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL; |
| 863 | struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL; |
| 864 | uint64_t value; |
| 865 | int ret; |
| 866 | |
| 867 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(type); |
| 868 | if (!clock_class) { |
| 869 | return set_int_value(err, field, copy_field, type); |
| 870 | } |
| 871 | |
| 872 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); |
| 873 | BT_PUT(clock_class); |
| 874 | if (!clock_value) { |
| 875 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 876 | __LINE__); |
| 877 | goto error; |
| 878 | } |
| 879 | |
| 880 | ret = bt_ctf_clock_value_get_value(clock_value, &value); |
| 881 | BT_PUT(clock_value); |
| 882 | if (ret) { |
| 883 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 884 | __LINE__); |
| 885 | goto error; |
| 886 | } |
| 887 | |
| 888 | ret = bt_ctf_field_unsigned_integer_set_value(copy_field, value); |
| 889 | if (ret) { |
| 890 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 891 | __LINE__); |
| 892 | goto error; |
| 893 | } |
| 894 | |
| 895 | writer_clock_class = event_get_clock_class(err, writer_event); |
| 896 | if (!writer_clock_class) { |
| 897 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 898 | __LINE__); |
| 899 | goto error; |
| 900 | } |
| 901 | |
| 902 | writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value); |
| 903 | BT_PUT(writer_clock_class); |
| 904 | if (!writer_clock_value) { |
| 905 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 906 | __LINE__); |
| 907 | goto error; |
| 908 | } |
| 909 | |
| 910 | ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value); |
| 911 | BT_PUT(writer_clock_value); |
| 912 | if (ret) { |
| 913 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 914 | __LINE__); |
| 915 | goto error; |
| 916 | } |
| 917 | |
| 918 | ret = 0; |
| 919 | goto end; |
| 920 | |
| 921 | error: |
| 922 | ret = -1; |
| 923 | end: |
| 924 | return ret; |
| 925 | } |
| 926 | |