| 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 <babeltrace/lib-logging-internal.h> |
| 27 | |
| 28 | #include <babeltrace/compat/fcntl-internal.h> |
| 29 | #include <babeltrace/ctf-writer/fields-internal.h> |
| 30 | #include <babeltrace/ctf-writer/field-types-internal.h> |
| 31 | #include <babeltrace/ctf-writer/serialize-internal.h> |
| 32 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 33 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 34 | #include <babeltrace/assert-pre-internal.h> |
| 35 | #include <babeltrace/object-internal.h> |
| 36 | #include <babeltrace/ref.h> |
| 37 | #include <babeltrace/compiler-internal.h> |
| 38 | #include <babeltrace/endian-internal.h> |
| 39 | #include <babeltrace/assert-internal.h> |
| 40 | #include <float.h> |
| 41 | #include <inttypes.h> |
| 42 | #include <stdlib.h> |
| 43 | |
| 44 | static struct bt_field_common_methods bt_ctf_field_integer_methods = { |
| 45 | .set_is_frozen = bt_field_common_generic_set_is_frozen, |
| 46 | .validate = bt_field_common_generic_validate, |
| 47 | .copy = NULL, |
| 48 | .is_set = bt_field_common_generic_is_set, |
| 49 | .reset = bt_field_common_generic_reset, |
| 50 | }; |
| 51 | |
| 52 | static struct bt_field_common_methods bt_ctf_field_floating_point_methods = { |
| 53 | .set_is_frozen = bt_field_common_generic_set_is_frozen, |
| 54 | .validate = bt_field_common_generic_validate, |
| 55 | .copy = NULL, |
| 56 | .is_set = bt_field_common_generic_is_set, |
| 57 | .reset = bt_field_common_generic_reset, |
| 58 | }; |
| 59 | |
| 60 | static |
| 61 | void bt_ctf_field_enumeration_set_is_frozen_recursive( |
| 62 | struct bt_field_common *field, bool is_frozen); |
| 63 | |
| 64 | static |
| 65 | int bt_ctf_field_enumeration_validate_recursive(struct bt_field_common *field); |
| 66 | |
| 67 | static |
| 68 | bt_bool bt_ctf_field_enumeration_is_set_recursive( |
| 69 | struct bt_field_common *field); |
| 70 | |
| 71 | static |
| 72 | void bt_ctf_field_enumeration_reset_recursive(struct bt_field_common *field); |
| 73 | |
| 74 | static struct bt_field_common_methods bt_ctf_field_enumeration_methods = { |
| 75 | .set_is_frozen = bt_ctf_field_enumeration_set_is_frozen_recursive, |
| 76 | .validate = bt_ctf_field_enumeration_validate_recursive, |
| 77 | .copy = NULL, |
| 78 | .is_set = bt_ctf_field_enumeration_is_set_recursive, |
| 79 | .reset = bt_ctf_field_enumeration_reset_recursive, |
| 80 | }; |
| 81 | |
| 82 | static struct bt_field_common_methods bt_ctf_field_string_methods = { |
| 83 | .set_is_frozen = bt_field_common_generic_set_is_frozen, |
| 84 | .validate = bt_field_common_generic_validate, |
| 85 | .copy = NULL, |
| 86 | .is_set = bt_field_common_generic_is_set, |
| 87 | .reset = bt_field_common_generic_reset, |
| 88 | }; |
| 89 | |
| 90 | static struct bt_field_common_methods bt_ctf_field_structure_methods = { |
| 91 | .set_is_frozen = bt_field_common_structure_set_is_frozen_recursive, |
| 92 | .validate = bt_field_common_structure_validate_recursive, |
| 93 | .copy = NULL, |
| 94 | .is_set = bt_field_common_structure_is_set_recursive, |
| 95 | .reset = bt_field_common_structure_reset_recursive, |
| 96 | }; |
| 97 | |
| 98 | static struct bt_field_common_methods bt_ctf_field_sequence_methods = { |
| 99 | .set_is_frozen = bt_field_common_sequence_set_is_frozen_recursive, |
| 100 | .validate = bt_field_common_sequence_validate_recursive, |
| 101 | .copy = NULL, |
| 102 | .is_set = bt_field_common_sequence_is_set_recursive, |
| 103 | .reset = bt_field_common_sequence_reset_recursive, |
| 104 | }; |
| 105 | |
| 106 | static struct bt_field_common_methods bt_ctf_field_array_methods = { |
| 107 | .set_is_frozen = bt_field_common_array_set_is_frozen_recursive, |
| 108 | .validate = bt_field_common_array_validate_recursive, |
| 109 | .copy = NULL, |
| 110 | .is_set = bt_field_common_array_is_set_recursive, |
| 111 | .reset = bt_field_common_array_reset_recursive, |
| 112 | }; |
| 113 | |
| 114 | static |
| 115 | void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_field_common *field, |
| 116 | bool is_frozen); |
| 117 | |
| 118 | static |
| 119 | int bt_ctf_field_variant_validate_recursive(struct bt_field_common *field); |
| 120 | |
| 121 | static |
| 122 | bt_bool bt_ctf_field_variant_is_set_recursive(struct bt_field_common *field); |
| 123 | |
| 124 | static |
| 125 | void bt_ctf_field_variant_reset_recursive(struct bt_field_common *field); |
| 126 | |
| 127 | static struct bt_field_common_methods bt_ctf_field_variant_methods = { |
| 128 | .set_is_frozen = bt_ctf_field_variant_set_is_frozen_recursive, |
| 129 | .validate = bt_ctf_field_variant_validate_recursive, |
| 130 | .copy = NULL, |
| 131 | .is_set = bt_ctf_field_variant_is_set_recursive, |
| 132 | .reset = bt_ctf_field_variant_reset_recursive, |
| 133 | }; |
| 134 | |
| 135 | static |
| 136 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); |
| 137 | |
| 138 | static |
| 139 | struct bt_ctf_field *bt_ctf_field_enumeration_create(struct bt_ctf_field_type *); |
| 140 | |
| 141 | static |
| 142 | struct bt_ctf_field *bt_ctf_field_floating_point_create(struct bt_ctf_field_type *); |
| 143 | |
| 144 | static |
| 145 | struct bt_ctf_field *bt_ctf_field_structure_create(struct bt_ctf_field_type *); |
| 146 | |
| 147 | static |
| 148 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *); |
| 149 | |
| 150 | static |
| 151 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *); |
| 152 | |
| 153 | static |
| 154 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *); |
| 155 | |
| 156 | static |
| 157 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); |
| 158 | |
| 159 | static |
| 160 | struct bt_ctf_field *(* const field_create_funcs[])(struct bt_ctf_field_type *) = { |
| 161 | [BT_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, |
| 162 | [BT_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, |
| 163 | [BT_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_create, |
| 164 | [BT_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, |
| 165 | [BT_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, |
| 166 | [BT_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create, |
| 167 | [BT_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, |
| 168 | [BT_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create, |
| 169 | }; |
| 170 | |
| 171 | typedef int (*bt_ctf_field_serialize_recursive_func)( |
| 172 | struct bt_field_common *, struct bt_ctf_stream_pos *, |
| 173 | enum bt_ctf_byte_order); |
| 174 | |
| 175 | static |
| 176 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) |
| 177 | { |
| 178 | BT_LOGD("Destroying CTF writer integer field object: addr=%p", field); |
| 179 | bt_field_common_integer_finalize((void *) field); |
| 180 | g_free(field); |
| 181 | } |
| 182 | |
| 183 | static |
| 184 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) |
| 185 | { |
| 186 | BT_LOGD("Destroying CTF writer floating point field object: addr=%p", |
| 187 | field); |
| 188 | bt_field_common_floating_point_finalize((void *) field); |
| 189 | g_free(field); |
| 190 | } |
| 191 | |
| 192 | static |
| 193 | void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field *field) |
| 194 | { |
| 195 | struct bt_ctf_field_enumeration *enumeration = BT_FROM_COMMON(field); |
| 196 | |
| 197 | BT_LOGD("Destroying CTF writer enumeration field object: addr=%p", |
| 198 | field); |
| 199 | BT_LOGD_STR("Putting container field."); |
| 200 | bt_put(enumeration->container); |
| 201 | bt_field_common_finalize((void *) field); |
| 202 | g_free(field); |
| 203 | } |
| 204 | |
| 205 | static |
| 206 | void bt_ctf_field_structure_destroy_recursive(struct bt_ctf_field *field) |
| 207 | { |
| 208 | BT_LOGD("Destroying CTF writer structure field object: addr=%p", field); |
| 209 | bt_field_common_structure_finalize_recursive((void *) field); |
| 210 | g_free(field); |
| 211 | } |
| 212 | |
| 213 | static |
| 214 | void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field *field) |
| 215 | { |
| 216 | struct bt_ctf_field_variant *variant = BT_FROM_COMMON(field); |
| 217 | |
| 218 | BT_LOGD("Destroying CTF writer variant field object: addr=%p", field); |
| 219 | BT_LOGD_STR("Putting tag field."); |
| 220 | bt_put(variant->tag); |
| 221 | bt_field_common_variant_finalize_recursive((void *) field); |
| 222 | g_free(field); |
| 223 | } |
| 224 | |
| 225 | static |
| 226 | void bt_ctf_field_array_destroy_recursive(struct bt_ctf_field *field) |
| 227 | { |
| 228 | BT_LOGD("Destroying CTF writer array field object: addr=%p", field); |
| 229 | bt_field_common_array_finalize_recursive((void *) field); |
| 230 | g_free(field); |
| 231 | } |
| 232 | |
| 233 | static |
| 234 | void bt_ctf_field_sequence_destroy_recursive(struct bt_ctf_field *field) |
| 235 | { |
| 236 | BT_LOGD("Destroying CTF writer sequence field object: addr=%p", field); |
| 237 | bt_field_common_sequence_finalize_recursive((void *) field); |
| 238 | g_free(field); |
| 239 | } |
| 240 | |
| 241 | static |
| 242 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) |
| 243 | { |
| 244 | BT_LOGD("Destroying CTF writer string field object: addr=%p", field); |
| 245 | bt_field_common_string_finalize((void *) field); |
| 246 | g_free(field); |
| 247 | } |
| 248 | |
| 249 | BT_HIDDEN |
| 250 | int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field, |
| 251 | struct bt_ctf_stream_pos *pos, |
| 252 | enum bt_ctf_byte_order native_byte_order) |
| 253 | { |
| 254 | struct bt_field_common *field_common = (void *) field; |
| 255 | bt_ctf_field_serialize_recursive_func serialize_func; |
| 256 | |
| 257 | BT_ASSERT(pos); |
| 258 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
| 259 | BT_ASSERT(field_common->spec.writer.serialize_func); |
| 260 | serialize_func = field_common->spec.writer.serialize_func; |
| 261 | return serialize_func(field_common, pos, |
| 262 | native_byte_order); |
| 263 | } |
| 264 | |
| 265 | static |
| 266 | int increase_packet_size(struct bt_ctf_stream_pos *pos) |
| 267 | { |
| 268 | int ret; |
| 269 | |
| 270 | BT_ASSERT(pos); |
| 271 | BT_LOGV("Increasing packet size: pos-offset=%" PRId64 ", " |
| 272 | "cur-packet-size=%" PRIu64, |
| 273 | pos->offset, pos->packet_size); |
| 274 | ret = munmap_align(pos->base_mma); |
| 275 | if (ret) { |
| 276 | BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping", |
| 277 | ": ret=%d", ret); |
| 278 | goto end; |
| 279 | } |
| 280 | |
| 281 | pos->packet_size += PACKET_LEN_INCREMENT; |
| 282 | do { |
| 283 | ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, |
| 284 | pos->packet_size / CHAR_BIT); |
| 285 | } while (ret == EINTR); |
| 286 | if (ret) { |
| 287 | BT_LOGE_ERRNO("Failed to preallocate memory space", |
| 288 | ": ret=%d", ret); |
| 289 | errno = EINTR; |
| 290 | ret = -1; |
| 291 | goto end; |
| 292 | } |
| 293 | |
| 294 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
| 295 | pos->flags, pos->fd, pos->mmap_offset); |
| 296 | if (pos->base_mma == MAP_FAILED) { |
| 297 | BT_LOGE_ERRNO("Failed to perform an aligned memory mapping", |
| 298 | ": ret=%d", ret); |
| 299 | ret = -1; |
| 300 | } |
| 301 | |
| 302 | BT_LOGV("Increased packet size: pos-offset=%" PRId64 ", " |
| 303 | "new-packet-size=%" PRIu64, |
| 304 | pos->offset, pos->packet_size); |
| 305 | BT_ASSERT(pos->packet_size % 8 == 0); |
| 306 | |
| 307 | end: |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static |
| 312 | int bt_ctf_field_integer_serialize(struct bt_field_common *field, |
| 313 | struct bt_ctf_stream_pos *pos, |
| 314 | enum bt_ctf_byte_order native_byte_order) |
| 315 | { |
| 316 | int ret = 0; |
| 317 | |
| 318 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Integer field"); |
| 319 | BT_LOGV("Serializing CTF writer integer field: addr=%p, pos-offset=%" PRId64 ", " |
| 320 | "native-bo=%s", field, pos->offset, |
| 321 | bt_common_byte_order_string((int) native_byte_order)); |
| 322 | |
| 323 | retry: |
| 324 | ret = bt_ctf_field_integer_write(field, pos, native_byte_order); |
| 325 | if (ret == -EFAULT) { |
| 326 | /* |
| 327 | * The field is too large to fit in the current packet's |
| 328 | * remaining space. Bump the packet size and retry. |
| 329 | */ |
| 330 | ret = increase_packet_size(pos); |
| 331 | if (ret) { |
| 332 | BT_LOGE("Cannot increase packet size: ret=%d", ret); |
| 333 | goto end; |
| 334 | } |
| 335 | goto retry; |
| 336 | } |
| 337 | |
| 338 | end: |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static |
| 343 | int bt_ctf_field_enumeration_serialize_recursive(struct bt_field_common *field, |
| 344 | struct bt_ctf_stream_pos *pos, |
| 345 | enum bt_ctf_byte_order native_byte_order) |
| 346 | { |
| 347 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 348 | |
| 349 | BT_LOGV("Serializing enumeration field: addr=%p, pos-offset=%" PRId64 ", " |
| 350 | "native-bo=%s", field, pos->offset, |
| 351 | bt_common_byte_order_string((int) native_byte_order)); |
| 352 | BT_LOGV_STR("Serializing enumeration field's payload field."); |
| 353 | return bt_ctf_field_serialize_recursive( |
| 354 | (void *) enumeration->container, pos, native_byte_order); |
| 355 | } |
| 356 | |
| 357 | static |
| 358 | int bt_ctf_field_floating_point_serialize(struct bt_field_common *field, |
| 359 | struct bt_ctf_stream_pos *pos, |
| 360 | enum bt_ctf_byte_order native_byte_order) |
| 361 | { |
| 362 | int ret = 0; |
| 363 | |
| 364 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Floating point number field"); |
| 365 | BT_LOGV("Serializing floating point number field: addr=%p, pos-offset=%" PRId64 ", " |
| 366 | "native-bo=%s", field, pos->offset, |
| 367 | bt_common_byte_order_string((int) native_byte_order)); |
| 368 | |
| 369 | retry: |
| 370 | ret = bt_ctf_field_floating_point_write(field, pos, |
| 371 | native_byte_order); |
| 372 | if (ret == -EFAULT) { |
| 373 | /* |
| 374 | * The field is too large to fit in the current packet's |
| 375 | * remaining space. Bump the packet size and retry. |
| 376 | */ |
| 377 | ret = increase_packet_size(pos); |
| 378 | if (ret) { |
| 379 | BT_LOGE("Cannot increase packet size: ret=%d", ret); |
| 380 | goto end; |
| 381 | } |
| 382 | goto retry; |
| 383 | } |
| 384 | |
| 385 | end: |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static |
| 390 | int bt_ctf_field_structure_serialize_recursive(struct bt_field_common *field, |
| 391 | struct bt_ctf_stream_pos *pos, |
| 392 | enum bt_ctf_byte_order native_byte_order) |
| 393 | { |
| 394 | int64_t i; |
| 395 | int ret = 0; |
| 396 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); |
| 397 | |
| 398 | BT_LOGV("Serializing structure field: addr=%p, pos-offset=%" PRId64 ", " |
| 399 | "native-bo=%s", field, pos->offset, |
| 400 | bt_common_byte_order_string((int) native_byte_order)); |
| 401 | |
| 402 | while (!bt_ctf_stream_pos_access_ok(pos, |
| 403 | offset_align(pos->offset, field->type->alignment))) { |
| 404 | ret = increase_packet_size(pos); |
| 405 | if (ret) { |
| 406 | BT_LOGE("Cannot increase packet size: ret=%d", ret); |
| 407 | goto end; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (!bt_ctf_stream_pos_align(pos, field->type->alignment)) { |
| 412 | BT_LOGE("Cannot align packet's position: pos-offset=%" PRId64 ", " |
| 413 | "align=%u", pos->offset, field->type->alignment); |
| 414 | ret = -1; |
| 415 | goto end; |
| 416 | } |
| 417 | |
| 418 | for (i = 0; i < structure->fields->len; i++) { |
| 419 | struct bt_field_common *member = g_ptr_array_index( |
| 420 | structure->fields, i); |
| 421 | const char *field_name = NULL; |
| 422 | |
| 423 | BT_LOGV("Serializing structure field's field: pos-offset=%" PRId64 ", " |
| 424 | "field-addr=%p, index=%" PRId64, |
| 425 | pos->offset, member, i); |
| 426 | |
| 427 | if (!member) { |
| 428 | ret = bt_field_type_common_structure_borrow_field_by_index( |
| 429 | field->type, &field_name, NULL, i); |
| 430 | BT_ASSERT(ret == 0); |
| 431 | BT_LOGW("Cannot serialize structure field's field: field is not set: " |
| 432 | "struct-field-addr=%p, " |
| 433 | "field-name=\"%s\", index=%" PRId64, |
| 434 | field, field_name, i); |
| 435 | ret = -1; |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | ret = bt_ctf_field_serialize_recursive((void *) member, pos, |
| 440 | native_byte_order); |
| 441 | if (ret) { |
| 442 | ret = bt_field_type_common_structure_borrow_field_by_index( |
| 443 | field->type, &field_name, NULL, i); |
| 444 | BT_ASSERT(ret == 0); |
| 445 | BT_LOGW("Cannot serialize structure field's field: " |
| 446 | "struct-field-addr=%p, field-addr=%p, " |
| 447 | "field-name=\"%s\", index=%" PRId64, |
| 448 | field->type, member, field_name, i); |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | end: |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | static |
| 458 | int bt_ctf_field_variant_serialize_recursive(struct bt_field_common *field, |
| 459 | struct bt_ctf_stream_pos *pos, |
| 460 | enum bt_ctf_byte_order native_byte_order) |
| 461 | { |
| 462 | struct bt_field_common_variant *variant = BT_FROM_COMMON(field); |
| 463 | |
| 464 | BT_LOGV("Serializing variant field: addr=%p, pos-offset=%" PRId64 ", " |
| 465 | "native-bo=%s", field, pos->offset, |
| 466 | bt_common_byte_order_string((int) native_byte_order)); |
| 467 | BT_LOGV_STR("Serializing variant field's payload field."); |
| 468 | return bt_ctf_field_serialize_recursive( |
| 469 | (void *) variant->current_field, pos, native_byte_order); |
| 470 | } |
| 471 | |
| 472 | static |
| 473 | int bt_ctf_field_array_serialize_recursive(struct bt_field_common *field, |
| 474 | struct bt_ctf_stream_pos *pos, |
| 475 | enum bt_ctf_byte_order native_byte_order) |
| 476 | { |
| 477 | int64_t i; |
| 478 | int ret = 0; |
| 479 | struct bt_field_common_array *array = BT_FROM_COMMON(field); |
| 480 | |
| 481 | BT_LOGV("Serializing array field: addr=%p, pos-offset=%" PRId64 ", " |
| 482 | "native-bo=%s", field, pos->offset, |
| 483 | bt_common_byte_order_string((int) native_byte_order)); |
| 484 | |
| 485 | for (i = 0; i < array->elements->len; i++) { |
| 486 | struct bt_field_common *elem_field = |
| 487 | g_ptr_array_index(array->elements, i); |
| 488 | |
| 489 | BT_LOGV("Serializing array field's element field: " |
| 490 | "pos-offset=%" PRId64 ", field-addr=%p, index=%" PRId64, |
| 491 | pos->offset, elem_field, i); |
| 492 | ret = bt_ctf_field_serialize_recursive( |
| 493 | (void *) elem_field, pos, native_byte_order); |
| 494 | if (ret) { |
| 495 | BT_LOGW("Cannot serialize array field's element field: " |
| 496 | "array-field-addr=%p, field-addr=%p, " |
| 497 | "index=%" PRId64, field, elem_field, i); |
| 498 | goto end; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | end: |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | static |
| 507 | int bt_ctf_field_sequence_serialize_recursive(struct bt_field_common *field, |
| 508 | struct bt_ctf_stream_pos *pos, |
| 509 | enum bt_ctf_byte_order native_byte_order) |
| 510 | { |
| 511 | int64_t i; |
| 512 | int ret = 0; |
| 513 | struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field); |
| 514 | |
| 515 | BT_LOGV("Serializing sequence field: addr=%p, pos-offset=%" PRId64 ", " |
| 516 | "native-bo=%s", field, pos->offset, |
| 517 | bt_common_byte_order_string((int) native_byte_order)); |
| 518 | |
| 519 | for (i = 0; i < sequence->elements->len; i++) { |
| 520 | struct bt_field_common *elem_field = |
| 521 | g_ptr_array_index(sequence->elements, i); |
| 522 | |
| 523 | BT_LOGV("Serializing sequence field's element field: " |
| 524 | "pos-offset=%" PRId64 ", field-addr=%p, index=%" PRId64, |
| 525 | pos->offset, elem_field, i); |
| 526 | ret = bt_ctf_field_serialize_recursive( |
| 527 | (void *) elem_field, pos, native_byte_order); |
| 528 | if (ret) { |
| 529 | BT_LOGW("Cannot serialize sequence field's element field: " |
| 530 | "sequence-field-addr=%p, field-addr=%p, " |
| 531 | "index=%" PRId64, field, elem_field, i); |
| 532 | goto end; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | end: |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static |
| 541 | int bt_ctf_field_string_serialize(struct bt_field_common *field, |
| 542 | struct bt_ctf_stream_pos *pos, |
| 543 | enum bt_ctf_byte_order native_byte_order) |
| 544 | { |
| 545 | int64_t i; |
| 546 | int ret = 0; |
| 547 | struct bt_field_common_string *string = BT_FROM_COMMON(field); |
| 548 | struct bt_ctf_field_type *character_type = |
| 549 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 550 | struct bt_ctf_field *character; |
| 551 | |
| 552 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "String field"); |
| 553 | BT_LOGV("Serializing string field: addr=%p, pos-offset=%" PRId64 ", " |
| 554 | "native-bo=%s", field, pos->offset, |
| 555 | bt_common_byte_order_string((int) native_byte_order)); |
| 556 | |
| 557 | BT_LOGV_STR("Creating character field from string field's character field type."); |
| 558 | character = bt_ctf_field_create(character_type); |
| 559 | |
| 560 | for (i = 0; i < string->size + 1; i++) { |
| 561 | const uint64_t chr = (uint64_t) ((char *) string->buf->data)[i]; |
| 562 | |
| 563 | ret = bt_ctf_field_integer_unsigned_set_value(character, chr); |
| 564 | BT_ASSERT(ret == 0); |
| 565 | BT_LOGV("Serializing string field's character field: " |
| 566 | "pos-offset=%" PRId64 ", field-addr=%p, " |
| 567 | "index=%" PRId64 ", char-int=%" PRIu64, |
| 568 | pos->offset, character, i, chr); |
| 569 | ret = bt_ctf_field_integer_serialize( |
| 570 | (void *) character, pos, native_byte_order); |
| 571 | if (ret) { |
| 572 | BT_LOGW_STR("Cannot serialize character field."); |
| 573 | goto end; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | end: |
| 578 | bt_put(character); |
| 579 | bt_put(character_type); |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) |
| 584 | { |
| 585 | struct bt_ctf_field *field = NULL; |
| 586 | enum bt_ctf_field_type_id type_id; |
| 587 | |
| 588 | BT_ASSERT_PRE_NON_NULL(type, "Field type"); |
| 589 | BT_ASSERT(field_type_common_has_known_id((void *) type)); |
| 590 | BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0, |
| 591 | "Field type is invalid: %!+wF", type); |
| 592 | type_id = bt_ctf_field_type_get_type_id(type); |
| 593 | field = field_create_funcs[type_id](type); |
| 594 | if (!field) { |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | bt_field_type_common_freeze((void *) type); |
| 599 | |
| 600 | end: |
| 601 | return field; |
| 602 | } |
| 603 | |
| 604 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) |
| 605 | { |
| 606 | return bt_get(bt_field_common_borrow_type((void *) field)); |
| 607 | } |
| 608 | |
| 609 | enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) |
| 610 | { |
| 611 | struct bt_field_common *field_common = (void *) field; |
| 612 | |
| 613 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
| 614 | return (int) field_common->type->id; |
| 615 | } |
| 616 | |
| 617 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
| 618 | struct bt_ctf_field *length_field) |
| 619 | { |
| 620 | int ret; |
| 621 | struct bt_field_common *common_length_field = (void *) length_field; |
| 622 | uint64_t length; |
| 623 | |
| 624 | BT_ASSERT_PRE_NON_NULL(length_field, "Length field"); |
| 625 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET((void *) length_field, "Length field"); |
| 626 | BT_ASSERT_PRE(common_length_field->type->id == BT_FIELD_TYPE_ID_INTEGER || |
| 627 | common_length_field->type->id == BT_FIELD_TYPE_ID_ENUM, |
| 628 | "Length field must be an integer or enumeration field: %!+wf", |
| 629 | length_field); |
| 630 | |
| 631 | if (common_length_field->type->id == BT_FIELD_TYPE_ID_ENUM) { |
| 632 | struct bt_ctf_field_enumeration *enumeration = (void *) |
| 633 | length_field; |
| 634 | |
| 635 | length_field = (void *) enumeration->container; |
| 636 | } |
| 637 | |
| 638 | ret = bt_ctf_field_integer_unsigned_get_value(length_field, &length); |
| 639 | BT_ASSERT(ret == 0); |
| 640 | return bt_field_common_sequence_set_length((void *) field, |
| 641 | length, (bt_field_common_create_func) bt_ctf_field_create); |
| 642 | } |
| 643 | |
| 644 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( |
| 645 | struct bt_ctf_field *field, uint64_t index) |
| 646 | { |
| 647 | return bt_get(bt_field_common_structure_borrow_field_by_index( |
| 648 | (void *) field, index)); |
| 649 | } |
| 650 | |
| 651 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name( |
| 652 | struct bt_ctf_field *field, const char *name) |
| 653 | { |
| 654 | return bt_get(bt_field_common_structure_borrow_field_by_name( |
| 655 | (void *) field, name)); |
| 656 | } |
| 657 | |
| 658 | struct bt_ctf_field *bt_ctf_field_array_get_field( |
| 659 | struct bt_ctf_field *field, uint64_t index) |
| 660 | { |
| 661 | return bt_get( |
| 662 | bt_field_common_array_borrow_field((void *) field, index)); |
| 663 | } |
| 664 | |
| 665 | struct bt_ctf_field *bt_ctf_field_sequence_get_field( |
| 666 | struct bt_ctf_field *field, uint64_t index) |
| 667 | { |
| 668 | return bt_get( |
| 669 | bt_field_common_sequence_borrow_field((void *) field, index)); |
| 670 | } |
| 671 | |
| 672 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, |
| 673 | struct bt_ctf_field *tag_field) |
| 674 | { |
| 675 | struct bt_ctf_field_variant *variant_field = (void *) field; |
| 676 | struct bt_ctf_field_enumeration *enum_field = (void *) tag_field; |
| 677 | struct bt_field_type_common_variant *variant_ft; |
| 678 | struct bt_field_type_common_enumeration *tag_ft; |
| 679 | struct bt_ctf_field *current_field = NULL; |
| 680 | bt_bool is_signed; |
| 681 | uint64_t tag_uval; |
| 682 | int ret; |
| 683 | |
| 684 | BT_ASSERT_PRE_NON_NULL(field, "Variant field"); |
| 685 | BT_ASSERT_PRE_NON_NULL(tag_field, "Tag field"); |
| 686 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET((void *) tag_field, "Tag field"); |
| 687 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID( |
| 688 | (struct bt_field_common *) tag_field, |
| 689 | BT_FIELD_TYPE_ID_ENUM, "Tag field"); |
| 690 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID( |
| 691 | (struct bt_field_common *) field, |
| 692 | BT_FIELD_TYPE_ID_VARIANT, "Field"); |
| 693 | BT_ASSERT_PRE( |
| 694 | bt_field_common_validate_recursive((void *) tag_field) == 0, |
| 695 | "Tag field is invalid: %!+wf", tag_field); |
| 696 | variant_ft = BT_FROM_COMMON(variant_field->common.common.type); |
| 697 | BT_ASSERT_PRE(bt_field_type_common_compare( |
| 698 | BT_TO_COMMON(variant_ft->tag_ft), enum_field->common.type) == 0, |
| 699 | "Unexpected tag field's type: %![expected-ft-]+wF, " |
| 700 | "%![tag-ft-]+wF", variant_ft->tag_ft, |
| 701 | enum_field->common.type); |
| 702 | tag_ft = BT_FROM_COMMON(enum_field->common.type); |
| 703 | is_signed = tag_ft->container_ft->is_signed; |
| 704 | |
| 705 | if (is_signed) { |
| 706 | int64_t tag_ival; |
| 707 | |
| 708 | ret = bt_ctf_field_integer_signed_get_value( |
| 709 | (void *) enum_field->container, &tag_ival); |
| 710 | tag_uval = (uint64_t) tag_ival; |
| 711 | } else { |
| 712 | ret = bt_ctf_field_integer_unsigned_get_value( |
| 713 | (void *) enum_field->container, &tag_uval); |
| 714 | } |
| 715 | |
| 716 | BT_ASSERT(ret == 0); |
| 717 | ret = bt_field_variant_common_set_tag((void *) field, tag_uval, |
| 718 | is_signed); |
| 719 | if (ret) { |
| 720 | goto end; |
| 721 | } |
| 722 | |
| 723 | bt_put(variant_field->tag); |
| 724 | variant_field->tag = bt_get(tag_field); |
| 725 | current_field = bt_ctf_field_variant_get_current_field(field); |
| 726 | BT_ASSERT(current_field); |
| 727 | |
| 728 | end: |
| 729 | return current_field; |
| 730 | } |
| 731 | |
| 732 | struct bt_ctf_field *bt_ctf_field_variant_get_current_field( |
| 733 | struct bt_ctf_field *variant_field) |
| 734 | { |
| 735 | return bt_get(bt_field_common_variant_borrow_current_field( |
| 736 | (void *) variant_field)); |
| 737 | } |
| 738 | |
| 739 | BT_HIDDEN |
| 740 | struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container( |
| 741 | struct bt_ctf_field *field) |
| 742 | { |
| 743 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 744 | |
| 745 | BT_ASSERT_PRE_NON_NULL(field, "Enumeration field"); |
| 746 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID((struct bt_field_common *) field, |
| 747 | BT_CTF_FIELD_TYPE_ID_ENUM, "Field"); |
| 748 | BT_ASSERT(enumeration->container); |
| 749 | return (void *) enumeration->container; |
| 750 | } |
| 751 | |
| 752 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( |
| 753 | struct bt_ctf_field *field) |
| 754 | { |
| 755 | return bt_get(bt_ctf_field_enumeration_borrow_container(field)); |
| 756 | } |
| 757 | |
| 758 | int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field, |
| 759 | int64_t *value) |
| 760 | { |
| 761 | struct bt_field_common_integer *integer = (void *) field; |
| 762 | |
| 763 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 764 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
| 765 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer), "Integer field"); |
| 766 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(BT_TO_COMMON(integer), |
| 767 | BT_FIELD_TYPE_ID_INTEGER, "Field"); |
| 768 | BT_ASSERT_PRE(bt_field_type_common_integer_is_signed( |
| 769 | integer->common.type), |
| 770 | "Field's type is unsigned: %!+_f", field); |
| 771 | *value = integer->payload.signd; |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field, |
| 776 | int64_t value) |
| 777 | { |
| 778 | int ret = 0; |
| 779 | struct bt_field_common_integer *integer = (void *) field; |
| 780 | struct bt_field_type_common_integer *integer_type; |
| 781 | |
| 782 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 783 | BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer), "Integer field"); |
| 784 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(BT_TO_COMMON(integer), |
| 785 | BT_FIELD_TYPE_ID_INTEGER, "Field"); |
| 786 | integer_type = BT_FROM_COMMON(integer->common.type); |
| 787 | BT_ASSERT_PRE( |
| 788 | bt_field_type_common_integer_is_signed(integer->common.type), |
| 789 | "Field's type is unsigned: %!+wf", field); |
| 790 | BT_ASSERT_PRE(value_is_in_range_signed(integer_type->size, value), |
| 791 | "Value is out of bounds: value=%" PRId64 ", %![field-]+wf", |
| 792 | value, field); |
| 793 | integer->payload.signd = value; |
| 794 | bt_field_common_set(BT_TO_COMMON(integer), true); |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field, |
| 799 | uint64_t *value) |
| 800 | { |
| 801 | struct bt_field_common_integer *integer = (void *) field; |
| 802 | |
| 803 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 804 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
| 805 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer), "Integer field"); |
| 806 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(BT_TO_COMMON(integer), |
| 807 | BT_FIELD_TYPE_ID_INTEGER, "Field"); |
| 808 | BT_ASSERT_PRE( |
| 809 | !bt_field_type_common_integer_is_signed(integer->common.type), |
| 810 | "Field's type is signed: %!+wf", field); |
| 811 | *value = integer->payload.unsignd; |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field, |
| 816 | uint64_t value) |
| 817 | { |
| 818 | struct bt_field_common_integer *integer = (void *) field; |
| 819 | struct bt_field_type_common_integer *integer_type; |
| 820 | |
| 821 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); |
| 822 | BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer), "Integer field"); |
| 823 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(BT_TO_COMMON(integer), |
| 824 | BT_FIELD_TYPE_ID_INTEGER, "Field"); |
| 825 | integer_type = BT_FROM_COMMON(integer->common.type); |
| 826 | BT_ASSERT_PRE( |
| 827 | !bt_field_type_common_integer_is_signed(integer->common.type), |
| 828 | "Field's type is signed: %!+wf", field); |
| 829 | BT_ASSERT_PRE(value_is_in_range_unsigned(integer_type->size, value), |
| 830 | "Value is out of bounds: value=%" PRIu64 ", %![field-]+wf", |
| 831 | value, field); |
| 832 | integer->payload.unsignd = value; |
| 833 | bt_field_common_set(BT_TO_COMMON(integer), true); |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, |
| 838 | double *value) |
| 839 | { |
| 840 | return bt_field_common_floating_point_get_value((void *) field, value); |
| 841 | } |
| 842 | |
| 843 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, |
| 844 | double value) |
| 845 | { |
| 846 | return bt_field_common_floating_point_set_value((void *) field, value); |
| 847 | } |
| 848 | |
| 849 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) |
| 850 | { |
| 851 | return bt_field_common_string_get_value((void *) field); |
| 852 | } |
| 853 | |
| 854 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value) |
| 855 | { |
| 856 | return bt_field_common_string_set_value((void *) field, value); |
| 857 | } |
| 858 | |
| 859 | int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value) |
| 860 | { |
| 861 | return bt_field_common_string_append((void *) field, value); |
| 862 | } |
| 863 | |
| 864 | int bt_ctf_field_string_append_len(struct bt_ctf_field *field, |
| 865 | const char *value, unsigned int length) |
| 866 | { |
| 867 | return bt_field_common_string_append_len((void *) field, value, length); |
| 868 | } |
| 869 | |
| 870 | struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) |
| 871 | { |
| 872 | return (void *) bt_field_common_copy((void *) field); |
| 873 | } |
| 874 | |
| 875 | static |
| 876 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) |
| 877 | { |
| 878 | struct bt_field_common_integer *integer = |
| 879 | g_new0(struct bt_field_common_integer, 1); |
| 880 | |
| 881 | BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type); |
| 882 | |
| 883 | if (integer) { |
| 884 | bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type, |
| 885 | true, |
| 886 | (bt_object_release_func) bt_ctf_field_integer_destroy, |
| 887 | &bt_ctf_field_integer_methods); |
| 888 | integer->common.spec.writer.serialize_func = |
| 889 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_integer_serialize; |
| 890 | BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p", |
| 891 | integer, type); |
| 892 | } else { |
| 893 | BT_LOGE_STR("Failed to allocate one integer field."); |
| 894 | } |
| 895 | |
| 896 | return (void *) integer; |
| 897 | } |
| 898 | |
| 899 | static |
| 900 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 901 | struct bt_ctf_field_type *type) |
| 902 | { |
| 903 | struct bt_field_type_common_enumeration *enum_ft = (void *) type; |
| 904 | struct bt_ctf_field_enumeration *enumeration = g_new0( |
| 905 | struct bt_ctf_field_enumeration, 1); |
| 906 | |
| 907 | BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type); |
| 908 | |
| 909 | if (!enumeration) { |
| 910 | BT_LOGE_STR("Failed to allocate one enumeration field."); |
| 911 | goto end; |
| 912 | } |
| 913 | |
| 914 | bt_field_common_initialize(BT_TO_COMMON(enumeration), |
| 915 | (void *) type, |
| 916 | true, (bt_object_release_func) |
| 917 | bt_ctf_field_enumeration_destroy_recursive, |
| 918 | &bt_ctf_field_enumeration_methods); |
| 919 | enumeration->container = (void *) bt_ctf_field_create( |
| 920 | BT_FROM_COMMON(enum_ft->container_ft)); |
| 921 | if (!enumeration->container) { |
| 922 | BT_PUT(enumeration); |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | enumeration->common.spec.writer.serialize_func = |
| 927 | (bt_ctf_field_serialize_recursive_func) |
| 928 | bt_ctf_field_enumeration_serialize_recursive; |
| 929 | BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p", |
| 930 | enumeration, type); |
| 931 | |
| 932 | end: |
| 933 | return (void *) enumeration; |
| 934 | } |
| 935 | |
| 936 | static |
| 937 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 938 | struct bt_ctf_field_type *type) |
| 939 | { |
| 940 | struct bt_field_common_floating_point *floating_point; |
| 941 | |
| 942 | BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type); |
| 943 | floating_point = g_new0(struct bt_field_common_floating_point, 1); |
| 944 | |
| 945 | if (floating_point) { |
| 946 | bt_field_common_initialize(BT_TO_COMMON(floating_point), |
| 947 | (void *) type, |
| 948 | true, (bt_object_release_func) |
| 949 | bt_ctf_field_floating_point_destroy, |
| 950 | &bt_ctf_field_floating_point_methods); |
| 951 | floating_point->common.spec.writer.serialize_func = |
| 952 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_floating_point_serialize; |
| 953 | BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p", |
| 954 | floating_point, type); |
| 955 | } else { |
| 956 | BT_LOGE_STR("Failed to allocate one floating point number field."); |
| 957 | } |
| 958 | |
| 959 | return (void *) floating_point; |
| 960 | } |
| 961 | |
| 962 | static |
| 963 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 964 | struct bt_ctf_field_type *type) |
| 965 | { |
| 966 | struct bt_field_common_structure *structure = g_new0( |
| 967 | struct bt_field_common_structure, 1); |
| 968 | int iret; |
| 969 | |
| 970 | BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type); |
| 971 | |
| 972 | if (!structure) { |
| 973 | BT_LOGE_STR("Failed to allocate one structure field."); |
| 974 | goto end; |
| 975 | } |
| 976 | |
| 977 | iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure), |
| 978 | (void *) type, |
| 979 | true, (bt_object_release_func) |
| 980 | bt_ctf_field_structure_destroy_recursive, |
| 981 | &bt_ctf_field_structure_methods, |
| 982 | (bt_field_common_create_func) bt_ctf_field_create, |
| 983 | (GDestroyNotify) bt_put); |
| 984 | structure->common.spec.writer.serialize_func = |
| 985 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive; |
| 986 | if (iret) { |
| 987 | BT_PUT(structure); |
| 988 | goto end; |
| 989 | } |
| 990 | |
| 991 | BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p", |
| 992 | structure, type); |
| 993 | |
| 994 | end: |
| 995 | return (void *) structure; |
| 996 | } |
| 997 | |
| 998 | static |
| 999 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) |
| 1000 | { |
| 1001 | struct bt_field_type_common_variant *var_ft = (void *) type; |
| 1002 | struct bt_ctf_field_variant *variant = g_new0( |
| 1003 | struct bt_ctf_field_variant, 1); |
| 1004 | |
| 1005 | BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type); |
| 1006 | |
| 1007 | if (!variant) { |
| 1008 | BT_LOGE_STR("Failed to allocate one variant field."); |
| 1009 | goto end; |
| 1010 | } |
| 1011 | |
| 1012 | bt_field_common_variant_initialize(BT_TO_COMMON(BT_TO_COMMON(variant)), |
| 1013 | (void *) type, |
| 1014 | true, (bt_object_release_func) |
| 1015 | bt_ctf_field_variant_destroy_recursive, |
| 1016 | &bt_ctf_field_variant_methods, |
| 1017 | (bt_field_common_create_func) bt_ctf_field_create, |
| 1018 | (GDestroyNotify) bt_put); |
| 1019 | variant->tag = (void *) bt_ctf_field_create( |
| 1020 | BT_FROM_COMMON(var_ft->tag_ft)); |
| 1021 | variant->common.common.spec.writer.serialize_func = |
| 1022 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_variant_serialize_recursive; |
| 1023 | BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p", |
| 1024 | variant, type); |
| 1025 | |
| 1026 | end: |
| 1027 | return (void *) variant; |
| 1028 | } |
| 1029 | |
| 1030 | static |
| 1031 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) |
| 1032 | { |
| 1033 | struct bt_field_common_array *array = |
| 1034 | g_new0(struct bt_field_common_array, 1); |
| 1035 | int ret; |
| 1036 | |
| 1037 | BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type); |
| 1038 | BT_ASSERT(type); |
| 1039 | |
| 1040 | if (!array) { |
| 1041 | BT_LOGE_STR("Failed to allocate one array field."); |
| 1042 | goto end; |
| 1043 | } |
| 1044 | |
| 1045 | ret = bt_field_common_array_initialize(BT_TO_COMMON(array), |
| 1046 | (void *) type, |
| 1047 | true, (bt_object_release_func) |
| 1048 | bt_ctf_field_array_destroy_recursive, |
| 1049 | &bt_ctf_field_array_methods, |
| 1050 | (bt_field_common_create_func) bt_ctf_field_create, |
| 1051 | (GDestroyNotify) bt_put); |
| 1052 | array->common.spec.writer.serialize_func = |
| 1053 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive; |
| 1054 | if (ret) { |
| 1055 | BT_PUT(array); |
| 1056 | goto end; |
| 1057 | } |
| 1058 | |
| 1059 | BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p", |
| 1060 | array, type); |
| 1061 | |
| 1062 | end: |
| 1063 | return (void *) array; |
| 1064 | } |
| 1065 | |
| 1066 | static |
| 1067 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *type) |
| 1068 | { |
| 1069 | struct bt_field_common_sequence *sequence = g_new0( |
| 1070 | struct bt_field_common_sequence, 1); |
| 1071 | |
| 1072 | BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type); |
| 1073 | |
| 1074 | if (sequence) { |
| 1075 | bt_field_common_sequence_initialize(BT_TO_COMMON(sequence), |
| 1076 | (void *) type, |
| 1077 | true, (bt_object_release_func) |
| 1078 | bt_ctf_field_sequence_destroy_recursive, |
| 1079 | &bt_ctf_field_sequence_methods, |
| 1080 | (GDestroyNotify) bt_put); |
| 1081 | sequence->common.spec.writer.serialize_func = |
| 1082 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_sequence_serialize_recursive; |
| 1083 | BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p", |
| 1084 | sequence, type); |
| 1085 | } else { |
| 1086 | BT_LOGE_STR("Failed to allocate one sequence field."); |
| 1087 | } |
| 1088 | |
| 1089 | return (void *) sequence; |
| 1090 | } |
| 1091 | |
| 1092 | static |
| 1093 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) |
| 1094 | { |
| 1095 | struct bt_field_common_string *string = g_new0( |
| 1096 | struct bt_field_common_string, 1); |
| 1097 | |
| 1098 | BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type); |
| 1099 | |
| 1100 | if (string) { |
| 1101 | bt_field_common_string_initialize(BT_TO_COMMON(string), |
| 1102 | (void *) type, |
| 1103 | true, (bt_object_release_func) |
| 1104 | bt_ctf_field_string_destroy, |
| 1105 | &bt_ctf_field_string_methods); |
| 1106 | string->common.spec.writer.serialize_func = |
| 1107 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_string_serialize; |
| 1108 | BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p", |
| 1109 | string, type); |
| 1110 | } else { |
| 1111 | BT_LOGE_STR("Failed to allocate one string field."); |
| 1112 | } |
| 1113 | |
| 1114 | return (void *) string; |
| 1115 | } |
| 1116 | |
| 1117 | static |
| 1118 | void bt_ctf_field_enumeration_set_is_frozen_recursive( |
| 1119 | struct bt_field_common *field, bool is_frozen) |
| 1120 | { |
| 1121 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1122 | |
| 1123 | if (enumeration->container) { |
| 1124 | bt_field_common_set_is_frozen_recursive( |
| 1125 | (void *) enumeration->container, is_frozen); |
| 1126 | } |
| 1127 | |
| 1128 | bt_field_common_generic_set_is_frozen((void *) field, is_frozen); |
| 1129 | } |
| 1130 | |
| 1131 | static |
| 1132 | int bt_ctf_field_enumeration_validate_recursive(struct bt_field_common *field) |
| 1133 | { |
| 1134 | int ret = -1; |
| 1135 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1136 | |
| 1137 | if (enumeration->container) { |
| 1138 | ret = bt_field_common_validate_recursive( |
| 1139 | (void *) enumeration->container); |
| 1140 | } |
| 1141 | |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
| 1145 | static |
| 1146 | bt_bool bt_ctf_field_enumeration_is_set_recursive(struct bt_field_common *field) |
| 1147 | { |
| 1148 | bt_bool is_set = BT_FALSE; |
| 1149 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1150 | |
| 1151 | if (enumeration->container) { |
| 1152 | is_set = bt_field_common_is_set_recursive( |
| 1153 | (void *) enumeration->container); |
| 1154 | } |
| 1155 | |
| 1156 | return is_set; |
| 1157 | } |
| 1158 | |
| 1159 | static |
| 1160 | void bt_ctf_field_enumeration_reset_recursive(struct bt_field_common *field) |
| 1161 | { |
| 1162 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
| 1163 | |
| 1164 | if (enumeration->container) { |
| 1165 | bt_field_common_reset_recursive( |
| 1166 | (void *) enumeration->container); |
| 1167 | } |
| 1168 | |
| 1169 | bt_field_common_generic_reset((void *) field); |
| 1170 | } |
| 1171 | |
| 1172 | static |
| 1173 | void bt_ctf_field_variant_set_is_frozen_recursive( |
| 1174 | struct bt_field_common *field, bool is_frozen) |
| 1175 | { |
| 1176 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1177 | |
| 1178 | if (variant->tag) { |
| 1179 | bt_field_common_set_is_frozen_recursive( |
| 1180 | (void *) variant->tag, is_frozen); |
| 1181 | } |
| 1182 | |
| 1183 | bt_field_common_variant_set_is_frozen_recursive((void *) field, |
| 1184 | is_frozen); |
| 1185 | } |
| 1186 | |
| 1187 | static |
| 1188 | int bt_ctf_field_variant_validate_recursive(struct bt_field_common *field) |
| 1189 | { |
| 1190 | int ret; |
| 1191 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1192 | |
| 1193 | if (variant->tag) { |
| 1194 | ret = bt_field_common_validate_recursive( |
| 1195 | (void *) variant->tag); |
| 1196 | if (ret) { |
| 1197 | goto end; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | ret = bt_field_common_variant_validate_recursive((void *) field); |
| 1202 | |
| 1203 | end: |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | static |
| 1208 | bt_bool bt_ctf_field_variant_is_set_recursive(struct bt_field_common *field) |
| 1209 | { |
| 1210 | bt_bool is_set; |
| 1211 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1212 | |
| 1213 | if (variant->tag) { |
| 1214 | is_set = bt_field_common_is_set_recursive( |
| 1215 | (void *) variant->tag); |
| 1216 | if (is_set) { |
| 1217 | goto end; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | is_set = bt_field_common_variant_is_set_recursive((void *) field); |
| 1222 | |
| 1223 | end: |
| 1224 | return is_set; |
| 1225 | } |
| 1226 | |
| 1227 | static |
| 1228 | void bt_ctf_field_variant_reset_recursive(struct bt_field_common *field) |
| 1229 | { |
| 1230 | struct bt_ctf_field_variant *variant = (void *) field; |
| 1231 | |
| 1232 | if (variant->tag) { |
| 1233 | bt_field_common_reset_recursive( |
| 1234 | (void *) variant->tag); |
| 1235 | } |
| 1236 | |
| 1237 | bt_field_common_variant_reset_recursive((void *) field); |
| 1238 | } |
| 1239 | |
| 1240 | BT_ASSERT_PRE_FUNC |
| 1241 | static inline bool field_to_set_has_expected_type( |
| 1242 | struct bt_field_common *struct_field, |
| 1243 | const char *name, struct bt_field_common *value) |
| 1244 | { |
| 1245 | bool ret = true; |
| 1246 | struct bt_field_type_common *expected_field_type = NULL; |
| 1247 | |
| 1248 | expected_field_type = |
| 1249 | bt_field_type_common_structure_borrow_field_type_by_name( |
| 1250 | struct_field->type, name); |
| 1251 | |
| 1252 | if (bt_field_type_common_compare(expected_field_type, value->type)) { |
| 1253 | BT_ASSERT_PRE_MSG("Value field's type is different from the expected field type: " |
| 1254 | "%![value-ft-]+_F, %![expected-ft-]+_F", value->type, |
| 1255 | expected_field_type); |
| 1256 | ret = false; |
| 1257 | goto end; |
| 1258 | } |
| 1259 | |
| 1260 | end: |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
| 1264 | BT_HIDDEN |
| 1265 | int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field, |
| 1266 | const char *name, struct bt_ctf_field *value) |
| 1267 | { |
| 1268 | int ret = 0; |
| 1269 | GQuark field_quark; |
| 1270 | struct bt_field_common *common_field = (void *) field; |
| 1271 | struct bt_field_common_structure *structure = |
| 1272 | BT_FROM_COMMON(common_field); |
| 1273 | struct bt_field_common *common_value = (void *) value; |
| 1274 | size_t index; |
| 1275 | GHashTable *field_name_to_index; |
| 1276 | struct bt_field_type_common_structure *structure_ft; |
| 1277 | |
| 1278 | BT_ASSERT_PRE_NON_NULL(field, "Parent field"); |
| 1279 | BT_ASSERT_PRE_NON_NULL(name, "Field name"); |
| 1280 | BT_ASSERT_PRE_NON_NULL(value, "Value field"); |
| 1281 | BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(common_field, |
| 1282 | BT_FIELD_TYPE_ID_STRUCT, "Parent field"); |
| 1283 | BT_ASSERT_PRE(field_to_set_has_expected_type(common_field, |
| 1284 | name, common_value), |
| 1285 | "Value field's type is different from the expected field type."); |
| 1286 | field_quark = g_quark_from_string(name); |
| 1287 | structure_ft = BT_FROM_COMMON(common_field->type); |
| 1288 | field_name_to_index = structure_ft->field_name_to_index; |
| 1289 | if (!g_hash_table_lookup_extended(field_name_to_index, |
| 1290 | GUINT_TO_POINTER(field_quark), NULL, |
| 1291 | (gpointer *) &index)) { |
| 1292 | BT_LOGV("Invalid parameter: no such field in structure field's type: " |
| 1293 | "struct-field-addr=%p, struct-ft-addr=%p, " |
| 1294 | "field-ft-addr=%p, name=\"%s\"", |
| 1295 | field, common_field->type, common_value->type, name); |
| 1296 | ret = -1; |
| 1297 | goto end; |
| 1298 | } |
| 1299 | bt_get(value); |
| 1300 | BT_MOVE(structure->fields->pdata[index], value); |
| 1301 | |
| 1302 | end: |
| 1303 | return ret; |
| 1304 | } |