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