| 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 | .freeze = bt_field_common_generic_freeze, |
| 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 | .freeze = bt_field_common_generic_freeze, |
| 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 struct bt_field_common_methods bt_ctf_field_enumeration_methods = { |
| 61 | .freeze = bt_field_common_enumeration_freeze_recursive, |
| 62 | .validate = bt_field_common_enumeration_validate_recursive, |
| 63 | .copy = NULL, |
| 64 | .is_set = bt_field_common_enumeration_is_set_recursive, |
| 65 | .reset = bt_field_common_enumeration_reset_recursive, |
| 66 | }; |
| 67 | |
| 68 | static struct bt_field_common_methods bt_ctf_field_string_methods = { |
| 69 | .freeze = bt_field_common_generic_freeze, |
| 70 | .validate = bt_field_common_generic_validate, |
| 71 | .copy = NULL, |
| 72 | .is_set = bt_field_common_generic_is_set, |
| 73 | .reset = bt_field_common_generic_reset, |
| 74 | }; |
| 75 | |
| 76 | static struct bt_field_common_methods bt_ctf_field_structure_methods = { |
| 77 | .freeze = bt_field_common_structure_freeze_recursive, |
| 78 | .validate = bt_field_common_structure_validate_recursive, |
| 79 | .copy = NULL, |
| 80 | .is_set = bt_field_common_structure_is_set_recursive, |
| 81 | .reset = bt_field_common_structure_reset_recursive, |
| 82 | }; |
| 83 | |
| 84 | static struct bt_field_common_methods bt_ctf_field_sequence_methods = { |
| 85 | .freeze = bt_field_common_sequence_freeze_recursive, |
| 86 | .validate = bt_field_common_sequence_validate_recursive, |
| 87 | .copy = NULL, |
| 88 | .is_set = bt_field_common_sequence_is_set_recursive, |
| 89 | .reset = bt_field_common_sequence_reset_recursive, |
| 90 | }; |
| 91 | |
| 92 | static struct bt_field_common_methods bt_ctf_field_array_methods = { |
| 93 | .freeze = bt_field_common_array_freeze_recursive, |
| 94 | .validate = bt_field_common_array_validate_recursive, |
| 95 | .copy = NULL, |
| 96 | .is_set = bt_field_common_array_is_set_recursive, |
| 97 | .reset = bt_field_common_array_reset_recursive, |
| 98 | }; |
| 99 | |
| 100 | static struct bt_field_common_methods bt_ctf_field_variant_methods = { |
| 101 | .freeze = bt_field_common_variant_freeze_recursive, |
| 102 | .validate = bt_field_common_variant_validate_recursive, |
| 103 | .copy = NULL, |
| 104 | .is_set = bt_field_common_variant_is_set_recursive, |
| 105 | .reset = bt_field_common_variant_reset_recursive, |
| 106 | }; |
| 107 | |
| 108 | static |
| 109 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); |
| 110 | |
| 111 | static |
| 112 | struct bt_ctf_field *bt_ctf_field_enumeration_create(struct bt_ctf_field_type *); |
| 113 | |
| 114 | static |
| 115 | struct bt_ctf_field *bt_ctf_field_floating_point_create(struct bt_ctf_field_type *); |
| 116 | |
| 117 | static |
| 118 | struct bt_ctf_field *bt_ctf_field_structure_create(struct bt_ctf_field_type *); |
| 119 | |
| 120 | static |
| 121 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *); |
| 122 | |
| 123 | static |
| 124 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *); |
| 125 | |
| 126 | static |
| 127 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *); |
| 128 | |
| 129 | static |
| 130 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); |
| 131 | |
| 132 | static |
| 133 | struct bt_ctf_field *(* const field_create_funcs[])(struct bt_ctf_field_type *) = { |
| 134 | [BT_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, |
| 135 | [BT_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, |
| 136 | [BT_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_create, |
| 137 | [BT_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, |
| 138 | [BT_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, |
| 139 | [BT_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create, |
| 140 | [BT_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, |
| 141 | [BT_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create, |
| 142 | }; |
| 143 | |
| 144 | typedef int (*bt_ctf_field_serialize_recursive_func)( |
| 145 | struct bt_field_common *, struct bt_ctf_stream_pos *, |
| 146 | enum bt_ctf_byte_order); |
| 147 | |
| 148 | BT_HIDDEN |
| 149 | int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field, |
| 150 | struct bt_ctf_stream_pos *pos, |
| 151 | enum bt_ctf_byte_order native_byte_order) |
| 152 | { |
| 153 | struct bt_field_common *field_common = (void *) field; |
| 154 | bt_ctf_field_serialize_recursive_func serialize_func; |
| 155 | |
| 156 | BT_ASSERT(pos); |
| 157 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
| 158 | BT_ASSERT(field_common->spec.writer.serialize_func); |
| 159 | serialize_func = field_common->spec.writer.serialize_func; |
| 160 | return serialize_func(field_common, pos, |
| 161 | native_byte_order); |
| 162 | } |
| 163 | |
| 164 | static |
| 165 | int increase_packet_size(struct bt_ctf_stream_pos *pos) |
| 166 | { |
| 167 | int ret; |
| 168 | |
| 169 | BT_ASSERT(pos); |
| 170 | BT_LOGV("Increasing packet size: pos-offset=%" PRId64 ", " |
| 171 | "cur-packet-size=%" PRIu64, |
| 172 | pos->offset, pos->packet_size); |
| 173 | ret = munmap_align(pos->base_mma); |
| 174 | if (ret) { |
| 175 | BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping", |
| 176 | ": ret=%d", ret); |
| 177 | goto end; |
| 178 | } |
| 179 | |
| 180 | pos->packet_size += PACKET_LEN_INCREMENT; |
| 181 | do { |
| 182 | ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, |
| 183 | pos->packet_size / CHAR_BIT); |
| 184 | } while (ret == EINTR); |
| 185 | if (ret) { |
| 186 | BT_LOGE_ERRNO("Failed to preallocate memory space", |
| 187 | ": ret=%d", ret); |
| 188 | errno = EINTR; |
| 189 | ret = -1; |
| 190 | goto end; |
| 191 | } |
| 192 | |
| 193 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
| 194 | pos->flags, pos->fd, pos->mmap_offset); |
| 195 | if (pos->base_mma == MAP_FAILED) { |
| 196 | BT_LOGE_ERRNO("Failed to perform an aligned memory mapping", |
| 197 | ": ret=%d", ret); |
| 198 | ret = -1; |
| 199 | } |
| 200 | |
| 201 | BT_LOGV("Increased packet size: pos-offset=%" PRId64 ", " |
| 202 | "new-packet-size=%" PRIu64, |
| 203 | pos->offset, pos->packet_size); |
| 204 | BT_ASSERT(pos->packet_size % 8 == 0); |
| 205 | |
| 206 | end: |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static |
| 211 | int bt_ctf_field_integer_serialize(struct bt_field_common *field, |
| 212 | struct bt_ctf_stream_pos *pos, |
| 213 | enum bt_ctf_byte_order native_byte_order) |
| 214 | { |
| 215 | int ret = 0; |
| 216 | |
| 217 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Integer field"); |
| 218 | BT_LOGV("Serializing CTF writer integer field: addr=%p, pos-offset=%" PRId64 ", " |
| 219 | "native-bo=%s", field, pos->offset, |
| 220 | bt_common_byte_order_string((int) native_byte_order)); |
| 221 | |
| 222 | retry: |
| 223 | ret = bt_ctf_field_integer_write(field, pos, native_byte_order); |
| 224 | if (ret == -EFAULT) { |
| 225 | /* |
| 226 | * The field is too large to fit in the current packet's |
| 227 | * remaining space. Bump the packet size and retry. |
| 228 | */ |
| 229 | ret = increase_packet_size(pos); |
| 230 | if (ret) { |
| 231 | BT_LOGE("Cannot increase packet size: ret=%d", ret); |
| 232 | goto end; |
| 233 | } |
| 234 | goto retry; |
| 235 | } |
| 236 | |
| 237 | end: |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | static |
| 242 | int bt_ctf_field_enumeration_serialize_recursive(struct bt_field_common *field, |
| 243 | struct bt_ctf_stream_pos *pos, |
| 244 | enum bt_ctf_byte_order native_byte_order) |
| 245 | { |
| 246 | struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field); |
| 247 | |
| 248 | BT_LOGV("Serializing enumeration field: addr=%p, pos-offset=%" PRId64 ", " |
| 249 | "native-bo=%s", field, pos->offset, |
| 250 | bt_common_byte_order_string((int) native_byte_order)); |
| 251 | BT_LOGV_STR("Serializing enumeration field's payload field."); |
| 252 | return bt_ctf_field_serialize_recursive((void *) enumeration->payload, |
| 253 | pos, native_byte_order); |
| 254 | } |
| 255 | |
| 256 | static |
| 257 | int bt_ctf_field_floating_point_serialize(struct bt_field_common *field, |
| 258 | struct bt_ctf_stream_pos *pos, |
| 259 | enum bt_ctf_byte_order native_byte_order) |
| 260 | { |
| 261 | int ret = 0; |
| 262 | |
| 263 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Floating point number field"); |
| 264 | BT_LOGV("Serializing floating point number field: addr=%p, pos-offset=%" PRId64 ", " |
| 265 | "native-bo=%s", field, pos->offset, |
| 266 | bt_common_byte_order_string((int) native_byte_order)); |
| 267 | |
| 268 | retry: |
| 269 | ret = bt_ctf_field_floating_point_write(field, pos, |
| 270 | native_byte_order); |
| 271 | if (ret == -EFAULT) { |
| 272 | /* |
| 273 | * The field is too large to fit in the current packet's |
| 274 | * remaining space. Bump the packet size and retry. |
| 275 | */ |
| 276 | ret = increase_packet_size(pos); |
| 277 | if (ret) { |
| 278 | BT_LOGE("Cannot increase packet size: ret=%d", ret); |
| 279 | goto end; |
| 280 | } |
| 281 | goto retry; |
| 282 | } |
| 283 | |
| 284 | end: |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static |
| 289 | int bt_ctf_field_structure_serialize_recursive(struct bt_field_common *field, |
| 290 | struct bt_ctf_stream_pos *pos, |
| 291 | enum bt_ctf_byte_order native_byte_order) |
| 292 | { |
| 293 | int64_t i; |
| 294 | int ret = 0; |
| 295 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); |
| 296 | |
| 297 | BT_LOGV("Serializing structure field: addr=%p, pos-offset=%" PRId64 ", " |
| 298 | "native-bo=%s", field, pos->offset, |
| 299 | bt_common_byte_order_string((int) native_byte_order)); |
| 300 | |
| 301 | while (!bt_ctf_stream_pos_access_ok(pos, |
| 302 | offset_align(pos->offset, field->type->alignment))) { |
| 303 | ret = increase_packet_size(pos); |
| 304 | if (ret) { |
| 305 | BT_LOGE("Cannot increase packet size: ret=%d", ret); |
| 306 | goto end; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (!bt_ctf_stream_pos_align(pos, field->type->alignment)) { |
| 311 | BT_LOGE("Cannot align packet's position: pos-offset=%" PRId64 ", " |
| 312 | "align=%u", pos->offset, field->type->alignment); |
| 313 | ret = -1; |
| 314 | goto end; |
| 315 | } |
| 316 | |
| 317 | for (i = 0; i < structure->fields->len; i++) { |
| 318 | struct bt_field_common *member = g_ptr_array_index( |
| 319 | structure->fields, i); |
| 320 | const char *field_name = NULL; |
| 321 | |
| 322 | BT_LOGV("Serializing structure field's field: pos-offset=%" PRId64 ", " |
| 323 | "field-addr=%p, index=%" PRId64, |
| 324 | pos->offset, member, i); |
| 325 | |
| 326 | if (!member) { |
| 327 | ret = bt_field_type_common_structure_get_field_by_index( |
| 328 | field->type, &field_name, NULL, i); |
| 329 | BT_ASSERT(ret == 0); |
| 330 | BT_LOGW("Cannot serialize structure field's field: field is not set: " |
| 331 | "struct-field-addr=%p, " |
| 332 | "field-name=\"%s\", index=%" PRId64, |
| 333 | field, field_name, i); |
| 334 | ret = -1; |
| 335 | goto end; |
| 336 | } |
| 337 | |
| 338 | ret = bt_ctf_field_serialize_recursive((void *) member, pos, |
| 339 | native_byte_order); |
| 340 | if (ret) { |
| 341 | ret = bt_field_type_common_structure_get_field_by_index( |
| 342 | field->type, &field_name, NULL, i); |
| 343 | BT_ASSERT(ret == 0); |
| 344 | BT_LOGW("Cannot serialize structure field's field: " |
| 345 | "struct-field-addr=%p, field-addr=%p, " |
| 346 | "field-name=\"%s\", index=%" PRId64, |
| 347 | field->type, member, field_name, i); |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | end: |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static |
| 357 | int bt_ctf_field_variant_serialize_recursive(struct bt_field_common *field, |
| 358 | struct bt_ctf_stream_pos *pos, |
| 359 | enum bt_ctf_byte_order native_byte_order) |
| 360 | { |
| 361 | struct bt_field_common_variant *variant = BT_FROM_COMMON(field); |
| 362 | |
| 363 | BT_LOGV("Serializing variant field: addr=%p, pos-offset=%" PRId64 ", " |
| 364 | "native-bo=%s", field, pos->offset, |
| 365 | bt_common_byte_order_string((int) native_byte_order)); |
| 366 | BT_LOGV_STR("Serializing variant field's payload field."); |
| 367 | return bt_ctf_field_serialize_recursive( |
| 368 | (void *) variant->payload, pos, native_byte_order); |
| 369 | } |
| 370 | |
| 371 | static |
| 372 | int bt_ctf_field_array_serialize_recursive(struct bt_field_common *field, |
| 373 | struct bt_ctf_stream_pos *pos, |
| 374 | enum bt_ctf_byte_order native_byte_order) |
| 375 | { |
| 376 | int64_t i; |
| 377 | int ret = 0; |
| 378 | struct bt_field_common_array *array = BT_FROM_COMMON(field); |
| 379 | |
| 380 | BT_LOGV("Serializing array field: addr=%p, pos-offset=%" PRId64 ", " |
| 381 | "native-bo=%s", field, pos->offset, |
| 382 | bt_common_byte_order_string((int) native_byte_order)); |
| 383 | |
| 384 | for (i = 0; i < array->elements->len; i++) { |
| 385 | struct bt_field_common *elem_field = |
| 386 | g_ptr_array_index(array->elements, i); |
| 387 | |
| 388 | BT_LOGV("Serializing array field's element field: " |
| 389 | "pos-offset=%" PRId64 ", field-addr=%p, index=%" PRId64, |
| 390 | pos->offset, elem_field, i); |
| 391 | ret = bt_ctf_field_serialize_recursive( |
| 392 | (void *) elem_field, pos, native_byte_order); |
| 393 | if (ret) { |
| 394 | BT_LOGW("Cannot serialize array field's element field: " |
| 395 | "array-field-addr=%p, field-addr=%p, " |
| 396 | "index=%" PRId64, field, elem_field, i); |
| 397 | goto end; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | end: |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | static |
| 406 | int bt_ctf_field_sequence_serialize_recursive(struct bt_field_common *field, |
| 407 | struct bt_ctf_stream_pos *pos, |
| 408 | enum bt_ctf_byte_order native_byte_order) |
| 409 | { |
| 410 | int64_t i; |
| 411 | int ret = 0; |
| 412 | struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field); |
| 413 | |
| 414 | BT_LOGV("Serializing sequence field: addr=%p, pos-offset=%" PRId64 ", " |
| 415 | "native-bo=%s", field, pos->offset, |
| 416 | bt_common_byte_order_string((int) native_byte_order)); |
| 417 | |
| 418 | for (i = 0; i < sequence->elements->len; i++) { |
| 419 | struct bt_field_common *elem_field = |
| 420 | g_ptr_array_index(sequence->elements, i); |
| 421 | |
| 422 | BT_LOGV("Serializing sequence field's element field: " |
| 423 | "pos-offset=%" PRId64 ", field-addr=%p, index=%" PRId64, |
| 424 | pos->offset, elem_field, i); |
| 425 | ret = bt_ctf_field_serialize_recursive( |
| 426 | (void *) elem_field, pos, native_byte_order); |
| 427 | if (ret) { |
| 428 | BT_LOGW("Cannot serialize sequence field's element field: " |
| 429 | "sequence-field-addr=%p, field-addr=%p, " |
| 430 | "index=%" PRId64, field, elem_field, i); |
| 431 | goto end; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | end: |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static |
| 440 | int bt_ctf_field_string_serialize(struct bt_field_common *field, |
| 441 | struct bt_ctf_stream_pos *pos, |
| 442 | enum bt_ctf_byte_order native_byte_order) |
| 443 | { |
| 444 | int64_t i; |
| 445 | int ret = 0; |
| 446 | struct bt_field_common_string *string = BT_FROM_COMMON(field); |
| 447 | struct bt_ctf_field_type *character_type = |
| 448 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 449 | struct bt_ctf_field *character; |
| 450 | |
| 451 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "String field"); |
| 452 | BT_LOGV("Serializing string field: addr=%p, pos-offset=%" PRId64 ", " |
| 453 | "native-bo=%s", field, pos->offset, |
| 454 | bt_common_byte_order_string((int) native_byte_order)); |
| 455 | |
| 456 | BT_LOGV_STR("Creating character field from string field's character field type."); |
| 457 | character = bt_ctf_field_create(character_type); |
| 458 | |
| 459 | for (i = 0; i < string->payload->len + 1; i++) { |
| 460 | const uint64_t chr = (uint64_t) string->payload->str[i]; |
| 461 | |
| 462 | ret = bt_ctf_field_integer_unsigned_set_value(character, chr); |
| 463 | BT_ASSERT(ret == 0); |
| 464 | BT_LOGV("Serializing string field's character field: " |
| 465 | "pos-offset=%" PRId64 ", field-addr=%p, " |
| 466 | "index=%" PRId64 ", char-int=%" PRIu64, |
| 467 | pos->offset, character, i, chr); |
| 468 | ret = bt_ctf_field_integer_serialize( |
| 469 | (void *) character, pos, native_byte_order); |
| 470 | if (ret) { |
| 471 | BT_LOGW_STR("Cannot serialize character field."); |
| 472 | goto end; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | end: |
| 477 | bt_put(character); |
| 478 | bt_put(character_type); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) |
| 483 | { |
| 484 | struct bt_ctf_field *field = NULL; |
| 485 | enum bt_ctf_field_type_id type_id; |
| 486 | |
| 487 | BT_ASSERT_PRE_NON_NULL(type, "Field type"); |
| 488 | BT_ASSERT(field_type_common_has_known_id((void *) type)); |
| 489 | BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0, |
| 490 | "Field type is invalid: %!+wF", type); |
| 491 | type_id = bt_ctf_field_type_get_type_id(type); |
| 492 | field = field_create_funcs[type_id](type); |
| 493 | if (!field) { |
| 494 | goto end; |
| 495 | } |
| 496 | |
| 497 | bt_field_type_common_freeze((void *) type); |
| 498 | |
| 499 | end: |
| 500 | return field; |
| 501 | } |
| 502 | |
| 503 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) |
| 504 | { |
| 505 | return (void *) bt_field_common_get_type((void *) field); |
| 506 | } |
| 507 | |
| 508 | enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) |
| 509 | { |
| 510 | struct bt_field_common *field_common = (void *) field; |
| 511 | |
| 512 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
| 513 | return (int) field_common->type->id; |
| 514 | } |
| 515 | |
| 516 | struct bt_ctf_field *bt_ctf_field_sequence_get_length( |
| 517 | struct bt_ctf_field *field) |
| 518 | { |
| 519 | return (void *) bt_field_common_sequence_get_length((void *) field); |
| 520 | } |
| 521 | |
| 522 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
| 523 | struct bt_ctf_field *length_field) |
| 524 | { |
| 525 | return bt_field_common_sequence_set_length((void *) field, |
| 526 | (void *) length_field); |
| 527 | } |
| 528 | |
| 529 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( |
| 530 | struct bt_ctf_field *field, uint64_t index) |
| 531 | { |
| 532 | return (void *) bt_field_common_structure_get_field_by_index( |
| 533 | (void *) field, index); |
| 534 | } |
| 535 | |
| 536 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name( |
| 537 | struct bt_ctf_field *field, const char *name) |
| 538 | { |
| 539 | return (void *) bt_field_common_structure_get_field_by_name( |
| 540 | (void *) field, name); |
| 541 | } |
| 542 | |
| 543 | int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field, |
| 544 | const char *name, struct bt_ctf_field *value) |
| 545 | { |
| 546 | return bt_field_common_structure_set_field_by_name((void *) field, |
| 547 | name, (void *) value); |
| 548 | } |
| 549 | |
| 550 | struct bt_ctf_field *bt_ctf_field_array_get_field( |
| 551 | struct bt_ctf_field *field, uint64_t index) |
| 552 | { |
| 553 | return (void *) bt_field_common_array_get_field((void *) field, index, |
| 554 | (bt_field_common_create_func) bt_ctf_field_create); |
| 555 | } |
| 556 | |
| 557 | struct bt_ctf_field *bt_ctf_field_sequence_get_field( |
| 558 | struct bt_ctf_field *field, uint64_t index) |
| 559 | { |
| 560 | return (void *) bt_field_common_sequence_get_field((void *) field, |
| 561 | index, (bt_field_common_create_func) bt_ctf_field_create); |
| 562 | } |
| 563 | |
| 564 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, |
| 565 | struct bt_ctf_field *tag_field) |
| 566 | { |
| 567 | return (void *) bt_field_common_variant_get_field((void *) field, |
| 568 | (void *) tag_field, |
| 569 | (bt_field_common_create_func) bt_ctf_field_create); |
| 570 | } |
| 571 | |
| 572 | struct bt_ctf_field *bt_ctf_field_variant_get_current_field( |
| 573 | struct bt_ctf_field *variant_field) |
| 574 | { |
| 575 | return (void *) bt_field_common_variant_get_current_field( |
| 576 | (void *) variant_field); |
| 577 | } |
| 578 | |
| 579 | struct bt_ctf_field *bt_ctf_field_variant_get_tag( |
| 580 | struct bt_ctf_field *variant_field) |
| 581 | { |
| 582 | return (void *) bt_field_common_variant_get_tag((void *) variant_field); |
| 583 | } |
| 584 | |
| 585 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container(struct bt_ctf_field *field) |
| 586 | { |
| 587 | return (void *) bt_field_common_enumeration_get_container( |
| 588 | (void *) field, (bt_field_common_create_func) bt_ctf_field_create); |
| 589 | } |
| 590 | |
| 591 | int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field, int64_t *value) |
| 592 | { |
| 593 | return bt_field_common_integer_signed_get_value((void *) field, value); |
| 594 | } |
| 595 | |
| 596 | int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field, |
| 597 | int64_t value) |
| 598 | { |
| 599 | return bt_field_common_integer_signed_set_value((void *) field, value); |
| 600 | } |
| 601 | |
| 602 | int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field, |
| 603 | uint64_t *value) |
| 604 | { |
| 605 | return bt_field_common_integer_unsigned_get_value((void *) field, |
| 606 | value); |
| 607 | } |
| 608 | |
| 609 | int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field, uint64_t value) |
| 610 | { |
| 611 | return bt_field_common_integer_unsigned_set_value((void *) field, value); |
| 612 | } |
| 613 | |
| 614 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, |
| 615 | double *value) |
| 616 | { |
| 617 | return bt_field_common_floating_point_get_value((void *) field, value); |
| 618 | } |
| 619 | |
| 620 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, |
| 621 | double value) |
| 622 | { |
| 623 | return bt_field_common_floating_point_set_value((void *) field, value); |
| 624 | } |
| 625 | |
| 626 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) |
| 627 | { |
| 628 | return bt_field_common_string_get_value((void *) field); |
| 629 | } |
| 630 | |
| 631 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value) |
| 632 | { |
| 633 | return bt_field_common_string_set_value((void *) field, value); |
| 634 | } |
| 635 | |
| 636 | int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value) |
| 637 | { |
| 638 | return bt_field_common_string_append((void *) field, value); |
| 639 | } |
| 640 | |
| 641 | int bt_ctf_field_string_append_len(struct bt_ctf_field *field, |
| 642 | const char *value, unsigned int length) |
| 643 | { |
| 644 | return bt_field_common_string_append_len((void *) field, value, length); |
| 645 | } |
| 646 | |
| 647 | struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) |
| 648 | { |
| 649 | return (void *) bt_field_common_copy((void *) field); |
| 650 | } |
| 651 | |
| 652 | static |
| 653 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) |
| 654 | { |
| 655 | struct bt_field_common_integer *integer = |
| 656 | g_new0(struct bt_field_common_integer, 1); |
| 657 | |
| 658 | BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type); |
| 659 | |
| 660 | if (integer) { |
| 661 | bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type, |
| 662 | bt_field_common_integer_destroy, |
| 663 | &bt_ctf_field_integer_methods); |
| 664 | integer->common.spec.writer.serialize_func = |
| 665 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_integer_serialize; |
| 666 | BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p", |
| 667 | integer, type); |
| 668 | } else { |
| 669 | BT_LOGE_STR("Failed to allocate one integer field."); |
| 670 | } |
| 671 | |
| 672 | return (void *) integer; |
| 673 | } |
| 674 | |
| 675 | static |
| 676 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 677 | struct bt_ctf_field_type *type) |
| 678 | { |
| 679 | struct bt_field_common_enumeration *enumeration = g_new0( |
| 680 | struct bt_field_common_enumeration, 1); |
| 681 | |
| 682 | BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type); |
| 683 | |
| 684 | if (enumeration) { |
| 685 | bt_field_common_initialize(BT_TO_COMMON(enumeration), |
| 686 | (void *) type, |
| 687 | bt_field_common_enumeration_destroy_recursive, |
| 688 | &bt_ctf_field_enumeration_methods); |
| 689 | enumeration->common.spec.writer.serialize_func = |
| 690 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_enumeration_serialize_recursive; |
| 691 | BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p", |
| 692 | enumeration, type); |
| 693 | } else { |
| 694 | BT_LOGE_STR("Failed to allocate one enumeration field."); |
| 695 | } |
| 696 | |
| 697 | return (void *) enumeration; |
| 698 | } |
| 699 | |
| 700 | static |
| 701 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 702 | struct bt_ctf_field_type *type) |
| 703 | { |
| 704 | struct bt_field_common_floating_point *floating_point; |
| 705 | |
| 706 | BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type); |
| 707 | floating_point = g_new0(struct bt_field_common_floating_point, 1); |
| 708 | |
| 709 | if (floating_point) { |
| 710 | bt_field_common_initialize(BT_TO_COMMON(floating_point), |
| 711 | (void *) type, |
| 712 | bt_field_common_floating_point_destroy, |
| 713 | &bt_ctf_field_floating_point_methods); |
| 714 | floating_point->common.spec.writer.serialize_func = |
| 715 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_floating_point_serialize; |
| 716 | BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p", |
| 717 | floating_point, type); |
| 718 | } else { |
| 719 | BT_LOGE_STR("Failed to allocate one floating point number field."); |
| 720 | } |
| 721 | |
| 722 | return (void *) floating_point; |
| 723 | } |
| 724 | |
| 725 | static |
| 726 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 727 | struct bt_ctf_field_type *type) |
| 728 | { |
| 729 | struct bt_field_common_structure *structure = g_new0( |
| 730 | struct bt_field_common_structure, 1); |
| 731 | int iret; |
| 732 | |
| 733 | BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type); |
| 734 | |
| 735 | if (!structure) { |
| 736 | BT_LOGE_STR("Failed to allocate one structure field."); |
| 737 | goto end; |
| 738 | } |
| 739 | |
| 740 | iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure), |
| 741 | (void *) type, bt_field_common_structure_destroy_recursive, |
| 742 | &bt_ctf_field_structure_methods, |
| 743 | (bt_field_common_create_func) bt_ctf_field_create); |
| 744 | structure->common.spec.writer.serialize_func = |
| 745 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive; |
| 746 | if (iret) { |
| 747 | BT_PUT(structure); |
| 748 | goto end; |
| 749 | } |
| 750 | |
| 751 | BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p", |
| 752 | structure, type); |
| 753 | |
| 754 | end: |
| 755 | return (void *) structure; |
| 756 | } |
| 757 | |
| 758 | static |
| 759 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) |
| 760 | { |
| 761 | struct bt_field_common_variant *variant = g_new0( |
| 762 | struct bt_field_common_variant, 1); |
| 763 | |
| 764 | BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type); |
| 765 | |
| 766 | if (variant) { |
| 767 | bt_field_common_initialize(BT_TO_COMMON(variant), |
| 768 | (void *) type, |
| 769 | bt_field_common_variant_destroy_recursive, |
| 770 | &bt_ctf_field_variant_methods); |
| 771 | variant->common.spec.writer.serialize_func = |
| 772 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_variant_serialize_recursive; |
| 773 | BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p", |
| 774 | variant, type); |
| 775 | } else { |
| 776 | BT_LOGE_STR("Failed to allocate one variant field."); |
| 777 | } |
| 778 | |
| 779 | return (void *) variant; |
| 780 | } |
| 781 | |
| 782 | static |
| 783 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) |
| 784 | { |
| 785 | struct bt_field_common_array *array = |
| 786 | g_new0(struct bt_field_common_array, 1); |
| 787 | int ret; |
| 788 | |
| 789 | BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type); |
| 790 | BT_ASSERT(type); |
| 791 | |
| 792 | if (!array) { |
| 793 | BT_LOGE_STR("Failed to allocate one array field."); |
| 794 | goto end; |
| 795 | } |
| 796 | |
| 797 | ret = bt_field_common_array_initialize(BT_TO_COMMON(array), |
| 798 | (void *) type, |
| 799 | bt_field_common_array_destroy_recursive, |
| 800 | &bt_ctf_field_array_methods); |
| 801 | array->common.spec.writer.serialize_func = |
| 802 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive; |
| 803 | if (ret) { |
| 804 | BT_PUT(array); |
| 805 | goto end; |
| 806 | } |
| 807 | |
| 808 | BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p", |
| 809 | array, type); |
| 810 | |
| 811 | end: |
| 812 | return (void *) array; |
| 813 | } |
| 814 | |
| 815 | static |
| 816 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *type) |
| 817 | { |
| 818 | struct bt_field_common_sequence *sequence = g_new0( |
| 819 | struct bt_field_common_sequence, 1); |
| 820 | |
| 821 | BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type); |
| 822 | |
| 823 | if (sequence) { |
| 824 | bt_field_common_initialize(BT_TO_COMMON(sequence), |
| 825 | (void *) type, |
| 826 | bt_field_common_sequence_destroy_recursive, |
| 827 | &bt_ctf_field_sequence_methods); |
| 828 | sequence->common.spec.writer.serialize_func = |
| 829 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_sequence_serialize_recursive; |
| 830 | BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p", |
| 831 | sequence, type); |
| 832 | } else { |
| 833 | BT_LOGE_STR("Failed to allocate one sequence field."); |
| 834 | } |
| 835 | |
| 836 | return (void *) sequence; |
| 837 | } |
| 838 | |
| 839 | static |
| 840 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) |
| 841 | { |
| 842 | struct bt_field_common_string *string = g_new0( |
| 843 | struct bt_field_common_string, 1); |
| 844 | |
| 845 | BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type); |
| 846 | |
| 847 | if (string) { |
| 848 | bt_field_common_initialize(BT_TO_COMMON(string), |
| 849 | (void *) type, |
| 850 | bt_field_common_string_destroy, |
| 851 | &bt_ctf_field_string_methods); |
| 852 | string->common.spec.writer.serialize_func = |
| 853 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_string_serialize; |
| 854 | BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p", |
| 855 | string, type); |
| 856 | } else { |
| 857 | BT_LOGE_STR("Failed to allocate one string field."); |
| 858 | } |
| 859 | |
| 860 | return (void *) string; |
| 861 | } |