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