| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define BT_LOG_TAG "CTF-WRITER/FIELDS" |
| 8 | #include "logging.h" |
| 9 | |
| 10 | #include <float.h> |
| 11 | #include <inttypes.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #include <babeltrace2-ctf-writer/object.h> |
| 16 | |
| 17 | #include "common/align.h" |
| 18 | #include "common/assert.h" |
| 19 | #include "compat/compiler.h" |
| 20 | #include "compat/endian.h" |
| 21 | #include "compat/fcntl.h" |
| 22 | #include "ctfser/ctfser.h" |
| 23 | |
| 24 | #include "assert-pre.h" |
| 25 | #include "fields.h" |
| 26 | #include "field-types.h" |
| 27 | #include "object.h" |
| 28 | |
| 29 | #define BT_CTF_ASSERT_PRE_CTF_FIELD_IS_INT_OR_ENUM(_field, _name) \ |
| 30 | BT_CTF_ASSERT_PRE((_field)->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER || \ |
| 31 | (_field)->type->id == BT_CTF_FIELD_TYPE_ID_ENUM, \ |
| 32 | _name " is not an integer or an enumeration field: " \ |
| 33 | "field-addr=%p", (_field)) |
| 34 | |
| 35 | struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field) |
| 36 | { |
| 37 | struct bt_ctf_field_common *copy = NULL; |
| 38 | |
| 39 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Field"); |
| 40 | BT_ASSERT_DBG(field_type_common_has_known_id(field->type)); |
| 41 | BT_ASSERT_DBG(field->methods->copy); |
| 42 | copy = field->methods->copy(field); |
| 43 | if (!copy) { |
| 44 | BT_LOGW("Cannot create field: ft-addr=%p", field->type); |
| 45 | goto end; |
| 46 | } |
| 47 | |
| 48 | bt_ctf_field_common_set(copy, field->payload_set); |
| 49 | |
| 50 | end: |
| 51 | return copy; |
| 52 | } |
| 53 | |
| 54 | int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common *field, |
| 55 | struct bt_ctf_field_type_common *type, |
| 56 | bool is_shared, bt_ctf_object_release_func release_func, |
| 57 | struct bt_ctf_field_common_methods *methods, |
| 58 | bt_ctf_field_common_create_func field_create_func, |
| 59 | GDestroyNotify field_release_func) |
| 60 | { |
| 61 | int ret = 0; |
| 62 | struct bt_ctf_field_type_common_structure *structure_type = |
| 63 | BT_CTF_FROM_COMMON(type); |
| 64 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); |
| 65 | size_t i; |
| 66 | |
| 67 | BT_LOGD("Initializing common structure field object: ft-addr=%p", type); |
| 68 | bt_ctf_field_common_initialize(field, type, is_shared, |
| 69 | release_func, methods); |
| 70 | structure->fields = g_ptr_array_new_with_free_func(field_release_func); |
| 71 | g_ptr_array_set_size(structure->fields, structure_type->fields->len); |
| 72 | |
| 73 | /* Create all fields contained in the structure field. */ |
| 74 | for (i = 0; i < structure_type->fields->len; i++) { |
| 75 | struct bt_ctf_field_common *member_field; |
| 76 | struct bt_ctf_field_type_common_structure_field *struct_field = |
| 77 | BT_CTF_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX( |
| 78 | structure_type, i); |
| 79 | member_field = field_create_func(struct_field->type); |
| 80 | if (!member_field) { |
| 81 | BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu", |
| 82 | g_quark_to_string(struct_field->name), i); |
| 83 | ret = -1; |
| 84 | goto end; |
| 85 | } |
| 86 | |
| 87 | g_ptr_array_index(structure->fields, i) = member_field; |
| 88 | } |
| 89 | |
| 90 | BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p", |
| 91 | field, type); |
| 92 | |
| 93 | end: |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common *field, |
| 98 | struct bt_ctf_field_type_common *type, |
| 99 | bool is_shared, bt_ctf_object_release_func release_func, |
| 100 | struct bt_ctf_field_common_methods *methods, |
| 101 | bt_ctf_field_common_create_func field_create_func, |
| 102 | GDestroyNotify field_release_func) |
| 103 | { |
| 104 | int ret = 0; |
| 105 | struct bt_ctf_field_type_common_variant *variant_type = |
| 106 | BT_CTF_FROM_COMMON(type); |
| 107 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); |
| 108 | size_t i; |
| 109 | |
| 110 | BT_LOGD("Initializing common variant field object: ft-addr=%p", type); |
| 111 | bt_ctf_field_common_initialize(field, type, is_shared, |
| 112 | release_func, methods); |
| 113 | ret = bt_ctf_field_type_common_variant_update_choices(type); |
| 114 | if (ret) { |
| 115 | BT_LOGE("Cannot update common variant field type choices: " |
| 116 | "ret=%d", ret); |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | variant->fields = g_ptr_array_new_with_free_func(field_release_func); |
| 121 | g_ptr_array_set_size(variant->fields, variant_type->choices->len); |
| 122 | |
| 123 | /* Create all fields contained in the variant field. */ |
| 124 | for (i = 0; i < variant_type->choices->len; i++) { |
| 125 | struct bt_ctf_field_common *member_field; |
| 126 | struct bt_ctf_field_type_common_variant_choice *var_choice = |
| 127 | BT_CTF_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX( |
| 128 | variant_type, i); |
| 129 | |
| 130 | member_field = field_create_func(var_choice->type); |
| 131 | if (!member_field) { |
| 132 | BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu", |
| 133 | g_quark_to_string(var_choice->name), i); |
| 134 | ret = -1; |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | g_ptr_array_index(variant->fields, i) = member_field; |
| 139 | } |
| 140 | |
| 141 | BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p", |
| 142 | field, type); |
| 143 | |
| 144 | end: |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field, |
| 149 | struct bt_ctf_field_type_common *type, |
| 150 | bool is_shared, bt_ctf_object_release_func release_func, |
| 151 | struct bt_ctf_field_common_methods *methods) |
| 152 | { |
| 153 | int ret = 0; |
| 154 | struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field); |
| 155 | |
| 156 | BT_LOGD("Initializing common string field object: ft-addr=%p", type); |
| 157 | bt_ctf_field_common_initialize(field, type, is_shared, |
| 158 | release_func, methods); |
| 159 | string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1); |
| 160 | if (!string->buf) { |
| 161 | ret = -1; |
| 162 | goto end; |
| 163 | } |
| 164 | |
| 165 | g_array_set_size(string->buf, 1); |
| 166 | bt_g_array_index(string->buf, char, 0) = '\0'; |
| 167 | BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p", |
| 168 | field, type); |
| 169 | |
| 170 | end: |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common *field, |
| 175 | struct bt_ctf_field_type_common *type, |
| 176 | bool is_shared, bt_ctf_object_release_func release_func, |
| 177 | struct bt_ctf_field_common_methods *methods, |
| 178 | bt_ctf_field_common_create_func field_create_func, |
| 179 | GDestroyNotify field_destroy_func) |
| 180 | { |
| 181 | struct bt_ctf_field_type_common_array *array_type = BT_CTF_FROM_COMMON(type); |
| 182 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); |
| 183 | unsigned int array_length; |
| 184 | int ret = 0; |
| 185 | uint64_t i; |
| 186 | |
| 187 | BT_LOGD("Initializing common array field object: ft-addr=%p", type); |
| 188 | BT_ASSERT_DBG(type); |
| 189 | bt_ctf_field_common_initialize(field, type, is_shared, |
| 190 | release_func, methods); |
| 191 | array_length = array_type->length; |
| 192 | array->elements = g_ptr_array_sized_new(array_length); |
| 193 | if (!array->elements) { |
| 194 | ret = -1; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | g_ptr_array_set_free_func(array->elements, field_destroy_func); |
| 199 | g_ptr_array_set_size(array->elements, array_length); |
| 200 | |
| 201 | for (i = 0; i < array_length; i++) { |
| 202 | array->elements->pdata[i] = field_create_func( |
| 203 | array_type->element_ft); |
| 204 | if (!array->elements->pdata[i]) { |
| 205 | ret = -1; |
| 206 | goto end; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p", |
| 211 | field, type); |
| 212 | |
| 213 | end: |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common *field, |
| 218 | struct bt_ctf_field_type_common *type, |
| 219 | bool is_shared, bt_ctf_object_release_func release_func, |
| 220 | struct bt_ctf_field_common_methods *methods, |
| 221 | GDestroyNotify field_destroy_func) |
| 222 | { |
| 223 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); |
| 224 | int ret = 0; |
| 225 | |
| 226 | BT_LOGD("Initializing common sequence field object: ft-addr=%p", type); |
| 227 | BT_ASSERT_DBG(type); |
| 228 | bt_ctf_field_common_initialize(field, type, is_shared, |
| 229 | release_func, methods); |
| 230 | sequence->elements = g_ptr_array_new(); |
| 231 | if (!sequence->elements) { |
| 232 | ret = -1; |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | g_ptr_array_set_free_func(sequence->elements, field_destroy_func); |
| 237 | BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p", |
| 238 | field, type); |
| 239 | |
| 240 | end: |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field) |
| 245 | { |
| 246 | return (field && field->payload_set) ? 0 : -1; |
| 247 | } |
| 248 | |
| 249 | int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field) |
| 250 | { |
| 251 | int64_t i; |
| 252 | int ret = 0; |
| 253 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); |
| 254 | |
| 255 | BT_ASSERT_DBG(field); |
| 256 | |
| 257 | for (i = 0; i < structure->fields->len; i++) { |
| 258 | ret = bt_ctf_field_common_validate_recursive( |
| 259 | (void *) structure->fields->pdata[i]); |
| 260 | |
| 261 | if (ret) { |
| 262 | int this_ret; |
| 263 | const char *name; |
| 264 | |
| 265 | this_ret = bt_ctf_field_type_common_structure_borrow_field_by_index( |
| 266 | field->type, &name, NULL, i); |
| 267 | BT_ASSERT_DBG(this_ret == 0); |
| 268 | BT_CTF_ASSERT_PRE_MSG("Invalid structure field's field: " |
| 269 | "struct-field-addr=%p, field-name=\"%s\", " |
| 270 | "index=%" PRId64 ", field-addr=%p", |
| 271 | field, name, i, structure->fields->pdata[i]); |
| 272 | goto end; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | end: |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field) |
| 281 | { |
| 282 | int ret = 0; |
| 283 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); |
| 284 | |
| 285 | BT_ASSERT_DBG(field); |
| 286 | |
| 287 | if (!variant->current_field) { |
| 288 | ret = -1; |
| 289 | goto end; |
| 290 | } |
| 291 | |
| 292 | ret = bt_ctf_field_common_validate_recursive(variant->current_field); |
| 293 | |
| 294 | end: |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field) |
| 299 | { |
| 300 | int64_t i; |
| 301 | int ret = 0; |
| 302 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); |
| 303 | |
| 304 | BT_ASSERT_DBG(field); |
| 305 | |
| 306 | for (i = 0; i < array->elements->len; i++) { |
| 307 | ret = bt_ctf_field_common_validate_recursive((void *) array->elements->pdata[i]); |
| 308 | if (ret) { |
| 309 | BT_CTF_ASSERT_PRE_MSG("Invalid array field's element field: " |
| 310 | "array-field-addr=%p, %" PRId64 ", " |
| 311 | "elem-field-addr=%p", |
| 312 | field, i, array->elements->pdata[i]); |
| 313 | goto end; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | end: |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field) |
| 322 | { |
| 323 | int64_t i; |
| 324 | int ret = 0; |
| 325 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); |
| 326 | |
| 327 | BT_ASSERT_DBG(field); |
| 328 | |
| 329 | for (i = 0; i < sequence->elements->len; i++) { |
| 330 | ret = bt_ctf_field_common_validate_recursive( |
| 331 | (void *) sequence->elements->pdata[i]); |
| 332 | if (ret) { |
| 333 | BT_CTF_ASSERT_PRE_MSG("Invalid sequence field's element field: " |
| 334 | "seq-field-addr=%p, %" PRId64 ", " |
| 335 | "elem-field-addr=%p", |
| 336 | field, i, sequence->elements->pdata[i]); |
| 337 | goto end; |
| 338 | } |
| 339 | } |
| 340 | end: |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field) |
| 345 | { |
| 346 | BT_ASSERT_DBG(field); |
| 347 | field->payload_set = false; |
| 348 | } |
| 349 | |
| 350 | void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field) |
| 351 | { |
| 352 | int64_t i; |
| 353 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); |
| 354 | |
| 355 | BT_ASSERT_DBG(field); |
| 356 | |
| 357 | for (i = 0; i < structure->fields->len; i++) { |
| 358 | struct bt_ctf_field_common *member = structure->fields->pdata[i]; |
| 359 | |
| 360 | if (!member) { |
| 361 | /* |
| 362 | * Structure members are lazily initialized; |
| 363 | * skip if this member has not been allocated |
| 364 | * yet. |
| 365 | */ |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | bt_ctf_field_common_reset_recursive(member); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field) |
| 374 | { |
| 375 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); |
| 376 | |
| 377 | BT_ASSERT_DBG(field); |
| 378 | variant->current_field = NULL; |
| 379 | } |
| 380 | |
| 381 | void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field) |
| 382 | { |
| 383 | size_t i; |
| 384 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); |
| 385 | |
| 386 | BT_ASSERT_DBG(field); |
| 387 | |
| 388 | for (i = 0; i < array->elements->len; i++) { |
| 389 | struct bt_ctf_field_common *member = array->elements->pdata[i]; |
| 390 | |
| 391 | if (!member) { |
| 392 | /* |
| 393 | * Array elements are lazily initialized; skip |
| 394 | * if this member has not been allocated yet. |
| 395 | */ |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | bt_ctf_field_common_reset_recursive(member); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field) |
| 404 | { |
| 405 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); |
| 406 | uint64_t i; |
| 407 | |
| 408 | BT_ASSERT_DBG(field); |
| 409 | |
| 410 | for (i = 0; i < sequence->elements->len; i++) { |
| 411 | if (sequence->elements->pdata[i]) { |
| 412 | bt_ctf_field_common_reset_recursive( |
| 413 | sequence->elements->pdata[i]); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | sequence->length = 0; |
| 418 | } |
| 419 | |
| 420 | void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field, |
| 421 | bool is_frozen) |
| 422 | { |
| 423 | field->frozen = is_frozen; |
| 424 | } |
| 425 | |
| 426 | void bt_ctf_field_common_structure_set_is_frozen_recursive( |
| 427 | struct bt_ctf_field_common *field, bool is_frozen) |
| 428 | { |
| 429 | uint64_t i; |
| 430 | struct bt_ctf_field_common_structure *structure_field = |
| 431 | BT_CTF_FROM_COMMON(field); |
| 432 | |
| 433 | BT_LOGD("Freezing structure field object: addr=%p", field); |
| 434 | |
| 435 | for (i = 0; i < structure_field->fields->len; i++) { |
| 436 | struct bt_ctf_field_common *struct_field = |
| 437 | g_ptr_array_index(structure_field->fields, i); |
| 438 | |
| 439 | BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64, |
| 440 | struct_field, i); |
| 441 | bt_ctf_field_common_set_is_frozen_recursive(struct_field, |
| 442 | is_frozen); |
| 443 | } |
| 444 | |
| 445 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); |
| 446 | } |
| 447 | |
| 448 | void bt_ctf_field_common_variant_set_is_frozen_recursive( |
| 449 | struct bt_ctf_field_common *field, bool is_frozen) |
| 450 | { |
| 451 | uint64_t i; |
| 452 | struct bt_ctf_field_common_variant *variant_field = BT_CTF_FROM_COMMON(field); |
| 453 | |
| 454 | BT_LOGD("Freezing variant field object: addr=%p", field); |
| 455 | |
| 456 | for (i = 0; i < variant_field->fields->len; i++) { |
| 457 | struct bt_ctf_field_common *var_field = |
| 458 | g_ptr_array_index(variant_field->fields, i); |
| 459 | |
| 460 | BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64, |
| 461 | var_field, i); |
| 462 | bt_ctf_field_common_set_is_frozen_recursive(var_field, is_frozen); |
| 463 | } |
| 464 | |
| 465 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); |
| 466 | } |
| 467 | |
| 468 | void bt_ctf_field_common_array_set_is_frozen_recursive( |
| 469 | struct bt_ctf_field_common *field, bool is_frozen) |
| 470 | { |
| 471 | int64_t i; |
| 472 | struct bt_ctf_field_common_array *array_field = BT_CTF_FROM_COMMON(field); |
| 473 | |
| 474 | BT_LOGD("Freezing array field object: addr=%p", field); |
| 475 | |
| 476 | for (i = 0; i < array_field->elements->len; i++) { |
| 477 | struct bt_ctf_field_common *elem_field = |
| 478 | g_ptr_array_index(array_field->elements, i); |
| 479 | |
| 480 | BT_LOGD("Freezing array field object's element field: " |
| 481 | "element-field-addr=%p, index=%" PRId64, |
| 482 | elem_field, i); |
| 483 | bt_ctf_field_common_set_is_frozen_recursive(elem_field, is_frozen); |
| 484 | } |
| 485 | |
| 486 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); |
| 487 | } |
| 488 | |
| 489 | void bt_ctf_field_common_sequence_set_is_frozen_recursive( |
| 490 | struct bt_ctf_field_common *field, bool is_frozen) |
| 491 | { |
| 492 | int64_t i; |
| 493 | struct bt_ctf_field_common_sequence *sequence_field = |
| 494 | BT_CTF_FROM_COMMON(field); |
| 495 | |
| 496 | BT_LOGD("Freezing sequence field object: addr=%p", field); |
| 497 | |
| 498 | for (i = 0; i < sequence_field->length; i++) { |
| 499 | struct bt_ctf_field_common *elem_field = |
| 500 | g_ptr_array_index(sequence_field->elements, i); |
| 501 | |
| 502 | BT_LOGD("Freezing sequence field object's element field: " |
| 503 | "element-field-addr=%p, index=%" PRId64, |
| 504 | elem_field, i); |
| 505 | bt_ctf_field_common_set_is_frozen_recursive(elem_field, is_frozen); |
| 506 | } |
| 507 | |
| 508 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); |
| 509 | } |
| 510 | |
| 511 | void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field, |
| 512 | bool is_frozen) |
| 513 | { |
| 514 | if (!field) { |
| 515 | goto end; |
| 516 | } |
| 517 | |
| 518 | BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d", |
| 519 | field, is_frozen); |
| 520 | BT_ASSERT_DBG(field_type_common_has_known_id(field->type)); |
| 521 | BT_ASSERT_DBG(field->methods->set_is_frozen); |
| 522 | field->methods->set_is_frozen(field, is_frozen); |
| 523 | |
| 524 | end: |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | bt_ctf_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field) |
| 529 | { |
| 530 | return field && field->payload_set; |
| 531 | } |
| 532 | |
| 533 | bt_ctf_bool bt_ctf_field_common_structure_is_set_recursive( |
| 534 | struct bt_ctf_field_common *field) |
| 535 | { |
| 536 | bt_ctf_bool is_set = BT_CTF_FALSE; |
| 537 | size_t i; |
| 538 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); |
| 539 | |
| 540 | BT_ASSERT_DBG(field); |
| 541 | |
| 542 | for (i = 0; i < structure->fields->len; i++) { |
| 543 | is_set = bt_ctf_field_common_is_set_recursive( |
| 544 | structure->fields->pdata[i]); |
| 545 | if (!is_set) { |
| 546 | goto end; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | end: |
| 551 | return is_set; |
| 552 | } |
| 553 | |
| 554 | bt_ctf_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field) |
| 555 | { |
| 556 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); |
| 557 | bt_ctf_bool is_set = BT_CTF_FALSE; |
| 558 | |
| 559 | BT_ASSERT_DBG(field); |
| 560 | |
| 561 | if (variant->current_field) { |
| 562 | is_set = bt_ctf_field_common_is_set_recursive( |
| 563 | variant->current_field); |
| 564 | } |
| 565 | |
| 566 | return is_set; |
| 567 | } |
| 568 | |
| 569 | bt_ctf_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field) |
| 570 | { |
| 571 | size_t i; |
| 572 | bt_ctf_bool is_set = BT_CTF_FALSE; |
| 573 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); |
| 574 | |
| 575 | BT_ASSERT_DBG(field); |
| 576 | |
| 577 | for (i = 0; i < array->elements->len; i++) { |
| 578 | is_set = bt_ctf_field_common_is_set_recursive(array->elements->pdata[i]); |
| 579 | if (!is_set) { |
| 580 | goto end; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | end: |
| 585 | return is_set; |
| 586 | } |
| 587 | |
| 588 | bt_ctf_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field) |
| 589 | { |
| 590 | size_t i; |
| 591 | bt_ctf_bool is_set = BT_CTF_FALSE; |
| 592 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); |
| 593 | |
| 594 | BT_ASSERT_DBG(field); |
| 595 | |
| 596 | if (!sequence->elements) { |
| 597 | goto end; |
| 598 | } |
| 599 | |
| 600 | for (i = 0; i < sequence->elements->len; i++) { |
| 601 | is_set = bt_ctf_field_common_is_set_recursive( |
| 602 | sequence->elements->pdata[i]); |
| 603 | if (!is_set) { |
| 604 | goto end; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | end: |
| 609 | return is_set; |
| 610 | } |
| 611 | |
| 612 | static struct bt_ctf_field_common_methods bt_ctf_field_integer_methods = { |
| 613 | .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen, |
| 614 | .validate = bt_ctf_field_common_generic_validate, |
| 615 | .copy = NULL, |
| 616 | .is_set = bt_ctf_field_common_generic_is_set, |
| 617 | .reset = bt_ctf_field_common_generic_reset, |
| 618 | }; |
| 619 | |
| 620 | static struct bt_ctf_field_common_methods bt_ctf_field_floating_point_methods = { |
| 621 | .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen, |
| 622 | .validate = bt_ctf_field_common_generic_validate, |
| 623 | .copy = NULL, |
| 624 | .is_set = bt_ctf_field_common_generic_is_set, |
| 625 | .reset = bt_ctf_field_common_generic_reset, |
| 626 | }; |
| 627 | |
| 628 | static |
| 629 | void bt_ctf_field_enumeration_set_is_frozen_recursive( |
| 630 | struct bt_ctf_field_common *field, bool is_frozen); |
| 631 | |
| 632 | static |
| 633 | int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field); |
| 634 | |
| 635 | static |
| 636 | bt_ctf_bool bt_ctf_field_enumeration_is_set_recursive( |
| 637 | struct bt_ctf_field_common *field); |
| 638 | |
| 639 | static |
| 640 | void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field); |
| 641 | |
| 642 | static struct bt_ctf_field_common_methods bt_ctf_field_enumeration_methods = { |
| 643 | .set_is_frozen = bt_ctf_field_enumeration_set_is_frozen_recursive, |
| 644 | .validate = bt_ctf_field_enumeration_validate_recursive, |
| 645 | .copy = NULL, |
| 646 | .is_set = bt_ctf_field_enumeration_is_set_recursive, |
| 647 | .reset = bt_ctf_field_enumeration_reset_recursive, |
| 648 | }; |
| 649 | |
| 650 | static struct bt_ctf_field_common_methods bt_ctf_field_string_methods = { |
| 651 | .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen, |
| 652 | .validate = bt_ctf_field_common_generic_validate, |
| 653 | .copy = NULL, |
| 654 | .is_set = bt_ctf_field_common_generic_is_set, |
| 655 | .reset = bt_ctf_field_common_generic_reset, |
| 656 | }; |
| 657 | |
| 658 | static struct bt_ctf_field_common_methods bt_ctf_field_structure_methods = { |
| 659 | .set_is_frozen = bt_ctf_field_common_structure_set_is_frozen_recursive, |
| 660 | .validate = bt_ctf_field_common_structure_validate_recursive, |
| 661 | .copy = NULL, |
| 662 | .is_set = bt_ctf_field_common_structure_is_set_recursive, |
| 663 | .reset = bt_ctf_field_common_structure_reset_recursive, |
| 664 | }; |
| 665 | |
| 666 | static struct bt_ctf_field_common_methods bt_ctf_field_sequence_methods = { |
| 667 | .set_is_frozen = bt_ctf_field_common_sequence_set_is_frozen_recursive, |
| 668 | .validate = bt_ctf_field_common_sequence_validate_recursive, |
| 669 | .copy = NULL, |
| 670 | .is_set = bt_ctf_field_common_sequence_is_set_recursive, |
| 671 | .reset = bt_ctf_field_common_sequence_reset_recursive, |
| 672 | }; |
| 673 | |
| 674 | static struct bt_ctf_field_common_methods bt_ctf_field_array_methods = { |
| 675 | .set_is_frozen = bt_ctf_field_common_array_set_is_frozen_recursive, |
| 676 | .validate = bt_ctf_field_common_array_validate_recursive, |
| 677 | .copy = NULL, |
| 678 | .is_set = bt_ctf_field_common_array_is_set_recursive, |
| 679 | .reset = bt_ctf_field_common_array_reset_recursive, |
| 680 | }; |
| 681 | |
| 682 | static |
| 683 | void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_ctf_field_common *field, |
| 684 | bool is_frozen); |
| 685 | |
| 686 | static |
| 687 | int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field); |
| 688 | |
| 689 | static |
| 690 | bt_ctf_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field); |
| 691 | |
| 692 | static |
| 693 | void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field); |
| 694 | |
| 695 | static struct bt_ctf_field_common_methods bt_ctf_field_variant_methods = { |
| 696 | .set_is_frozen = bt_ctf_field_variant_set_is_frozen_recursive, |
| 697 | .validate = bt_ctf_field_variant_validate_recursive, |
| 698 | .copy = NULL, |
| 699 | .is_set = bt_ctf_field_variant_is_set_recursive, |
| 700 | .reset = bt_ctf_field_variant_reset_recursive, |
| 701 | }; |
| 702 | |
| 703 | static |
| 704 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); |
| 705 | |
| 706 | static |
| 707 | struct bt_ctf_field *bt_ctf_field_enumeration_create(struct bt_ctf_field_type *); |
| 708 | |
| 709 | static |
| 710 | struct bt_ctf_field *bt_ctf_field_floating_point_create(struct bt_ctf_field_type *); |
| 711 | |
| 712 | static |
| 713 | struct bt_ctf_field *bt_ctf_field_structure_create(struct bt_ctf_field_type *); |
| 714 | |
| 715 | static |
| 716 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *); |
| 717 | |
| 718 | static |
| 719 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *); |
| 720 | |
| 721 | static |
| 722 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *); |
| 723 | |
| 724 | static |
| 725 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); |
| 726 | |
| 727 | static |
| 728 | struct bt_ctf_field *(* const field_create_funcs[])(struct bt_ctf_field_type *) = { |
| 729 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, |
| 730 | [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, |
| 731 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_create, |
| 732 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, |
| 733 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, |
| 734 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create, |
| 735 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, |
| 736 | [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create, |
| 737 | }; |
| 738 | |
| 739 | typedef int (*bt_ctf_field_serialize_recursive_func)( |
| 740 | struct bt_ctf_field_common *, struct bt_ctfser *, |
| 741 | enum bt_ctf_byte_order); |
| 742 | |
| 743 | static |
| 744 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) |
| 745 | { |
| 746 | BT_LOGD("Destroying CTF writer integer field object: addr=%p", field); |
| 747 | bt_ctf_field_common_integer_finalize((void *) field); |
| 748 | g_free(field); |
| 749 | } |
| 750 | |
| 751 | static |
| 752 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) |
| 753 | { |
| 754 | BT_LOGD("Destroying CTF writer floating point field object: addr=%p", |
| 755 | field); |
| 756 | bt_ctf_field_common_floating_point_finalize((void *) field); |
| 757 | g_free(field); |
| 758 | } |
| 759 | |
| 760 | static |
| 761 | void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field *field) |
| 762 | { |
| 763 | struct bt_ctf_field_enumeration *enumeration = BT_CTF_FROM_COMMON(field); |
| 764 | |
| 765 | BT_LOGD("Destroying CTF writer enumeration field object: addr=%p", |
| 766 | field); |
| 767 | BT_LOGD_STR("Putting container field."); |
| 768 | bt_ctf_object_put_ref(enumeration->container); |
| 769 | bt_ctf_field_common_finalize((void *) field); |
| 770 | g_free(field); |
| 771 | } |
| 772 | |
| 773 | static |
| 774 | void bt_ctf_field_structure_destroy_recursive(struct bt_ctf_field *field) |
| 775 | { |
| 776 | BT_LOGD("Destroying CTF writer structure field object: addr=%p", field); |
| 777 | bt_ctf_field_common_structure_finalize_recursive((void *) field); |
| 778 | g_free(field); |
| 779 | } |
| 780 | |
| 781 | static |
| 782 | void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field *field) |
| 783 | { |
| 784 | struct bt_ctf_field_variant *variant = BT_CTF_FROM_COMMON(field); |
| 785 | |
| 786 | BT_LOGD("Destroying CTF writer variant field object: addr=%p", field); |
| 787 | BT_LOGD_STR("Putting tag field."); |
| 788 | bt_ctf_object_put_ref(variant->tag); |
| 789 | bt_ctf_field_common_variant_finalize_recursive((void *) field); |
| 790 | g_free(field); |
| 791 | } |
| 792 | |
| 793 | static |
| 794 | void bt_ctf_field_array_destroy_recursive(struct bt_ctf_field *field) |
| 795 | { |
| 796 | BT_LOGD("Destroying CTF writer array field object: addr=%p", field); |
| 797 | bt_ctf_field_common_array_finalize_recursive((void *) field); |
| 798 | g_free(field); |
| 799 | } |
| 800 | |
| 801 | static |
| 802 | void bt_ctf_field_sequence_destroy_recursive(struct bt_ctf_field *field) |
| 803 | { |
| 804 | BT_LOGD("Destroying CTF writer sequence field object: addr=%p", field); |
| 805 | bt_ctf_field_common_sequence_finalize_recursive((void *) field); |
| 806 | g_free(field); |
| 807 | } |
| 808 | |
| 809 | static |
| 810 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) |
| 811 | { |
| 812 | BT_LOGD("Destroying CTF writer string field object: addr=%p", field); |
| 813 | bt_ctf_field_common_string_finalize((void *) field); |
| 814 | g_free(field); |
| 815 | } |
| 816 | |
| 817 | int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field, |
| 818 | struct bt_ctfser *ctfser, |
| 819 | enum bt_ctf_byte_order native_byte_order) |
| 820 | { |
| 821 | struct bt_ctf_field_common *field_common = (void *) field; |
| 822 | bt_ctf_field_serialize_recursive_func serialize_func; |
| 823 | |
| 824 | BT_ASSERT_DBG(ctfser); |
| 825 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Field"); |
| 826 | BT_ASSERT_DBG(field_common->spec.writer.serialize_func); |
| 827 | serialize_func = field_common->spec.writer.serialize_func; |
| 828 | return serialize_func(field_common, ctfser, |
| 829 | native_byte_order); |
| 830 | } |
| 831 | |
| 832 | static |
| 833 | int bt_ctf_field_integer_serialize(struct bt_ctf_field_common *field, |
| 834 | struct bt_ctfser *ctfser, |
| 835 | enum bt_ctf_byte_order native_byte_order) |
| 836 | { |
| 837 | int ret; |
| 838 | struct bt_ctf_field_type_common_integer *int_type = |
| 839 | BT_CTF_FROM_COMMON(field->type); |
| 840 | struct bt_ctf_field_common_integer *int_field = |
| 841 | BT_CTF_FROM_COMMON(field); |
| 842 | enum bt_ctf_byte_order byte_order; |
| 843 | |
| 844 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Integer field"); |
| 845 | BT_LOGT("Serializing CTF writer integer field: addr=%p, native-bo=%s", |
| 846 | field, |
| 847 | bt_ctf_byte_order_string(native_byte_order)); |
| 848 | byte_order = int_type->user_byte_order; |
| 849 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
| 850 | byte_order = native_byte_order; |
| 851 | } |
| 852 | |
| 853 | if (int_type->is_signed) { |
| 854 | ret = bt_ctfser_write_signed_int(ctfser, |
| 855 | int_field->payload.signd, int_type->common.alignment, |
| 856 | int_type->size, |
| 857 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? |
| 858 | LITTLE_ENDIAN : BIG_ENDIAN); |
| 859 | } else { |
| 860 | ret = bt_ctfser_write_unsigned_int(ctfser, |
| 861 | int_field->payload.unsignd, int_type->common.alignment, |
| 862 | int_type->size, |
| 863 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? |
| 864 | LITTLE_ENDIAN : BIG_ENDIAN); |
| 865 | } |
| 866 | |
| 867 | if (G_UNLIKELY(ret)) { |
| 868 | BT_LOGE("Cannot serialize integer field: ret=%d", ret); |
| 869 | goto end; |
| 870 | } |
| 871 | |
| 872 | end: |
| 873 | return ret; |
| 874 | } |
| 875 | |
| 876 | static |
| 877 | int bt_ctf_field_enumeration_serialize_recursive( |
| 878 | struct bt_ctf_field_common *field, struct bt_ctfser *ctfser, |
| 879 | enum bt_ctf_byte_order native_byte_order) |
| 880 | { |
| 881 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 882 | |
| 883 | BT_LOGT("Serializing enumeration field: addr=%p, native-bo=%s", |
| 884 | field, bt_ctf_byte_order_string(native_byte_order)); |
| 885 | BT_LOGT_STR("Serializing enumeration field's payload field."); |
| 886 | return bt_ctf_field_serialize_recursive( |
| 887 | (void *) enumeration->container, ctfser, native_byte_order); |
| 888 | } |
| 889 | |
| 890 | static |
| 891 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field_common *field, |
| 892 | struct bt_ctfser *ctfser, |
| 893 | enum bt_ctf_byte_order native_byte_order) |
| 894 | { |
| 895 | int ret = -1; |
| 896 | struct bt_ctf_field_type_common_floating_point *flt_type = |
| 897 | BT_CTF_FROM_COMMON(field->type); |
| 898 | struct bt_ctf_field_common_floating_point *flt_field = BT_CTF_FROM_COMMON(field); |
| 899 | enum bt_ctf_byte_order byte_order; |
| 900 | |
| 901 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Floating point number field"); |
| 902 | BT_LOGT("Serializing floating point number field: " |
| 903 | "addr=%p, native-bo=%s", field, |
| 904 | bt_ctf_byte_order_string(native_byte_order)); |
| 905 | |
| 906 | byte_order = flt_type->user_byte_order; |
| 907 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
| 908 | byte_order = native_byte_order; |
| 909 | } |
| 910 | |
| 911 | if (flt_type->mant_dig == FLT_MANT_DIG) { |
| 912 | ret = bt_ctfser_write_float32(ctfser, flt_field->payload, |
| 913 | flt_type->common.alignment, |
| 914 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? |
| 915 | LITTLE_ENDIAN : BIG_ENDIAN); |
| 916 | } else if (flt_type->mant_dig == DBL_MANT_DIG) { |
| 917 | ret = bt_ctfser_write_float64(ctfser, flt_field->payload, |
| 918 | flt_type->common.alignment, |
| 919 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? |
| 920 | LITTLE_ENDIAN : BIG_ENDIAN); |
| 921 | } else { |
| 922 | bt_common_abort(); |
| 923 | } |
| 924 | |
| 925 | if (G_UNLIKELY(ret)) { |
| 926 | BT_LOGE("Cannot serialize floating point number field: " |
| 927 | "ret=%d", ret); |
| 928 | goto end; |
| 929 | } |
| 930 | |
| 931 | end: |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | static |
| 936 | int bt_ctf_field_structure_serialize_recursive(struct bt_ctf_field_common *field, |
| 937 | struct bt_ctfser *ctfser, |
| 938 | enum bt_ctf_byte_order native_byte_order) |
| 939 | { |
| 940 | int64_t i; |
| 941 | int ret; |
| 942 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); |
| 943 | |
| 944 | BT_LOGT("Serializing structure field: addr=%p, native-bo=%s", |
| 945 | field, bt_ctf_byte_order_string(native_byte_order)); |
| 946 | ret = bt_ctfser_align_offset_in_current_packet(ctfser, |
| 947 | field->type->alignment); |
| 948 | if (G_UNLIKELY(ret)) { |
| 949 | BT_LOGE("Cannot align offset before serializing structure field: " |
| 950 | "ret=%d", ret); |
| 951 | goto end; |
| 952 | } |
| 953 | |
| 954 | for (i = 0; i < structure->fields->len; i++) { |
| 955 | struct bt_ctf_field_common *member = g_ptr_array_index( |
| 956 | structure->fields, i); |
| 957 | const char *field_name = NULL; |
| 958 | |
| 959 | BT_LOGT("Serializing structure field's field: ser-offset=%" PRIu64 ", " |
| 960 | "field-addr=%p, index=%" PRIu64, |
| 961 | bt_ctfser_get_offset_in_current_packet_bits(ctfser), |
| 962 | member, i); |
| 963 | |
| 964 | if (G_UNLIKELY(!member)) { |
| 965 | ret = bt_ctf_field_type_common_structure_borrow_field_by_index( |
| 966 | field->type, &field_name, NULL, i); |
| 967 | BT_ASSERT_DBG(ret == 0); |
| 968 | BT_LOGW("Cannot serialize structure field's field: field is not set: " |
| 969 | "struct-field-addr=%p, " |
| 970 | "field-name=\"%s\", index=%" PRId64, |
| 971 | field, field_name, i); |
| 972 | ret = -1; |
| 973 | goto end; |
| 974 | } |
| 975 | |
| 976 | ret = bt_ctf_field_serialize_recursive((void *) member, ctfser, |
| 977 | native_byte_order); |
| 978 | if (G_UNLIKELY(ret)) { |
| 979 | ret = bt_ctf_field_type_common_structure_borrow_field_by_index( |
| 980 | field->type, &field_name, NULL, i); |
| 981 | BT_ASSERT_DBG(ret == 0); |
| 982 | BT_LOGW("Cannot serialize structure field's field: " |
| 983 | "struct-field-addr=%p, field-addr=%p, " |
| 984 | "field-name=\"%s\", index=%" PRId64, |
| 985 | field->type, member, field_name, i); |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | end: |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | static |
| 995 | int bt_ctf_field_variant_serialize_recursive(struct bt_ctf_field_common *field, |
| 996 | struct bt_ctfser *ctfser, |
| 997 | enum bt_ctf_byte_order native_byte_order) |
| 998 | { |
| 999 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); |
| 1000 | |
| 1001 | BT_LOGT("Serializing variant field: addr=%p, native-bo=%s", |
| 1002 | field, bt_ctf_byte_order_string(native_byte_order)); |
| 1003 | BT_LOGT_STR("Serializing variant field's payload field."); |
| 1004 | return bt_ctf_field_serialize_recursive( |
| 1005 | (void *) variant->current_field, ctfser, native_byte_order); |
| 1006 | } |
| 1007 | |
| 1008 | static |
| 1009 | int bt_ctf_field_array_serialize_recursive(struct bt_ctf_field_common *field, |
| 1010 | struct bt_ctfser *ctfser, |
| 1011 | enum bt_ctf_byte_order native_byte_order) |
| 1012 | { |
| 1013 | int64_t i; |
| 1014 | int ret = 0; |
| 1015 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); |
| 1016 | |
| 1017 | BT_LOGT("Serializing array field: addr=%p, native-bo=%s", |
| 1018 | field, bt_ctf_byte_order_string(native_byte_order)); |
| 1019 | |
| 1020 | for (i = 0; i < array->elements->len; i++) { |
| 1021 | struct bt_ctf_field_common *elem_field = |
| 1022 | g_ptr_array_index(array->elements, i); |
| 1023 | |
| 1024 | BT_LOGT("Serializing array field's element field: " |
| 1025 | "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64, |
| 1026 | bt_ctfser_get_offset_in_current_packet_bits(ctfser), |
| 1027 | elem_field, i); |
| 1028 | ret = bt_ctf_field_serialize_recursive( |
| 1029 | (void *) elem_field, ctfser, native_byte_order); |
| 1030 | if (G_UNLIKELY(ret)) { |
| 1031 | BT_LOGW("Cannot serialize array field's element field: " |
| 1032 | "array-field-addr=%p, field-addr=%p, " |
| 1033 | "index=%" PRId64, field, elem_field, i); |
| 1034 | goto end; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | end: |
| 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | static |
| 1043 | int bt_ctf_field_sequence_serialize_recursive(struct bt_ctf_field_common *field, |
| 1044 | struct bt_ctfser *ctfser, |
| 1045 | enum bt_ctf_byte_order native_byte_order) |
| 1046 | { |
| 1047 | int64_t i; |
| 1048 | int ret = 0; |
| 1049 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); |
| 1050 | |
| 1051 | BT_LOGT("Serializing sequence field: addr=%p, native-bo=%s", |
| 1052 | field, bt_ctf_byte_order_string(native_byte_order)); |
| 1053 | |
| 1054 | for (i = 0; i < sequence->elements->len; i++) { |
| 1055 | struct bt_ctf_field_common *elem_field = |
| 1056 | g_ptr_array_index(sequence->elements, i); |
| 1057 | |
| 1058 | BT_LOGT("Serializing sequence field's element field: " |
| 1059 | "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64, |
| 1060 | bt_ctfser_get_offset_in_current_packet_bits(ctfser), |
| 1061 | elem_field, i); |
| 1062 | ret = bt_ctf_field_serialize_recursive( |
| 1063 | (void *) elem_field, ctfser, native_byte_order); |
| 1064 | if (G_UNLIKELY(ret)) { |
| 1065 | BT_LOGW("Cannot serialize sequence field's element field: " |
| 1066 | "sequence-field-addr=%p, field-addr=%p, " |
| 1067 | "index=%" PRId64, field, elem_field, i); |
| 1068 | goto end; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | end: |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | static |
| 1077 | int bt_ctf_field_string_serialize(struct bt_ctf_field_common *field, |
| 1078 | struct bt_ctfser *ctfser, |
| 1079 | enum bt_ctf_byte_order native_byte_order) |
| 1080 | { |
| 1081 | int ret; |
| 1082 | struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field); |
| 1083 | |
| 1084 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "String field"); |
| 1085 | BT_LOGT("Serializing string field: addr=%p, native-bo=%s", |
| 1086 | field, bt_ctf_byte_order_string((int) native_byte_order)); |
| 1087 | ret = bt_ctfser_write_string(ctfser, (const char *) string->buf->data); |
| 1088 | if (G_UNLIKELY(ret)) { |
| 1089 | BT_LOGE("Cannot serialize string field: ret=%d", ret); |
| 1090 | goto end; |
| 1091 | } |
| 1092 | |
| 1093 | end: |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | BT_EXPORT |
| 1098 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) |
| 1099 | { |
| 1100 | struct bt_ctf_field *field = NULL; |
| 1101 | enum bt_ctf_field_type_id type_id; |
| 1102 | |
| 1103 | BT_CTF_ASSERT_PRE_NON_NULL(type, "Field type"); |
| 1104 | BT_ASSERT_DBG(field_type_common_has_known_id((void *) type)); |
| 1105 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_validate((void *) type) == 0, |
| 1106 | "Field type is invalid: ft-addr=%p", type); |
| 1107 | type_id = bt_ctf_field_type_get_type_id(type); |
| 1108 | field = field_create_funcs[type_id](type); |
| 1109 | if (!field) { |
| 1110 | goto end; |
| 1111 | } |
| 1112 | |
| 1113 | bt_ctf_field_type_common_freeze((void *) type); |
| 1114 | |
| 1115 | end: |
| 1116 | return field; |
| 1117 | } |
| 1118 | |
| 1119 | BT_EXPORT |
| 1120 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) |
| 1121 | { |
| 1122 | return bt_ctf_object_get_ref(bt_ctf_field_common_borrow_type((void *) field)); |
| 1123 | } |
| 1124 | |
| 1125 | BT_EXPORT |
| 1126 | enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) |
| 1127 | { |
| 1128 | struct bt_ctf_field_common *field_common = (void *) field; |
| 1129 | |
| 1130 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Field"); |
| 1131 | return (int) field_common->type->id; |
| 1132 | } |
| 1133 | |
| 1134 | BT_EXPORT |
| 1135 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
| 1136 | struct bt_ctf_field *length_field) |
| 1137 | { |
| 1138 | int ret; |
| 1139 | struct bt_ctf_field_common *common_length_field = (void *) length_field; |
| 1140 | uint64_t length; |
| 1141 | |
| 1142 | BT_CTF_ASSERT_PRE_NON_NULL(length_field, "Length field"); |
| 1143 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) length_field, "Length field"); |
| 1144 | BT_CTF_ASSERT_PRE(common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER || |
| 1145 | common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM, |
| 1146 | "Length field must be an integer or enumeration field: field-addr=%p", |
| 1147 | length_field); |
| 1148 | |
| 1149 | if (common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 1150 | struct bt_ctf_field_enumeration *enumeration = (void *) |
| 1151 | length_field; |
| 1152 | |
| 1153 | length_field = (void *) enumeration->container; |
| 1154 | } |
| 1155 | |
| 1156 | ret = bt_ctf_field_integer_unsigned_get_value(length_field, &length); |
| 1157 | BT_ASSERT_DBG(ret == 0); |
| 1158 | return bt_ctf_field_common_sequence_set_length((void *) field, |
| 1159 | length, (bt_ctf_field_common_create_func) bt_ctf_field_create); |
| 1160 | } |
| 1161 | |
| 1162 | BT_EXPORT |
| 1163 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( |
| 1164 | struct bt_ctf_field *field, uint64_t index) |
| 1165 | { |
| 1166 | return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_index( |
| 1167 | (void *) field, index)); |
| 1168 | } |
| 1169 | |
| 1170 | BT_EXPORT |
| 1171 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name( |
| 1172 | struct bt_ctf_field *field, const char *name) |
| 1173 | { |
| 1174 | return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_name( |
| 1175 | (void *) field, name)); |
| 1176 | } |
| 1177 | |
| 1178 | BT_EXPORT |
| 1179 | struct bt_ctf_field *bt_ctf_field_array_get_field( |
| 1180 | struct bt_ctf_field *field, uint64_t index) |
| 1181 | { |
| 1182 | return bt_ctf_object_get_ref( |
| 1183 | bt_ctf_field_common_array_borrow_field((void *) field, index)); |
| 1184 | } |
| 1185 | |
| 1186 | BT_EXPORT |
| 1187 | struct bt_ctf_field *bt_ctf_field_sequence_get_field( |
| 1188 | struct bt_ctf_field *field, uint64_t index) |
| 1189 | { |
| 1190 | return bt_ctf_object_get_ref( |
| 1191 | bt_ctf_field_common_sequence_borrow_field((void *) field, index)); |
| 1192 | } |
| 1193 | |
| 1194 | BT_EXPORT |
| 1195 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, |
| 1196 | struct bt_ctf_field *tag_field) |
| 1197 | { |
| 1198 | struct bt_ctf_field_variant *variant_field = (void *) field; |
| 1199 | struct bt_ctf_field_enumeration *enum_field = (void *) tag_field; |
| 1200 | struct bt_ctf_field_type_common_variant *variant_ft; |
| 1201 | struct bt_ctf_field_type_common_enumeration *tag_ft; |
| 1202 | struct bt_ctf_field *current_field = NULL; |
| 1203 | bt_ctf_bool is_signed; |
| 1204 | uint64_t tag_uval; |
| 1205 | int ret; |
| 1206 | |
| 1207 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Variant field"); |
| 1208 | BT_CTF_ASSERT_PRE_NON_NULL(tag_field, "Tag field"); |
| 1209 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) tag_field, "Tag field"); |
| 1210 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID( |
| 1211 | (struct bt_ctf_field_common *) tag_field, |
| 1212 | BT_CTF_FIELD_TYPE_ID_ENUM, "Tag field"); |
| 1213 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID( |
| 1214 | (struct bt_ctf_field_common *) field, |
| 1215 | BT_CTF_FIELD_TYPE_ID_VARIANT, "Field"); |
| 1216 | BT_CTF_ASSERT_PRE( |
| 1217 | bt_ctf_field_common_validate_recursive((void *) tag_field) == 0, |
| 1218 | "Tag field is invalid: field-addr=%p", tag_field); |
| 1219 | variant_ft = BT_CTF_FROM_COMMON(variant_field->common.common.type); |
| 1220 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare( |
| 1221 | BT_CTF_TO_COMMON(variant_ft->tag_ft), enum_field->common.type) == 0, |
| 1222 | "Unexpected tag field's type: expected-ft-addr=%p, " |
| 1223 | "tag-ft-addr=%p", variant_ft->tag_ft, |
| 1224 | enum_field->common.type); |
| 1225 | tag_ft = BT_CTF_FROM_COMMON(enum_field->common.type); |
| 1226 | is_signed = tag_ft->container_ft->is_signed; |
| 1227 | |
| 1228 | if (is_signed) { |
| 1229 | int64_t tag_ival; |
| 1230 | |
| 1231 | ret = bt_ctf_field_integer_signed_get_value( |
| 1232 | (void *) enum_field->container, &tag_ival); |
| 1233 | tag_uval = (uint64_t) tag_ival; |
| 1234 | } else { |
| 1235 | ret = bt_ctf_field_integer_unsigned_get_value( |
| 1236 | (void *) enum_field->container, &tag_uval); |
| 1237 | } |
| 1238 | |
| 1239 | BT_ASSERT_DBG(ret == 0); |
| 1240 | ret = bt_ctf_field_common_variant_set_tag((void *) field, tag_uval, |
| 1241 | is_signed); |
| 1242 | if (ret) { |
| 1243 | goto end; |
| 1244 | } |
| 1245 | |
| 1246 | bt_ctf_object_put_ref(variant_field->tag); |
| 1247 | variant_field->tag = bt_ctf_object_get_ref(tag_field); |
| 1248 | current_field = bt_ctf_field_variant_get_current_field(field); |
| 1249 | BT_ASSERT_DBG(current_field); |
| 1250 | |
| 1251 | end: |
| 1252 | return current_field; |
| 1253 | } |
| 1254 | |
| 1255 | BT_EXPORT |
| 1256 | struct bt_ctf_field *bt_ctf_field_variant_get_current_field( |
| 1257 | struct bt_ctf_field *variant_field) |
| 1258 | { |
| 1259 | return bt_ctf_object_get_ref(bt_ctf_field_common_variant_borrow_current_field( |
| 1260 | (void *) variant_field)); |
| 1261 | } |
| 1262 | |
| 1263 | struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container( |
| 1264 | struct bt_ctf_field *field) |
| 1265 | { |
| 1266 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1267 | |
| 1268 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Enumeration field"); |
| 1269 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID((struct bt_ctf_field_common *) field, |
| 1270 | BT_CTF_FIELD_TYPE_ID_ENUM, "Field"); |
| 1271 | BT_ASSERT_DBG(enumeration->container); |
| 1272 | return (void *) enumeration->container; |
| 1273 | } |
| 1274 | |
| 1275 | BT_EXPORT |
| 1276 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( |
| 1277 | struct bt_ctf_field *field) |
| 1278 | { |
| 1279 | return bt_ctf_object_get_ref(bt_ctf_field_enumeration_borrow_container(field)); |
| 1280 | } |
| 1281 | |
| 1282 | BT_EXPORT |
| 1283 | int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field, |
| 1284 | int64_t *value) |
| 1285 | { |
| 1286 | struct bt_ctf_field_common_integer *integer = (void *) field; |
| 1287 | |
| 1288 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 1289 | BT_CTF_ASSERT_PRE_NON_NULL(value, "Value"); |
| 1290 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer), "Integer field"); |
| 1291 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), |
| 1292 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); |
| 1293 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_integer_is_signed( |
| 1294 | integer->common.type), |
| 1295 | "Field's type is unsigned: field-addr=%p", field); |
| 1296 | *value = integer->payload.signd; |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | BT_EXPORT |
| 1301 | int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field, |
| 1302 | int64_t value) |
| 1303 | { |
| 1304 | int ret = 0; |
| 1305 | struct bt_ctf_field_common_integer *integer = (void *) field; |
| 1306 | struct bt_ctf_field_type_common_integer *integer_type; |
| 1307 | |
| 1308 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 1309 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer), "Integer field"); |
| 1310 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), |
| 1311 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); |
| 1312 | integer_type = BT_CTF_FROM_COMMON(integer->common.type); |
| 1313 | BT_CTF_ASSERT_PRE( |
| 1314 | bt_ctf_field_type_common_integer_is_signed(integer->common.type), |
| 1315 | "Field's type is unsigned: field-addr=%p", field); |
| 1316 | BT_CTF_ASSERT_PRE(value_is_in_range_signed(integer_type->size, value), |
| 1317 | "Value is out of bounds: value=%" PRId64 ", field-addr=%p", |
| 1318 | value, field); |
| 1319 | integer->payload.signd = value; |
| 1320 | bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true); |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
| 1324 | BT_EXPORT |
| 1325 | int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field, |
| 1326 | uint64_t *value) |
| 1327 | { |
| 1328 | struct bt_ctf_field_common_integer *integer = (void *) field; |
| 1329 | |
| 1330 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 1331 | BT_CTF_ASSERT_PRE_NON_NULL(value, "Value"); |
| 1332 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer), "Integer field"); |
| 1333 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), |
| 1334 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); |
| 1335 | BT_CTF_ASSERT_PRE( |
| 1336 | !bt_ctf_field_type_common_integer_is_signed(integer->common.type), |
| 1337 | "Field's type is signed: field-addr=%p", field); |
| 1338 | *value = integer->payload.unsignd; |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | BT_EXPORT |
| 1343 | int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field, |
| 1344 | uint64_t value) |
| 1345 | { |
| 1346 | struct bt_ctf_field_common_integer *integer = (void *) field; |
| 1347 | struct bt_ctf_field_type_common_integer *integer_type; |
| 1348 | |
| 1349 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 1350 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer), "Integer field"); |
| 1351 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), |
| 1352 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); |
| 1353 | integer_type = BT_CTF_FROM_COMMON(integer->common.type); |
| 1354 | BT_CTF_ASSERT_PRE( |
| 1355 | !bt_ctf_field_type_common_integer_is_signed(integer->common.type), |
| 1356 | "Field's type is signed: field-addr=%p", field); |
| 1357 | BT_CTF_ASSERT_PRE(value_is_in_range_unsigned(integer_type->size, value), |
| 1358 | "Value is out of bounds: value=%" PRIu64 ", field-addr=%p", |
| 1359 | value, field); |
| 1360 | integer->payload.unsignd = value; |
| 1361 | bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true); |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | BT_EXPORT |
| 1366 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, |
| 1367 | double *value) |
| 1368 | { |
| 1369 | return bt_ctf_field_common_floating_point_get_value((void *) field, value); |
| 1370 | } |
| 1371 | |
| 1372 | BT_EXPORT |
| 1373 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, |
| 1374 | double value) |
| 1375 | { |
| 1376 | return bt_ctf_field_common_floating_point_set_value((void *) field, value); |
| 1377 | } |
| 1378 | |
| 1379 | BT_EXPORT |
| 1380 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) |
| 1381 | { |
| 1382 | return bt_ctf_field_common_string_get_value((void *) field); |
| 1383 | } |
| 1384 | |
| 1385 | BT_EXPORT |
| 1386 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value) |
| 1387 | { |
| 1388 | return bt_ctf_field_common_string_set_value((void *) field, value); |
| 1389 | } |
| 1390 | |
| 1391 | BT_EXPORT |
| 1392 | int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value) |
| 1393 | { |
| 1394 | return bt_ctf_field_common_string_append((void *) field, value); |
| 1395 | } |
| 1396 | |
| 1397 | BT_EXPORT |
| 1398 | int bt_ctf_field_string_append_len(struct bt_ctf_field *field, |
| 1399 | const char *value, unsigned int length) |
| 1400 | { |
| 1401 | return bt_ctf_field_common_string_append_len((void *) field, value, length); |
| 1402 | } |
| 1403 | |
| 1404 | BT_EXPORT |
| 1405 | struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) |
| 1406 | { |
| 1407 | return (void *) bt_ctf_field_common_copy((void *) field); |
| 1408 | } |
| 1409 | |
| 1410 | static |
| 1411 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) |
| 1412 | { |
| 1413 | struct bt_ctf_field_common_integer *integer = |
| 1414 | g_new0(struct bt_ctf_field_common_integer, 1); |
| 1415 | |
| 1416 | BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type); |
| 1417 | |
| 1418 | if (integer) { |
| 1419 | bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(integer), (void *) type, |
| 1420 | true, |
| 1421 | (bt_ctf_object_release_func) bt_ctf_field_integer_destroy, |
| 1422 | &bt_ctf_field_integer_methods); |
| 1423 | integer->common.spec.writer.serialize_func = |
| 1424 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_integer_serialize; |
| 1425 | BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p", |
| 1426 | integer, type); |
| 1427 | } else { |
| 1428 | BT_LOGE_STR("Failed to allocate one integer field."); |
| 1429 | } |
| 1430 | |
| 1431 | return (void *) integer; |
| 1432 | } |
| 1433 | |
| 1434 | static |
| 1435 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 1436 | struct bt_ctf_field_type *type) |
| 1437 | { |
| 1438 | struct bt_ctf_field_type_common_enumeration *enum_ft = (void *) type; |
| 1439 | struct bt_ctf_field_enumeration *enumeration = g_new0( |
| 1440 | struct bt_ctf_field_enumeration, 1); |
| 1441 | |
| 1442 | BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type); |
| 1443 | |
| 1444 | if (!enumeration) { |
| 1445 | BT_LOGE_STR("Failed to allocate one enumeration field."); |
| 1446 | goto end; |
| 1447 | } |
| 1448 | |
| 1449 | bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(enumeration), |
| 1450 | (void *) type, |
| 1451 | true, (bt_ctf_object_release_func) |
| 1452 | bt_ctf_field_enumeration_destroy_recursive, |
| 1453 | &bt_ctf_field_enumeration_methods); |
| 1454 | enumeration->container = (void *) bt_ctf_field_create( |
| 1455 | BT_CTF_FROM_COMMON(enum_ft->container_ft)); |
| 1456 | if (!enumeration->container) { |
| 1457 | BT_CTF_OBJECT_PUT_REF_AND_RESET(enumeration); |
| 1458 | goto end; |
| 1459 | } |
| 1460 | |
| 1461 | enumeration->common.spec.writer.serialize_func = |
| 1462 | (bt_ctf_field_serialize_recursive_func) |
| 1463 | bt_ctf_field_enumeration_serialize_recursive; |
| 1464 | BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p", |
| 1465 | enumeration, type); |
| 1466 | |
| 1467 | end: |
| 1468 | return (void *) enumeration; |
| 1469 | } |
| 1470 | |
| 1471 | static |
| 1472 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 1473 | struct bt_ctf_field_type *type) |
| 1474 | { |
| 1475 | struct bt_ctf_field_common_floating_point *floating_point; |
| 1476 | |
| 1477 | BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type); |
| 1478 | floating_point = g_new0(struct bt_ctf_field_common_floating_point, 1); |
| 1479 | |
| 1480 | if (floating_point) { |
| 1481 | bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(floating_point), |
| 1482 | (void *) type, |
| 1483 | true, (bt_ctf_object_release_func) |
| 1484 | bt_ctf_field_floating_point_destroy, |
| 1485 | &bt_ctf_field_floating_point_methods); |
| 1486 | floating_point->common.spec.writer.serialize_func = |
| 1487 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_floating_point_serialize; |
| 1488 | BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p", |
| 1489 | floating_point, type); |
| 1490 | } else { |
| 1491 | BT_LOGE_STR("Failed to allocate one floating point number field."); |
| 1492 | } |
| 1493 | |
| 1494 | return (void *) floating_point; |
| 1495 | } |
| 1496 | |
| 1497 | static |
| 1498 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 1499 | struct bt_ctf_field_type *type) |
| 1500 | { |
| 1501 | struct bt_ctf_field_common_structure *structure = g_new0( |
| 1502 | struct bt_ctf_field_common_structure, 1); |
| 1503 | int iret; |
| 1504 | |
| 1505 | BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type); |
| 1506 | |
| 1507 | if (!structure) { |
| 1508 | BT_LOGE_STR("Failed to allocate one structure field."); |
| 1509 | goto end; |
| 1510 | } |
| 1511 | |
| 1512 | iret = bt_ctf_field_common_structure_initialize(BT_CTF_TO_COMMON(structure), |
| 1513 | (void *) type, |
| 1514 | true, (bt_ctf_object_release_func) |
| 1515 | bt_ctf_field_structure_destroy_recursive, |
| 1516 | &bt_ctf_field_structure_methods, |
| 1517 | (bt_ctf_field_common_create_func) bt_ctf_field_create, |
| 1518 | (GDestroyNotify) bt_ctf_object_put_ref); |
| 1519 | structure->common.spec.writer.serialize_func = |
| 1520 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive; |
| 1521 | if (iret) { |
| 1522 | BT_CTF_OBJECT_PUT_REF_AND_RESET(structure); |
| 1523 | goto end; |
| 1524 | } |
| 1525 | |
| 1526 | BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p", |
| 1527 | structure, type); |
| 1528 | |
| 1529 | end: |
| 1530 | return (void *) structure; |
| 1531 | } |
| 1532 | |
| 1533 | static |
| 1534 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) |
| 1535 | { |
| 1536 | struct bt_ctf_field_type_common_variant *var_ft = (void *) type; |
| 1537 | struct bt_ctf_field_variant *variant = g_new0( |
| 1538 | struct bt_ctf_field_variant, 1); |
| 1539 | |
| 1540 | BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type); |
| 1541 | |
| 1542 | if (!variant) { |
| 1543 | BT_LOGE_STR("Failed to allocate one variant field."); |
| 1544 | goto end; |
| 1545 | } |
| 1546 | |
| 1547 | bt_ctf_field_common_variant_initialize(BT_CTF_TO_COMMON(BT_CTF_TO_COMMON(variant)), |
| 1548 | (void *) type, |
| 1549 | true, (bt_ctf_object_release_func) |
| 1550 | bt_ctf_field_variant_destroy_recursive, |
| 1551 | &bt_ctf_field_variant_methods, |
| 1552 | (bt_ctf_field_common_create_func) bt_ctf_field_create, |
| 1553 | (GDestroyNotify) bt_ctf_object_put_ref); |
| 1554 | variant->tag = (void *) bt_ctf_field_create( |
| 1555 | BT_CTF_FROM_COMMON(var_ft->tag_ft)); |
| 1556 | variant->common.common.spec.writer.serialize_func = |
| 1557 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_variant_serialize_recursive; |
| 1558 | BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p", |
| 1559 | variant, type); |
| 1560 | |
| 1561 | end: |
| 1562 | return (void *) variant; |
| 1563 | } |
| 1564 | |
| 1565 | static |
| 1566 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) |
| 1567 | { |
| 1568 | struct bt_ctf_field_common_array *array = |
| 1569 | g_new0(struct bt_ctf_field_common_array, 1); |
| 1570 | int ret; |
| 1571 | |
| 1572 | BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type); |
| 1573 | BT_ASSERT_DBG(type); |
| 1574 | |
| 1575 | if (!array) { |
| 1576 | BT_LOGE_STR("Failed to allocate one array field."); |
| 1577 | goto end; |
| 1578 | } |
| 1579 | |
| 1580 | ret = bt_ctf_field_common_array_initialize(BT_CTF_TO_COMMON(array), |
| 1581 | (void *) type, |
| 1582 | true, (bt_ctf_object_release_func) |
| 1583 | bt_ctf_field_array_destroy_recursive, |
| 1584 | &bt_ctf_field_array_methods, |
| 1585 | (bt_ctf_field_common_create_func) bt_ctf_field_create, |
| 1586 | (GDestroyNotify) bt_ctf_object_put_ref); |
| 1587 | array->common.spec.writer.serialize_func = |
| 1588 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive; |
| 1589 | if (ret) { |
| 1590 | BT_CTF_OBJECT_PUT_REF_AND_RESET(array); |
| 1591 | goto end; |
| 1592 | } |
| 1593 | |
| 1594 | BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p", |
| 1595 | array, type); |
| 1596 | |
| 1597 | end: |
| 1598 | return (void *) array; |
| 1599 | } |
| 1600 | |
| 1601 | static |
| 1602 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *type) |
| 1603 | { |
| 1604 | struct bt_ctf_field_common_sequence *sequence = g_new0( |
| 1605 | struct bt_ctf_field_common_sequence, 1); |
| 1606 | |
| 1607 | BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type); |
| 1608 | |
| 1609 | if (sequence) { |
| 1610 | bt_ctf_field_common_sequence_initialize(BT_CTF_TO_COMMON(sequence), |
| 1611 | (void *) type, |
| 1612 | true, (bt_ctf_object_release_func) |
| 1613 | bt_ctf_field_sequence_destroy_recursive, |
| 1614 | &bt_ctf_field_sequence_methods, |
| 1615 | (GDestroyNotify) bt_ctf_object_put_ref); |
| 1616 | sequence->common.spec.writer.serialize_func = |
| 1617 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_sequence_serialize_recursive; |
| 1618 | BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p", |
| 1619 | sequence, type); |
| 1620 | } else { |
| 1621 | BT_LOGE_STR("Failed to allocate one sequence field."); |
| 1622 | } |
| 1623 | |
| 1624 | return (void *) sequence; |
| 1625 | } |
| 1626 | |
| 1627 | static |
| 1628 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) |
| 1629 | { |
| 1630 | struct bt_ctf_field_common_string *string = g_new0( |
| 1631 | struct bt_ctf_field_common_string, 1); |
| 1632 | |
| 1633 | BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type); |
| 1634 | |
| 1635 | if (string) { |
| 1636 | bt_ctf_field_common_string_initialize(BT_CTF_TO_COMMON(string), |
| 1637 | (void *) type, |
| 1638 | true, (bt_ctf_object_release_func) |
| 1639 | bt_ctf_field_string_destroy, |
| 1640 | &bt_ctf_field_string_methods); |
| 1641 | string->common.spec.writer.serialize_func = |
| 1642 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_string_serialize; |
| 1643 | BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p", |
| 1644 | string, type); |
| 1645 | } else { |
| 1646 | BT_LOGE_STR("Failed to allocate one string field."); |
| 1647 | } |
| 1648 | |
| 1649 | return (void *) string; |
| 1650 | } |
| 1651 | |
| 1652 | static |
| 1653 | void bt_ctf_field_enumeration_set_is_frozen_recursive( |
| 1654 | struct bt_ctf_field_common *field, bool is_frozen) |
| 1655 | { |
| 1656 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1657 | |
| 1658 | if (enumeration->container) { |
| 1659 | bt_ctf_field_common_set_is_frozen_recursive( |
| 1660 | (void *) enumeration->container, is_frozen); |
| 1661 | } |
| 1662 | |
| 1663 | bt_ctf_field_common_generic_set_is_frozen((void *) field, is_frozen); |
| 1664 | } |
| 1665 | |
| 1666 | static |
| 1667 | int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field) |
| 1668 | { |
| 1669 | int ret = -1; |
| 1670 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1671 | |
| 1672 | if (enumeration->container) { |
| 1673 | ret = bt_ctf_field_common_validate_recursive( |
| 1674 | (void *) enumeration->container); |
| 1675 | } |
| 1676 | |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | static |
| 1681 | bt_ctf_bool bt_ctf_field_enumeration_is_set_recursive(struct bt_ctf_field_common *field) |
| 1682 | { |
| 1683 | bt_ctf_bool is_set = BT_CTF_FALSE; |
| 1684 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1685 | |
| 1686 | if (enumeration->container) { |
| 1687 | is_set = bt_ctf_field_common_is_set_recursive( |
| 1688 | (void *) enumeration->container); |
| 1689 | } |
| 1690 | |
| 1691 | return is_set; |
| 1692 | } |
| 1693 | |
| 1694 | static |
| 1695 | void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field) |
| 1696 | { |
| 1697 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1698 | |
| 1699 | if (enumeration->container) { |
| 1700 | bt_ctf_field_common_reset_recursive( |
| 1701 | (void *) enumeration->container); |
| 1702 | } |
| 1703 | |
| 1704 | bt_ctf_field_common_generic_reset((void *) field); |
| 1705 | } |
| 1706 | |
| 1707 | static |
| 1708 | void bt_ctf_field_variant_set_is_frozen_recursive( |
| 1709 | struct bt_ctf_field_common *field, bool is_frozen) |
| 1710 | { |
| 1711 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1712 | |
| 1713 | if (variant->tag) { |
| 1714 | bt_ctf_field_common_set_is_frozen_recursive( |
| 1715 | (void *) variant->tag, is_frozen); |
| 1716 | } |
| 1717 | |
| 1718 | bt_ctf_field_common_variant_set_is_frozen_recursive((void *) field, |
| 1719 | is_frozen); |
| 1720 | } |
| 1721 | |
| 1722 | static |
| 1723 | int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field) |
| 1724 | { |
| 1725 | int ret; |
| 1726 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1727 | |
| 1728 | if (variant->tag) { |
| 1729 | ret = bt_ctf_field_common_validate_recursive( |
| 1730 | (void *) variant->tag); |
| 1731 | if (ret) { |
| 1732 | goto end; |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | ret = bt_ctf_field_common_variant_validate_recursive((void *) field); |
| 1737 | |
| 1738 | end: |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
| 1742 | static |
| 1743 | bt_ctf_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field) |
| 1744 | { |
| 1745 | bt_ctf_bool is_set; |
| 1746 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1747 | |
| 1748 | if (variant->tag) { |
| 1749 | is_set = bt_ctf_field_common_is_set_recursive( |
| 1750 | (void *) variant->tag); |
| 1751 | if (is_set) { |
| 1752 | goto end; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | is_set = bt_ctf_field_common_variant_is_set_recursive((void *) field); |
| 1757 | |
| 1758 | end: |
| 1759 | return is_set; |
| 1760 | } |
| 1761 | |
| 1762 | static |
| 1763 | void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field) |
| 1764 | { |
| 1765 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1766 | |
| 1767 | if (variant->tag) { |
| 1768 | bt_ctf_field_common_reset_recursive( |
| 1769 | (void *) variant->tag); |
| 1770 | } |
| 1771 | |
| 1772 | bt_ctf_field_common_variant_reset_recursive((void *) field); |
| 1773 | } |
| 1774 | |
| 1775 | BT_CTF_ASSERT_PRE_FUNC |
| 1776 | static inline bool field_to_set_has_expected_type( |
| 1777 | struct bt_ctf_field_common *struct_field, |
| 1778 | const char *name, struct bt_ctf_field_common *value) |
| 1779 | { |
| 1780 | bool ret = true; |
| 1781 | struct bt_ctf_field_type_common *expected_field_type = NULL; |
| 1782 | |
| 1783 | expected_field_type = |
| 1784 | bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 1785 | struct_field->type, name); |
| 1786 | |
| 1787 | if (bt_ctf_field_type_common_compare(expected_field_type, value->type)) { |
| 1788 | BT_CTF_ASSERT_PRE_MSG("Value field's type is different from the expected field type: " |
| 1789 | "value-ft-addr=%p, expected-ft-addr=%p", value->type, |
| 1790 | expected_field_type); |
| 1791 | ret = false; |
| 1792 | goto end; |
| 1793 | } |
| 1794 | |
| 1795 | end: |
| 1796 | return ret; |
| 1797 | } |
| 1798 | |
| 1799 | int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field, |
| 1800 | const char *name, struct bt_ctf_field *value) |
| 1801 | { |
| 1802 | int ret = 0; |
| 1803 | GQuark field_quark; |
| 1804 | struct bt_ctf_field_common *common_field = (void *) field; |
| 1805 | struct bt_ctf_field_common_structure *structure = |
| 1806 | BT_CTF_FROM_COMMON(common_field); |
| 1807 | struct bt_ctf_field_common *common_value = (void *) value; |
| 1808 | size_t index; |
| 1809 | GHashTable *field_name_to_index; |
| 1810 | struct bt_ctf_field_type_common_structure *structure_ft; |
| 1811 | |
| 1812 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Parent field"); |
| 1813 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Field name"); |
| 1814 | BT_CTF_ASSERT_PRE_NON_NULL(value, "Value field"); |
| 1815 | BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(common_field, |
| 1816 | BT_CTF_FIELD_TYPE_ID_STRUCT, "Parent field"); |
| 1817 | BT_CTF_ASSERT_PRE(field_to_set_has_expected_type(common_field, |
| 1818 | name, common_value), |
| 1819 | "Value field's type is different from the expected field type."); |
| 1820 | field_quark = g_quark_from_string(name); |
| 1821 | structure_ft = BT_CTF_FROM_COMMON(common_field->type); |
| 1822 | field_name_to_index = structure_ft->field_name_to_index; |
| 1823 | if (!g_hash_table_lookup_extended(field_name_to_index, |
| 1824 | GUINT_TO_POINTER(field_quark), NULL, |
| 1825 | (gpointer *) &index)) { |
| 1826 | BT_LOGT("Invalid parameter: no such field in structure field's type: " |
| 1827 | "struct-field-addr=%p, struct-ft-addr=%p, " |
| 1828 | "field-ft-addr=%p, name=\"%s\"", |
| 1829 | field, common_field->type, common_value->type, name); |
| 1830 | ret = -1; |
| 1831 | goto end; |
| 1832 | } |
| 1833 | bt_ctf_object_get_ref(value); |
| 1834 | BT_CTF_OBJECT_MOVE_REF(structure->fields->pdata[index], value); |
| 1835 | |
| 1836 | end: |
| 1837 | return ret; |
| 1838 | } |