| 1 | /* |
| 2 | * event-fields.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Event Fields |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/ctf-writer/event-fields.h> |
| 30 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
| 31 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 32 | #include <babeltrace/compiler.h> |
| 33 | |
| 34 | #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) |
| 35 | |
| 36 | static |
| 37 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); |
| 38 | static |
| 39 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 40 | struct bt_ctf_field_type *); |
| 41 | static |
| 42 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 43 | struct bt_ctf_field_type *); |
| 44 | static |
| 45 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 46 | struct bt_ctf_field_type *); |
| 47 | static |
| 48 | struct bt_ctf_field *bt_ctf_field_variant_create( |
| 49 | struct bt_ctf_field_type *); |
| 50 | static |
| 51 | struct bt_ctf_field *bt_ctf_field_array_create( |
| 52 | struct bt_ctf_field_type *); |
| 53 | static |
| 54 | struct bt_ctf_field *bt_ctf_field_sequence_create( |
| 55 | struct bt_ctf_field_type *); |
| 56 | static |
| 57 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); |
| 58 | |
| 59 | static |
| 60 | void bt_ctf_field_destroy(struct bt_ctf_ref *); |
| 61 | static |
| 62 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *); |
| 63 | static |
| 64 | void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *); |
| 65 | static |
| 66 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *); |
| 67 | static |
| 68 | void bt_ctf_field_structure_destroy(struct bt_ctf_field *); |
| 69 | static |
| 70 | void bt_ctf_field_variant_destroy(struct bt_ctf_field *); |
| 71 | static |
| 72 | void bt_ctf_field_array_destroy(struct bt_ctf_field *); |
| 73 | static |
| 74 | void bt_ctf_field_sequence_destroy(struct bt_ctf_field *); |
| 75 | static |
| 76 | void bt_ctf_field_string_destroy(struct bt_ctf_field *); |
| 77 | |
| 78 | static |
| 79 | int bt_ctf_field_generic_validate(struct bt_ctf_field *); |
| 80 | static |
| 81 | int bt_ctf_field_structure_validate(struct bt_ctf_field *); |
| 82 | static |
| 83 | int bt_ctf_field_variant_validate(struct bt_ctf_field *); |
| 84 | static |
| 85 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *); |
| 86 | static |
| 87 | int bt_ctf_field_array_validate(struct bt_ctf_field *); |
| 88 | static |
| 89 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *); |
| 90 | |
| 91 | static |
| 92 | int bt_ctf_field_generic_reset(struct bt_ctf_field *); |
| 93 | static |
| 94 | int bt_ctf_field_structure_reset(struct bt_ctf_field *); |
| 95 | static |
| 96 | int bt_ctf_field_variant_reset(struct bt_ctf_field *); |
| 97 | static |
| 98 | int bt_ctf_field_enumeration_reset(struct bt_ctf_field *); |
| 99 | static |
| 100 | int bt_ctf_field_array_reset(struct bt_ctf_field *); |
| 101 | static |
| 102 | int bt_ctf_field_sequence_reset(struct bt_ctf_field *); |
| 103 | static |
| 104 | int bt_ctf_field_string_reset(struct bt_ctf_field *); |
| 105 | |
| 106 | static |
| 107 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *, |
| 108 | struct ctf_stream_pos *); |
| 109 | static |
| 110 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *, |
| 111 | struct ctf_stream_pos *); |
| 112 | static |
| 113 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *, |
| 114 | struct ctf_stream_pos *); |
| 115 | static |
| 116 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *, |
| 117 | struct ctf_stream_pos *); |
| 118 | static |
| 119 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *, |
| 120 | struct ctf_stream_pos *); |
| 121 | static |
| 122 | int bt_ctf_field_array_serialize(struct bt_ctf_field *, |
| 123 | struct ctf_stream_pos *); |
| 124 | static |
| 125 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *, |
| 126 | struct ctf_stream_pos *); |
| 127 | static |
| 128 | int bt_ctf_field_string_serialize(struct bt_ctf_field *, |
| 129 | struct ctf_stream_pos *); |
| 130 | |
| 131 | static |
| 132 | int bt_ctf_field_integer_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 133 | static |
| 134 | int bt_ctf_field_enumeration_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 135 | static |
| 136 | int bt_ctf_field_floating_point_copy(struct bt_ctf_field *, |
| 137 | struct bt_ctf_field *); |
| 138 | static |
| 139 | int bt_ctf_field_structure_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 140 | static |
| 141 | int bt_ctf_field_variant_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 142 | static |
| 143 | int bt_ctf_field_array_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 144 | static |
| 145 | int bt_ctf_field_sequence_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 146 | static |
| 147 | int bt_ctf_field_string_copy(struct bt_ctf_field *, struct bt_ctf_field *); |
| 148 | |
| 149 | static |
| 150 | int increase_packet_size(struct ctf_stream_pos *pos); |
| 151 | |
| 152 | static |
| 153 | struct bt_ctf_field *(*field_create_funcs[])( |
| 154 | struct bt_ctf_field_type *) = { |
| 155 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create, |
| 156 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create, |
| 157 | [CTF_TYPE_FLOAT] = |
| 158 | bt_ctf_field_floating_point_create, |
| 159 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create, |
| 160 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create, |
| 161 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_create, |
| 162 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create, |
| 163 | [CTF_TYPE_STRING] = bt_ctf_field_string_create, |
| 164 | }; |
| 165 | |
| 166 | static |
| 167 | void (*field_destroy_funcs[])(struct bt_ctf_field *) = { |
| 168 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy, |
| 169 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy, |
| 170 | [CTF_TYPE_FLOAT] = |
| 171 | bt_ctf_field_floating_point_destroy, |
| 172 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy, |
| 173 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy, |
| 174 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy, |
| 175 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy, |
| 176 | [CTF_TYPE_STRING] = bt_ctf_field_string_destroy, |
| 177 | }; |
| 178 | |
| 179 | static |
| 180 | int (*field_validate_funcs[])(struct bt_ctf_field *) = { |
| 181 | [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate, |
| 182 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate, |
| 183 | [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate, |
| 184 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate, |
| 185 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate, |
| 186 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate, |
| 187 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate, |
| 188 | [CTF_TYPE_STRING] = bt_ctf_field_generic_validate, |
| 189 | }; |
| 190 | |
| 191 | static |
| 192 | int (*field_reset_funcs[])(struct bt_ctf_field *) = { |
| 193 | [CTF_TYPE_INTEGER] = bt_ctf_field_generic_reset, |
| 194 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_reset, |
| 195 | [CTF_TYPE_FLOAT] = bt_ctf_field_generic_reset, |
| 196 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_reset, |
| 197 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_reset, |
| 198 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_reset, |
| 199 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_reset, |
| 200 | [CTF_TYPE_STRING] = bt_ctf_field_string_reset, |
| 201 | }; |
| 202 | |
| 203 | static |
| 204 | int (*field_serialize_funcs[])(struct bt_ctf_field *, |
| 205 | struct ctf_stream_pos *) = { |
| 206 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize, |
| 207 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize, |
| 208 | [CTF_TYPE_FLOAT] = |
| 209 | bt_ctf_field_floating_point_serialize, |
| 210 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize, |
| 211 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize, |
| 212 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize, |
| 213 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize, |
| 214 | [CTF_TYPE_STRING] = bt_ctf_field_string_serialize, |
| 215 | }; |
| 216 | |
| 217 | static |
| 218 | int (*field_copy_funcs[])(struct bt_ctf_field *, struct bt_ctf_field *) = { |
| 219 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_copy, |
| 220 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_copy, |
| 221 | [CTF_TYPE_FLOAT] = bt_ctf_field_floating_point_copy, |
| 222 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_copy, |
| 223 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_copy, |
| 224 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_copy, |
| 225 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_copy, |
| 226 | [CTF_TYPE_STRING] = bt_ctf_field_string_copy, |
| 227 | }; |
| 228 | |
| 229 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) |
| 230 | { |
| 231 | struct bt_ctf_field *field = NULL; |
| 232 | enum ctf_type_id type_id; |
| 233 | |
| 234 | if (!type) { |
| 235 | goto error; |
| 236 | } |
| 237 | |
| 238 | type_id = bt_ctf_field_type_get_type_id(type); |
| 239 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES || |
| 240 | bt_ctf_field_type_validate(type)) { |
| 241 | goto error; |
| 242 | } |
| 243 | |
| 244 | field = field_create_funcs[type_id](type); |
| 245 | if (!field) { |
| 246 | goto error; |
| 247 | } |
| 248 | |
| 249 | /* The type's declaration can't change after this point */ |
| 250 | bt_ctf_field_type_freeze(type); |
| 251 | bt_ctf_field_type_get(type); |
| 252 | bt_ctf_ref_init(&field->ref_count); |
| 253 | field->type = type; |
| 254 | error: |
| 255 | return field; |
| 256 | } |
| 257 | |
| 258 | void bt_ctf_field_get(struct bt_ctf_field *field) |
| 259 | { |
| 260 | if (field) { |
| 261 | bt_ctf_ref_get(&field->ref_count); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void bt_ctf_field_put(struct bt_ctf_field *field) |
| 266 | { |
| 267 | if (field) { |
| 268 | bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) |
| 273 | { |
| 274 | struct bt_ctf_field_type *ret = NULL; |
| 275 | |
| 276 | if (!field) { |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | ret = field->type; |
| 281 | bt_ctf_field_type_get(ret); |
| 282 | end: |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | struct bt_ctf_field *bt_ctf_field_sequence_get_length( |
| 287 | struct bt_ctf_field *field) |
| 288 | { |
| 289 | struct bt_ctf_field *ret = NULL; |
| 290 | struct bt_ctf_field_sequence *sequence; |
| 291 | |
| 292 | if (!field) { |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | if (bt_ctf_field_type_get_type_id(field->type) != |
| 297 | CTF_TYPE_SEQUENCE) { |
| 298 | goto end; |
| 299 | } |
| 300 | |
| 301 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 302 | ret = sequence->length; |
| 303 | bt_ctf_field_get(ret); |
| 304 | end: |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
| 309 | struct bt_ctf_field *length_field) |
| 310 | { |
| 311 | int ret = 0; |
| 312 | struct bt_ctf_field_type_integer *length_type; |
| 313 | struct bt_ctf_field_integer *length; |
| 314 | struct bt_ctf_field_sequence *sequence; |
| 315 | uint64_t sequence_length; |
| 316 | |
| 317 | if (!field || !length_field) { |
| 318 | ret = -1; |
| 319 | goto end; |
| 320 | } |
| 321 | if (bt_ctf_field_type_get_type_id(length_field->type) != |
| 322 | CTF_TYPE_INTEGER) { |
| 323 | ret = -1; |
| 324 | goto end; |
| 325 | } |
| 326 | |
| 327 | length_type = container_of(length_field->type, |
| 328 | struct bt_ctf_field_type_integer, parent); |
| 329 | /* The length field must be unsigned */ |
| 330 | if (length_type->declaration.signedness) { |
| 331 | ret = -1; |
| 332 | goto end; |
| 333 | } |
| 334 | |
| 335 | length = container_of(length_field, struct bt_ctf_field_integer, |
| 336 | parent); |
| 337 | sequence_length = length->definition.value._unsigned; |
| 338 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 339 | if (sequence->elements) { |
| 340 | g_ptr_array_free(sequence->elements, TRUE); |
| 341 | bt_ctf_field_put(sequence->length); |
| 342 | } |
| 343 | |
| 344 | sequence->elements = g_ptr_array_sized_new((size_t)sequence_length); |
| 345 | if (!sequence->elements) { |
| 346 | ret = -1; |
| 347 | goto end; |
| 348 | } |
| 349 | |
| 350 | g_ptr_array_set_free_func(sequence->elements, |
| 351 | (GDestroyNotify)bt_ctf_field_put); |
| 352 | g_ptr_array_set_size(sequence->elements, (size_t)sequence_length); |
| 353 | bt_ctf_field_get(length_field); |
| 354 | sequence->length = length_field; |
| 355 | end: |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | struct bt_ctf_field *bt_ctf_field_structure_get_field( |
| 360 | struct bt_ctf_field *field, const char *name) |
| 361 | { |
| 362 | struct bt_ctf_field *new_field = NULL; |
| 363 | GQuark field_quark; |
| 364 | struct bt_ctf_field_structure *structure; |
| 365 | struct bt_ctf_field_type *field_type = NULL; |
| 366 | size_t index; |
| 367 | |
| 368 | if (!field || !name || |
| 369 | bt_ctf_field_type_get_type_id(field->type) != |
| 370 | CTF_TYPE_STRUCT) { |
| 371 | goto error; |
| 372 | } |
| 373 | |
| 374 | field_quark = g_quark_from_string(name); |
| 375 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 376 | field_type = |
| 377 | bt_ctf_field_type_structure_get_field_type_by_name(field->type, |
| 378 | name); |
| 379 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 380 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) { |
| 381 | goto error; |
| 382 | } |
| 383 | |
| 384 | if (structure->fields->pdata[index]) { |
| 385 | new_field = structure->fields->pdata[index]; |
| 386 | goto end; |
| 387 | } |
| 388 | |
| 389 | new_field = bt_ctf_field_create(field_type); |
| 390 | if (!new_field) { |
| 391 | goto error; |
| 392 | } |
| 393 | |
| 394 | structure->fields->pdata[index] = new_field; |
| 395 | end: |
| 396 | bt_ctf_field_get(new_field); |
| 397 | error: |
| 398 | if (field_type) { |
| 399 | bt_ctf_field_type_put(field_type); |
| 400 | } |
| 401 | return new_field; |
| 402 | } |
| 403 | |
| 404 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( |
| 405 | struct bt_ctf_field *field, int index) |
| 406 | { |
| 407 | int ret; |
| 408 | const char *field_name; |
| 409 | struct bt_ctf_field_structure *structure; |
| 410 | struct bt_ctf_field_type *structure_type; |
| 411 | struct bt_ctf_field_type *field_type = NULL; |
| 412 | struct bt_ctf_field *ret_field = NULL; |
| 413 | |
| 414 | if (!field || |
| 415 | bt_ctf_field_type_get_type_id(field->type) != CTF_TYPE_STRUCT) { |
| 416 | goto end; |
| 417 | } |
| 418 | |
| 419 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 420 | if (index >= structure->fields->len) { |
| 421 | goto error; |
| 422 | } |
| 423 | |
| 424 | ret_field = structure->fields->pdata[index]; |
| 425 | if (ret_field) { |
| 426 | goto end; |
| 427 | } |
| 428 | |
| 429 | /* Field has not been instanciated yet, create it */ |
| 430 | structure_type = bt_ctf_field_get_type(field); |
| 431 | if (!structure_type) { |
| 432 | goto error; |
| 433 | } |
| 434 | |
| 435 | ret = bt_ctf_field_type_structure_get_field(structure_type, |
| 436 | &field_name, &field_type, index); |
| 437 | bt_ctf_field_type_put(structure_type); |
| 438 | if (ret) { |
| 439 | goto error; |
| 440 | } |
| 441 | |
| 442 | ret_field = bt_ctf_field_create(field_type); |
| 443 | if (!ret_field) { |
| 444 | goto error; |
| 445 | } |
| 446 | |
| 447 | structure->fields->pdata[index] = ret_field; |
| 448 | end: |
| 449 | bt_ctf_field_get(ret_field); |
| 450 | error: |
| 451 | if (field_type) { |
| 452 | bt_ctf_field_type_put(field_type); |
| 453 | } |
| 454 | return ret_field; |
| 455 | } |
| 456 | |
| 457 | BT_HIDDEN |
| 458 | int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, |
| 459 | const char *name, struct bt_ctf_field *value) |
| 460 | { |
| 461 | int ret = 0; |
| 462 | GQuark field_quark; |
| 463 | struct bt_ctf_field_structure *structure; |
| 464 | struct bt_ctf_field_type *expected_field_type = NULL; |
| 465 | size_t index; |
| 466 | |
| 467 | if (!field || !name || !value || |
| 468 | bt_ctf_field_type_get_type_id(field->type) != |
| 469 | CTF_TYPE_STRUCT) { |
| 470 | ret = -1; |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | field_quark = g_quark_from_string(name); |
| 475 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 476 | expected_field_type = |
| 477 | bt_ctf_field_type_structure_get_field_type_by_name(field->type, |
| 478 | name); |
| 479 | if (expected_field_type != value->type) { |
| 480 | ret = -1; |
| 481 | goto end; |
| 482 | } |
| 483 | |
| 484 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 485 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) { |
| 486 | goto end; |
| 487 | } |
| 488 | |
| 489 | if (structure->fields->pdata[index]) { |
| 490 | bt_ctf_field_put(structure->fields->pdata[index]); |
| 491 | } |
| 492 | |
| 493 | structure->fields->pdata[index] = value; |
| 494 | bt_ctf_field_get(value); |
| 495 | end: |
| 496 | if (expected_field_type) { |
| 497 | bt_ctf_field_type_put(expected_field_type); |
| 498 | } |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field, |
| 503 | uint64_t index) |
| 504 | { |
| 505 | struct bt_ctf_field *new_field = NULL; |
| 506 | struct bt_ctf_field_type *field_type = NULL; |
| 507 | struct bt_ctf_field_array *array; |
| 508 | |
| 509 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 510 | CTF_TYPE_ARRAY) { |
| 511 | goto end; |
| 512 | } |
| 513 | |
| 514 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 515 | if (index >= array->elements->len) { |
| 516 | goto end; |
| 517 | } |
| 518 | |
| 519 | field_type = bt_ctf_field_type_array_get_element_type(field->type); |
| 520 | if (array->elements->pdata[(size_t)index]) { |
| 521 | new_field = array->elements->pdata[(size_t)index]; |
| 522 | goto end; |
| 523 | } |
| 524 | |
| 525 | new_field = bt_ctf_field_create(field_type); |
| 526 | bt_ctf_field_get(new_field); |
| 527 | array->elements->pdata[(size_t)index] = new_field; |
| 528 | end: |
| 529 | if (field_type) { |
| 530 | bt_ctf_field_type_put(field_type); |
| 531 | } |
| 532 | return new_field; |
| 533 | } |
| 534 | |
| 535 | struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field, |
| 536 | uint64_t index) |
| 537 | { |
| 538 | struct bt_ctf_field *new_field = NULL; |
| 539 | struct bt_ctf_field_type *field_type = NULL; |
| 540 | struct bt_ctf_field_sequence *sequence; |
| 541 | |
| 542 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 543 | CTF_TYPE_SEQUENCE) { |
| 544 | goto end; |
| 545 | } |
| 546 | |
| 547 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 548 | if (!sequence->elements || sequence->elements->len <= index) { |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | field_type = bt_ctf_field_type_sequence_get_element_type(field->type); |
| 553 | if (sequence->elements->pdata[(size_t)index]) { |
| 554 | new_field = sequence->elements->pdata[(size_t)index]; |
| 555 | goto end; |
| 556 | } |
| 557 | |
| 558 | new_field = bt_ctf_field_create(field_type); |
| 559 | bt_ctf_field_get(new_field); |
| 560 | sequence->elements->pdata[(size_t)index] = new_field; |
| 561 | end: |
| 562 | if (field_type) { |
| 563 | bt_ctf_field_type_put(field_type); |
| 564 | } |
| 565 | return new_field; |
| 566 | } |
| 567 | |
| 568 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, |
| 569 | struct bt_ctf_field *tag_field) |
| 570 | { |
| 571 | struct bt_ctf_field *new_field = NULL; |
| 572 | struct bt_ctf_field_variant *variant; |
| 573 | struct bt_ctf_field_type_variant *variant_type; |
| 574 | struct bt_ctf_field_type *field_type; |
| 575 | struct bt_ctf_field *tag_enum = NULL; |
| 576 | struct bt_ctf_field_integer *tag_enum_integer; |
| 577 | int64_t tag_enum_value; |
| 578 | |
| 579 | if (!field || !tag_field || |
| 580 | bt_ctf_field_type_get_type_id(field->type) != |
| 581 | CTF_TYPE_VARIANT || |
| 582 | bt_ctf_field_type_get_type_id(tag_field->type) != |
| 583 | CTF_TYPE_ENUM) { |
| 584 | goto end; |
| 585 | } |
| 586 | |
| 587 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 588 | variant_type = container_of(field->type, |
| 589 | struct bt_ctf_field_type_variant, parent); |
| 590 | tag_enum = bt_ctf_field_enumeration_get_container(tag_field); |
| 591 | if (!tag_enum) { |
| 592 | goto end; |
| 593 | } |
| 594 | |
| 595 | tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer, |
| 596 | parent); |
| 597 | |
| 598 | if (bt_ctf_field_validate(tag_field) < 0) { |
| 599 | goto end; |
| 600 | } |
| 601 | |
| 602 | tag_enum_value = tag_enum_integer->definition.value._signed; |
| 603 | field_type = bt_ctf_field_type_variant_get_field_type_signed( |
| 604 | variant_type, tag_enum_value); |
| 605 | if (!field_type) { |
| 606 | goto end; |
| 607 | } |
| 608 | |
| 609 | new_field = bt_ctf_field_create(field_type); |
| 610 | if (!new_field) { |
| 611 | goto end; |
| 612 | } |
| 613 | |
| 614 | bt_ctf_field_put(variant->tag); |
| 615 | bt_ctf_field_put(variant->payload); |
| 616 | bt_ctf_field_get(new_field); |
| 617 | bt_ctf_field_get(tag_field); |
| 618 | variant->tag = tag_field; |
| 619 | variant->payload = new_field; |
| 620 | end: |
| 621 | bt_ctf_field_put(tag_enum); |
| 622 | return new_field; |
| 623 | } |
| 624 | |
| 625 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( |
| 626 | struct bt_ctf_field *field) |
| 627 | { |
| 628 | struct bt_ctf_field *container = NULL; |
| 629 | struct bt_ctf_field_enumeration *enumeration; |
| 630 | |
| 631 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 632 | CTF_TYPE_ENUM) { |
| 633 | goto end; |
| 634 | } |
| 635 | |
| 636 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 637 | parent); |
| 638 | if (!enumeration->payload) { |
| 639 | struct bt_ctf_field_type_enumeration *enumeration_type = |
| 640 | container_of(field->type, |
| 641 | struct bt_ctf_field_type_enumeration, parent); |
| 642 | enumeration->payload = |
| 643 | bt_ctf_field_create(enumeration_type->container); |
| 644 | } |
| 645 | |
| 646 | container = enumeration->payload; |
| 647 | bt_ctf_field_get(container); |
| 648 | end: |
| 649 | return container; |
| 650 | } |
| 651 | |
| 652 | const char *bt_ctf_field_enumeration_get_mapping_name( |
| 653 | struct bt_ctf_field *field) |
| 654 | { |
| 655 | int ret; |
| 656 | const char *name = NULL; |
| 657 | struct bt_ctf_field *container = NULL; |
| 658 | struct bt_ctf_field_type *container_type = NULL; |
| 659 | struct bt_ctf_field_type_integer *integer_type = NULL; |
| 660 | struct bt_ctf_field_type_enumeration *enumeration_type = NULL; |
| 661 | |
| 662 | container = bt_ctf_field_enumeration_get_container(field); |
| 663 | if (!container) { |
| 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | container_type = bt_ctf_field_get_type(container); |
| 668 | if (!container_type) { |
| 669 | goto error_put_container; |
| 670 | } |
| 671 | |
| 672 | integer_type = container_of(container_type, |
| 673 | struct bt_ctf_field_type_integer, parent); |
| 674 | enumeration_type = container_of(field->type, |
| 675 | struct bt_ctf_field_type_enumeration, parent); |
| 676 | |
| 677 | if (!integer_type->declaration.signedness) { |
| 678 | uint64_t value; |
| 679 | ret = bt_ctf_field_unsigned_integer_get_value(container, |
| 680 | &value); |
| 681 | if (ret) { |
| 682 | goto error_put_container_type; |
| 683 | } |
| 684 | |
| 685 | name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned( |
| 686 | enumeration_type, value); |
| 687 | } else { |
| 688 | int64_t value; |
| 689 | ret = bt_ctf_field_signed_integer_get_value(container, |
| 690 | &value); |
| 691 | if (ret) { |
| 692 | goto error_put_container_type; |
| 693 | } |
| 694 | |
| 695 | name = bt_ctf_field_type_enumeration_get_mapping_name_signed( |
| 696 | enumeration_type, value); |
| 697 | } |
| 698 | |
| 699 | error_put_container_type: |
| 700 | bt_ctf_field_type_put(container_type); |
| 701 | error_put_container: |
| 702 | bt_ctf_field_put(container); |
| 703 | end: |
| 704 | return name; |
| 705 | } |
| 706 | |
| 707 | int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field, |
| 708 | int64_t *value) |
| 709 | { |
| 710 | int ret = 0; |
| 711 | struct bt_ctf_field_integer *integer; |
| 712 | struct bt_ctf_field_type_integer *integer_type; |
| 713 | |
| 714 | if (!field || !value || !field->payload_set || |
| 715 | bt_ctf_field_type_get_type_id(field->type) != |
| 716 | CTF_TYPE_INTEGER) { |
| 717 | ret = -1; |
| 718 | goto end; |
| 719 | } |
| 720 | |
| 721 | integer_type = container_of(field->type, |
| 722 | struct bt_ctf_field_type_integer, parent); |
| 723 | if (!integer_type->declaration.signedness) { |
| 724 | ret = -1; |
| 725 | goto end; |
| 726 | } |
| 727 | |
| 728 | integer = container_of(field, |
| 729 | struct bt_ctf_field_integer, parent); |
| 730 | *value = integer->definition.value._signed; |
| 731 | end: |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field, |
| 736 | int64_t value) |
| 737 | { |
| 738 | int ret = 0; |
| 739 | struct bt_ctf_field_integer *integer; |
| 740 | struct bt_ctf_field_type_integer *integer_type; |
| 741 | unsigned int size; |
| 742 | int64_t min_value, max_value; |
| 743 | |
| 744 | if (!field || |
| 745 | bt_ctf_field_type_get_type_id(field->type) != |
| 746 | CTF_TYPE_INTEGER) { |
| 747 | ret = -1; |
| 748 | goto end; |
| 749 | } |
| 750 | |
| 751 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 752 | integer_type = container_of(field->type, |
| 753 | struct bt_ctf_field_type_integer, parent); |
| 754 | if (!integer_type->declaration.signedness) { |
| 755 | ret = -1; |
| 756 | goto end; |
| 757 | } |
| 758 | |
| 759 | size = integer_type->declaration.len; |
| 760 | min_value = -((int64_t)1 << (size - 1)); |
| 761 | max_value = ((int64_t)1 << (size - 1)) - 1; |
| 762 | if (value < min_value || value > max_value) { |
| 763 | ret = -1; |
| 764 | goto end; |
| 765 | } |
| 766 | |
| 767 | integer->definition.value._signed = value; |
| 768 | integer->parent.payload_set = 1; |
| 769 | end: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field, |
| 774 | uint64_t *value) |
| 775 | { |
| 776 | int ret = 0; |
| 777 | struct bt_ctf_field_integer *integer; |
| 778 | struct bt_ctf_field_type_integer *integer_type; |
| 779 | |
| 780 | if (!field || !value || !field->payload_set || |
| 781 | bt_ctf_field_type_get_type_id(field->type) != |
| 782 | CTF_TYPE_INTEGER) { |
| 783 | ret = -1; |
| 784 | goto end; |
| 785 | } |
| 786 | |
| 787 | integer_type = container_of(field->type, |
| 788 | struct bt_ctf_field_type_integer, parent); |
| 789 | if (integer_type->declaration.signedness) { |
| 790 | ret = -1; |
| 791 | goto end; |
| 792 | } |
| 793 | |
| 794 | integer = container_of(field, |
| 795 | struct bt_ctf_field_integer, parent); |
| 796 | *value = integer->definition.value._unsigned; |
| 797 | end: |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field, |
| 802 | uint64_t value) |
| 803 | { |
| 804 | int ret = 0; |
| 805 | struct bt_ctf_field_integer *integer; |
| 806 | struct bt_ctf_field_type_integer *integer_type; |
| 807 | unsigned int size; |
| 808 | uint64_t max_value; |
| 809 | |
| 810 | if (!field || |
| 811 | bt_ctf_field_type_get_type_id(field->type) != |
| 812 | CTF_TYPE_INTEGER) { |
| 813 | ret = -1; |
| 814 | goto end; |
| 815 | } |
| 816 | |
| 817 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 818 | integer_type = container_of(field->type, |
| 819 | struct bt_ctf_field_type_integer, parent); |
| 820 | if (integer_type->declaration.signedness) { |
| 821 | ret = -1; |
| 822 | goto end; |
| 823 | } |
| 824 | |
| 825 | size = integer_type->declaration.len; |
| 826 | max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1; |
| 827 | if (value > max_value) { |
| 828 | ret = -1; |
| 829 | goto end; |
| 830 | } |
| 831 | |
| 832 | integer->definition.value._unsigned = value; |
| 833 | integer->parent.payload_set = 1; |
| 834 | end: |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, |
| 839 | double *value) |
| 840 | { |
| 841 | int ret = 0; |
| 842 | struct bt_ctf_field_floating_point *floating_point; |
| 843 | |
| 844 | if (!field || !value || !field->payload_set || |
| 845 | bt_ctf_field_type_get_type_id(field->type) != |
| 846 | CTF_TYPE_FLOAT) { |
| 847 | ret = -1; |
| 848 | goto end; |
| 849 | } |
| 850 | |
| 851 | floating_point = container_of(field, |
| 852 | struct bt_ctf_field_floating_point, parent); |
| 853 | *value = floating_point->definition.value; |
| 854 | end: |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, |
| 859 | double value) |
| 860 | { |
| 861 | int ret = 0; |
| 862 | struct bt_ctf_field_floating_point *floating_point; |
| 863 | |
| 864 | if (!field || |
| 865 | bt_ctf_field_type_get_type_id(field->type) != |
| 866 | CTF_TYPE_FLOAT) { |
| 867 | ret = -1; |
| 868 | goto end; |
| 869 | } |
| 870 | floating_point = container_of(field, struct bt_ctf_field_floating_point, |
| 871 | parent); |
| 872 | floating_point->definition.value = value; |
| 873 | floating_point->parent.payload_set = 1; |
| 874 | end: |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) |
| 879 | { |
| 880 | const char *ret = NULL; |
| 881 | struct bt_ctf_field_string *string; |
| 882 | |
| 883 | if (!field || !field->payload_set || |
| 884 | bt_ctf_field_type_get_type_id(field->type) != |
| 885 | CTF_TYPE_STRING) { |
| 886 | goto end; |
| 887 | } |
| 888 | |
| 889 | string = container_of(field, |
| 890 | struct bt_ctf_field_string, parent); |
| 891 | ret = string->payload->str; |
| 892 | end: |
| 893 | return ret; |
| 894 | } |
| 895 | |
| 896 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, |
| 897 | const char *value) |
| 898 | { |
| 899 | int ret = 0; |
| 900 | struct bt_ctf_field_string *string; |
| 901 | |
| 902 | if (!field || !value || |
| 903 | bt_ctf_field_type_get_type_id(field->type) != |
| 904 | CTF_TYPE_STRING) { |
| 905 | ret = -1; |
| 906 | goto end; |
| 907 | } |
| 908 | |
| 909 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 910 | if (string->payload) { |
| 911 | g_string_assign(string->payload, value); |
| 912 | } else { |
| 913 | string->payload = g_string_new(value); |
| 914 | } |
| 915 | |
| 916 | string->parent.payload_set = 1; |
| 917 | end: |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | BT_HIDDEN |
| 922 | int bt_ctf_field_validate(struct bt_ctf_field *field) |
| 923 | { |
| 924 | int ret = 0; |
| 925 | enum ctf_type_id type_id; |
| 926 | |
| 927 | if (!field) { |
| 928 | ret = -1; |
| 929 | goto end; |
| 930 | } |
| 931 | |
| 932 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 933 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 934 | ret = -1; |
| 935 | goto end; |
| 936 | } |
| 937 | |
| 938 | ret = field_validate_funcs[type_id](field); |
| 939 | end: |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | BT_HIDDEN |
| 944 | int bt_ctf_field_reset(struct bt_ctf_field *field) |
| 945 | { |
| 946 | int ret = 0; |
| 947 | enum ctf_type_id type_id; |
| 948 | |
| 949 | if (!field) { |
| 950 | ret = -1; |
| 951 | goto end; |
| 952 | } |
| 953 | |
| 954 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 955 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 956 | ret = -1; |
| 957 | goto end; |
| 958 | } |
| 959 | |
| 960 | ret = field_reset_funcs[type_id](field); |
| 961 | end: |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | BT_HIDDEN |
| 966 | int bt_ctf_field_serialize(struct bt_ctf_field *field, |
| 967 | struct ctf_stream_pos *pos) |
| 968 | { |
| 969 | int ret = 0; |
| 970 | enum ctf_type_id type_id; |
| 971 | |
| 972 | if (!field || !pos) { |
| 973 | ret = -1; |
| 974 | goto end; |
| 975 | } |
| 976 | |
| 977 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 978 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 979 | ret = -1; |
| 980 | goto end; |
| 981 | } |
| 982 | |
| 983 | ret = field_serialize_funcs[type_id](field, pos); |
| 984 | end: |
| 985 | return ret; |
| 986 | } |
| 987 | |
| 988 | BT_HIDDEN |
| 989 | struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) |
| 990 | { |
| 991 | int ret; |
| 992 | struct bt_ctf_field *copy = NULL; |
| 993 | enum ctf_type_id type_id; |
| 994 | |
| 995 | if (!field) { |
| 996 | goto end; |
| 997 | } |
| 998 | |
| 999 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 1000 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 1001 | goto end; |
| 1002 | } |
| 1003 | |
| 1004 | copy = bt_ctf_field_create(field->type); |
| 1005 | if (!copy) { |
| 1006 | goto end; |
| 1007 | } |
| 1008 | |
| 1009 | bt_ctf_field_type_get(field->type); |
| 1010 | copy->type = field->type; |
| 1011 | ret = field_copy_funcs[type_id](field, copy); |
| 1012 | if (ret) { |
| 1013 | bt_ctf_field_put(copy); |
| 1014 | copy = NULL; |
| 1015 | } |
| 1016 | end: |
| 1017 | return copy; |
| 1018 | } |
| 1019 | |
| 1020 | static |
| 1021 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) |
| 1022 | { |
| 1023 | struct bt_ctf_field_type_integer *integer_type = container_of(type, |
| 1024 | struct bt_ctf_field_type_integer, parent); |
| 1025 | struct bt_ctf_field_integer *integer = g_new0( |
| 1026 | struct bt_ctf_field_integer, 1); |
| 1027 | |
| 1028 | if (integer) { |
| 1029 | integer->definition.declaration = &integer_type->declaration; |
| 1030 | } |
| 1031 | |
| 1032 | return integer ? &integer->parent : NULL; |
| 1033 | } |
| 1034 | |
| 1035 | static |
| 1036 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 1037 | struct bt_ctf_field_type *type) |
| 1038 | { |
| 1039 | struct bt_ctf_field_enumeration *enumeration = g_new0( |
| 1040 | struct bt_ctf_field_enumeration, 1); |
| 1041 | |
| 1042 | return enumeration ? &enumeration->parent : NULL; |
| 1043 | } |
| 1044 | |
| 1045 | static |
| 1046 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 1047 | struct bt_ctf_field_type *type) |
| 1048 | { |
| 1049 | struct bt_ctf_field_floating_point *floating_point; |
| 1050 | struct bt_ctf_field_type_floating_point *floating_point_type; |
| 1051 | |
| 1052 | floating_point = g_new0(struct bt_ctf_field_floating_point, 1); |
| 1053 | if (!floating_point) { |
| 1054 | goto end; |
| 1055 | } |
| 1056 | |
| 1057 | floating_point_type = container_of(type, |
| 1058 | struct bt_ctf_field_type_floating_point, parent); |
| 1059 | floating_point->definition.declaration = container_of( |
| 1060 | type->declaration, struct declaration_float, p); |
| 1061 | |
| 1062 | |
| 1063 | floating_point->definition.sign = &floating_point->sign; |
| 1064 | floating_point->sign.declaration = &floating_point_type->sign; |
| 1065 | floating_point->definition.sign->p.declaration = |
| 1066 | &floating_point_type->sign.p; |
| 1067 | |
| 1068 | floating_point->definition.mantissa = &floating_point->mantissa; |
| 1069 | floating_point->mantissa.declaration = &floating_point_type->mantissa; |
| 1070 | floating_point->definition.mantissa->p.declaration = |
| 1071 | &floating_point_type->mantissa.p; |
| 1072 | |
| 1073 | floating_point->definition.exp = &floating_point->exp; |
| 1074 | floating_point->exp.declaration = &floating_point_type->exp; |
| 1075 | floating_point->definition.exp->p.declaration = |
| 1076 | &floating_point_type->exp.p; |
| 1077 | |
| 1078 | end: |
| 1079 | return floating_point ? &floating_point->parent : NULL; |
| 1080 | } |
| 1081 | |
| 1082 | static |
| 1083 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 1084 | struct bt_ctf_field_type *type) |
| 1085 | { |
| 1086 | struct bt_ctf_field_type_structure *structure_type = container_of(type, |
| 1087 | struct bt_ctf_field_type_structure, parent); |
| 1088 | struct bt_ctf_field_structure *structure = g_new0( |
| 1089 | struct bt_ctf_field_structure, 1); |
| 1090 | struct bt_ctf_field *field = NULL; |
| 1091 | |
| 1092 | if (!structure || !structure_type->fields->len) { |
| 1093 | goto end; |
| 1094 | } |
| 1095 | |
| 1096 | structure->field_name_to_index = structure_type->field_name_to_index; |
| 1097 | structure->fields = g_ptr_array_new_with_free_func( |
| 1098 | (GDestroyNotify)bt_ctf_field_put); |
| 1099 | g_ptr_array_set_size(structure->fields, |
| 1100 | g_hash_table_size(structure->field_name_to_index)); |
| 1101 | field = &structure->parent; |
| 1102 | end: |
| 1103 | return field; |
| 1104 | } |
| 1105 | |
| 1106 | static |
| 1107 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) |
| 1108 | { |
| 1109 | struct bt_ctf_field_variant *variant = g_new0( |
| 1110 | struct bt_ctf_field_variant, 1); |
| 1111 | return variant ? &variant->parent : NULL; |
| 1112 | } |
| 1113 | |
| 1114 | static |
| 1115 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) |
| 1116 | { |
| 1117 | struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1); |
| 1118 | struct bt_ctf_field_type_array *array_type; |
| 1119 | unsigned int array_length; |
| 1120 | |
| 1121 | if (!array || !type) { |
| 1122 | goto error; |
| 1123 | } |
| 1124 | |
| 1125 | array_type = container_of(type, struct bt_ctf_field_type_array, parent); |
| 1126 | array_length = array_type->length; |
| 1127 | array->elements = g_ptr_array_sized_new(array_length); |
| 1128 | if (!array->elements) { |
| 1129 | goto error; |
| 1130 | } |
| 1131 | |
| 1132 | g_ptr_array_set_free_func(array->elements, |
| 1133 | (GDestroyNotify)bt_ctf_field_put); |
| 1134 | g_ptr_array_set_size(array->elements, array_length); |
| 1135 | return &array->parent; |
| 1136 | error: |
| 1137 | g_free(array); |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | |
| 1141 | static |
| 1142 | struct bt_ctf_field *bt_ctf_field_sequence_create( |
| 1143 | struct bt_ctf_field_type *type) |
| 1144 | { |
| 1145 | struct bt_ctf_field_sequence *sequence = g_new0( |
| 1146 | struct bt_ctf_field_sequence, 1); |
| 1147 | return sequence ? &sequence->parent : NULL; |
| 1148 | } |
| 1149 | |
| 1150 | static |
| 1151 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) |
| 1152 | { |
| 1153 | struct bt_ctf_field_string *string = g_new0( |
| 1154 | struct bt_ctf_field_string, 1); |
| 1155 | return string ? &string->parent : NULL; |
| 1156 | } |
| 1157 | |
| 1158 | static |
| 1159 | void bt_ctf_field_destroy(struct bt_ctf_ref *ref) |
| 1160 | { |
| 1161 | struct bt_ctf_field *field; |
| 1162 | struct bt_ctf_field_type *type; |
| 1163 | enum ctf_type_id type_id; |
| 1164 | |
| 1165 | if (!ref) { |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | field = container_of(ref, struct bt_ctf_field, ref_count); |
| 1170 | type = field->type; |
| 1171 | type_id = bt_ctf_field_type_get_type_id(type); |
| 1172 | if (type_id <= CTF_TYPE_UNKNOWN || |
| 1173 | type_id >= NR_CTF_TYPES) { |
| 1174 | return; |
| 1175 | } |
| 1176 | |
| 1177 | field_destroy_funcs[type_id](field); |
| 1178 | if (type) { |
| 1179 | bt_ctf_field_type_put(type); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | static |
| 1184 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) |
| 1185 | { |
| 1186 | struct bt_ctf_field_integer *integer; |
| 1187 | |
| 1188 | if (!field) { |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 1193 | g_free(integer); |
| 1194 | } |
| 1195 | |
| 1196 | static |
| 1197 | void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field) |
| 1198 | { |
| 1199 | struct bt_ctf_field_enumeration *enumeration; |
| 1200 | |
| 1201 | if (!field) { |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 1206 | parent); |
| 1207 | bt_ctf_field_put(enumeration->payload); |
| 1208 | g_free(enumeration); |
| 1209 | } |
| 1210 | |
| 1211 | static |
| 1212 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) |
| 1213 | { |
| 1214 | struct bt_ctf_field_floating_point *floating_point; |
| 1215 | |
| 1216 | if (!field) { |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | floating_point = container_of(field, struct bt_ctf_field_floating_point, |
| 1221 | parent); |
| 1222 | g_free(floating_point); |
| 1223 | } |
| 1224 | |
| 1225 | static |
| 1226 | void bt_ctf_field_structure_destroy(struct bt_ctf_field *field) |
| 1227 | { |
| 1228 | struct bt_ctf_field_structure *structure; |
| 1229 | |
| 1230 | if (!field) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 1235 | g_ptr_array_free(structure->fields, TRUE); |
| 1236 | g_free(structure); |
| 1237 | } |
| 1238 | |
| 1239 | static |
| 1240 | void bt_ctf_field_variant_destroy(struct bt_ctf_field *field) |
| 1241 | { |
| 1242 | struct bt_ctf_field_variant *variant; |
| 1243 | |
| 1244 | if (!field) { |
| 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 1249 | bt_ctf_field_put(variant->tag); |
| 1250 | bt_ctf_field_put(variant->payload); |
| 1251 | g_free(variant); |
| 1252 | } |
| 1253 | |
| 1254 | static |
| 1255 | void bt_ctf_field_array_destroy(struct bt_ctf_field *field) |
| 1256 | { |
| 1257 | struct bt_ctf_field_array *array; |
| 1258 | |
| 1259 | if (!field) { |
| 1260 | return; |
| 1261 | } |
| 1262 | |
| 1263 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 1264 | g_ptr_array_free(array->elements, TRUE); |
| 1265 | g_free(array); |
| 1266 | } |
| 1267 | |
| 1268 | static |
| 1269 | void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field) |
| 1270 | { |
| 1271 | struct bt_ctf_field_sequence *sequence; |
| 1272 | |
| 1273 | if (!field) { |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 1278 | g_ptr_array_free(sequence->elements, TRUE); |
| 1279 | bt_ctf_field_put(sequence->length); |
| 1280 | g_free(sequence); |
| 1281 | } |
| 1282 | |
| 1283 | static |
| 1284 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) |
| 1285 | { |
| 1286 | struct bt_ctf_field_string *string; |
| 1287 | if (!field) { |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 1292 | g_string_free(string->payload, TRUE); |
| 1293 | g_free(string); |
| 1294 | } |
| 1295 | |
| 1296 | static |
| 1297 | int bt_ctf_field_generic_validate(struct bt_ctf_field *field) |
| 1298 | { |
| 1299 | return (field && field->payload_set) ? 0 : -1; |
| 1300 | } |
| 1301 | |
| 1302 | static |
| 1303 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) |
| 1304 | { |
| 1305 | int ret; |
| 1306 | struct bt_ctf_field_enumeration *enumeration; |
| 1307 | |
| 1308 | if (!field) { |
| 1309 | ret = -1; |
| 1310 | goto end; |
| 1311 | } |
| 1312 | |
| 1313 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 1314 | parent); |
| 1315 | if (!enumeration->payload) { |
| 1316 | ret = -1; |
| 1317 | goto end; |
| 1318 | } |
| 1319 | |
| 1320 | ret = bt_ctf_field_validate(enumeration->payload); |
| 1321 | end: |
| 1322 | return ret; |
| 1323 | } |
| 1324 | |
| 1325 | static |
| 1326 | int bt_ctf_field_structure_validate(struct bt_ctf_field *field) |
| 1327 | { |
| 1328 | size_t i; |
| 1329 | int ret = 0; |
| 1330 | struct bt_ctf_field_structure *structure; |
| 1331 | |
| 1332 | if (!field) { |
| 1333 | ret = -1; |
| 1334 | goto end; |
| 1335 | } |
| 1336 | |
| 1337 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 1338 | for (i = 0; i < structure->fields->len; i++) { |
| 1339 | ret = bt_ctf_field_validate(structure->fields->pdata[i]); |
| 1340 | if (ret) { |
| 1341 | goto end; |
| 1342 | } |
| 1343 | } |
| 1344 | end: |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
| 1348 | static |
| 1349 | int bt_ctf_field_variant_validate(struct bt_ctf_field *field) |
| 1350 | { |
| 1351 | int ret = 0; |
| 1352 | struct bt_ctf_field_variant *variant; |
| 1353 | |
| 1354 | if (!field) { |
| 1355 | ret = -1; |
| 1356 | goto end; |
| 1357 | } |
| 1358 | |
| 1359 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 1360 | ret = bt_ctf_field_validate(variant->payload); |
| 1361 | end: |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
| 1365 | static |
| 1366 | int bt_ctf_field_array_validate(struct bt_ctf_field *field) |
| 1367 | { |
| 1368 | size_t i; |
| 1369 | int ret = 0; |
| 1370 | struct bt_ctf_field_array *array; |
| 1371 | |
| 1372 | if (!field) { |
| 1373 | ret = -1; |
| 1374 | goto end; |
| 1375 | } |
| 1376 | |
| 1377 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 1378 | for (i = 0; i < array->elements->len; i++) { |
| 1379 | ret = bt_ctf_field_validate(array->elements->pdata[i]); |
| 1380 | if (ret) { |
| 1381 | goto end; |
| 1382 | } |
| 1383 | } |
| 1384 | end: |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | static |
| 1389 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) |
| 1390 | { |
| 1391 | size_t i; |
| 1392 | int ret = 0; |
| 1393 | struct bt_ctf_field_sequence *sequence; |
| 1394 | |
| 1395 | if (!field) { |
| 1396 | ret = -1; |
| 1397 | goto end; |
| 1398 | } |
| 1399 | |
| 1400 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 1401 | for (i = 0; i < sequence->elements->len; i++) { |
| 1402 | ret = bt_ctf_field_validate(sequence->elements->pdata[i]); |
| 1403 | if (ret) { |
| 1404 | goto end; |
| 1405 | } |
| 1406 | } |
| 1407 | end: |
| 1408 | return ret; |
| 1409 | } |
| 1410 | |
| 1411 | static |
| 1412 | int bt_ctf_field_generic_reset(struct bt_ctf_field *field) |
| 1413 | { |
| 1414 | int ret = 0; |
| 1415 | |
| 1416 | if (!field) { |
| 1417 | ret = -1; |
| 1418 | goto end; |
| 1419 | } |
| 1420 | |
| 1421 | field->payload_set = 0; |
| 1422 | end: |
| 1423 | return ret; |
| 1424 | } |
| 1425 | |
| 1426 | static |
| 1427 | int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field) |
| 1428 | { |
| 1429 | int ret = 0; |
| 1430 | struct bt_ctf_field_enumeration *enumeration; |
| 1431 | |
| 1432 | if (!field) { |
| 1433 | ret = -1; |
| 1434 | goto end; |
| 1435 | } |
| 1436 | |
| 1437 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 1438 | parent); |
| 1439 | if (!enumeration->payload) { |
| 1440 | goto end; |
| 1441 | } |
| 1442 | |
| 1443 | ret = bt_ctf_field_reset(enumeration->payload); |
| 1444 | end: |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
| 1448 | static |
| 1449 | int bt_ctf_field_structure_reset(struct bt_ctf_field *field) |
| 1450 | { |
| 1451 | size_t i; |
| 1452 | int ret = 0; |
| 1453 | struct bt_ctf_field_structure *structure; |
| 1454 | |
| 1455 | if (!field) { |
| 1456 | ret = -1; |
| 1457 | goto end; |
| 1458 | } |
| 1459 | |
| 1460 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 1461 | for (i = 0; i < structure->fields->len; i++) { |
| 1462 | struct bt_ctf_field *member = structure->fields->pdata[i]; |
| 1463 | |
| 1464 | if (!member) { |
| 1465 | /* |
| 1466 | * Structure members are lazily initialized; skip if |
| 1467 | * this member has not been allocated yet. |
| 1468 | */ |
| 1469 | continue; |
| 1470 | } |
| 1471 | |
| 1472 | ret = bt_ctf_field_reset(member); |
| 1473 | if (ret) { |
| 1474 | goto end; |
| 1475 | } |
| 1476 | } |
| 1477 | end: |
| 1478 | return ret; |
| 1479 | } |
| 1480 | |
| 1481 | static |
| 1482 | int bt_ctf_field_variant_reset(struct bt_ctf_field *field) |
| 1483 | { |
| 1484 | int ret = 0; |
| 1485 | struct bt_ctf_field_variant *variant; |
| 1486 | |
| 1487 | if (!field) { |
| 1488 | ret = -1; |
| 1489 | goto end; |
| 1490 | } |
| 1491 | |
| 1492 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 1493 | if (variant->payload) { |
| 1494 | ret = bt_ctf_field_reset(variant->payload); |
| 1495 | } |
| 1496 | end: |
| 1497 | return ret; |
| 1498 | } |
| 1499 | |
| 1500 | static |
| 1501 | int bt_ctf_field_array_reset(struct bt_ctf_field *field) |
| 1502 | { |
| 1503 | size_t i; |
| 1504 | int ret = 0; |
| 1505 | struct bt_ctf_field_array *array; |
| 1506 | |
| 1507 | if (!field) { |
| 1508 | ret = -1; |
| 1509 | goto end; |
| 1510 | } |
| 1511 | |
| 1512 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 1513 | for (i = 0; i < array->elements->len; i++) { |
| 1514 | struct bt_ctf_field *member = array->elements->pdata[i]; |
| 1515 | |
| 1516 | if (!member) { |
| 1517 | /* |
| 1518 | * Array elements are lazily initialized; skip if |
| 1519 | * this member has not been allocated yet. |
| 1520 | */ |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
| 1524 | ret = bt_ctf_field_reset(member); |
| 1525 | if (ret) { |
| 1526 | goto end; |
| 1527 | } |
| 1528 | } |
| 1529 | end: |
| 1530 | return ret; |
| 1531 | } |
| 1532 | |
| 1533 | static |
| 1534 | int bt_ctf_field_sequence_reset(struct bt_ctf_field *field) |
| 1535 | { |
| 1536 | size_t i; |
| 1537 | int ret = 0; |
| 1538 | struct bt_ctf_field_sequence *sequence; |
| 1539 | |
| 1540 | if (!field) { |
| 1541 | ret = -1; |
| 1542 | goto end; |
| 1543 | } |
| 1544 | |
| 1545 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 1546 | for (i = 0; i < sequence->elements->len; i++) { |
| 1547 | struct bt_ctf_field *member = sequence->elements->pdata[i]; |
| 1548 | |
| 1549 | if (!member) { |
| 1550 | /* |
| 1551 | * Sequence elements are lazily initialized; skip if |
| 1552 | * this member has not been allocated yet. |
| 1553 | */ |
| 1554 | continue; |
| 1555 | } |
| 1556 | |
| 1557 | ret = bt_ctf_field_reset(member); |
| 1558 | if (ret) { |
| 1559 | goto end; |
| 1560 | } |
| 1561 | } |
| 1562 | end: |
| 1563 | return ret; |
| 1564 | } |
| 1565 | |
| 1566 | static |
| 1567 | int bt_ctf_field_string_reset(struct bt_ctf_field *field) |
| 1568 | { |
| 1569 | int ret = 0; |
| 1570 | struct bt_ctf_field_string *string; |
| 1571 | |
| 1572 | if (!field) { |
| 1573 | ret = -1; |
| 1574 | goto end; |
| 1575 | } |
| 1576 | |
| 1577 | ret = bt_ctf_field_generic_reset(field); |
| 1578 | if (ret) { |
| 1579 | goto end; |
| 1580 | } |
| 1581 | |
| 1582 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 1583 | if (string->payload) { |
| 1584 | g_string_truncate(string->payload, 0); |
| 1585 | } |
| 1586 | end: |
| 1587 | return ret; |
| 1588 | } |
| 1589 | |
| 1590 | static |
| 1591 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, |
| 1592 | struct ctf_stream_pos *pos) |
| 1593 | { |
| 1594 | int ret = 0; |
| 1595 | struct bt_ctf_field_integer *integer = container_of(field, |
| 1596 | struct bt_ctf_field_integer, parent); |
| 1597 | |
| 1598 | retry: |
| 1599 | ret = ctf_integer_write(&pos->parent, &integer->definition.p); |
| 1600 | if (ret == -EFAULT) { |
| 1601 | /* |
| 1602 | * The field is too large to fit in the current packet's |
| 1603 | * remaining space. Bump the packet size and retry. |
| 1604 | */ |
| 1605 | ret = increase_packet_size(pos); |
| 1606 | if (ret) { |
| 1607 | goto end; |
| 1608 | } |
| 1609 | goto retry; |
| 1610 | } |
| 1611 | end: |
| 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | static |
| 1616 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, |
| 1617 | struct ctf_stream_pos *pos) |
| 1618 | { |
| 1619 | struct bt_ctf_field_enumeration *enumeration = container_of( |
| 1620 | field, struct bt_ctf_field_enumeration, parent); |
| 1621 | |
| 1622 | return bt_ctf_field_serialize(enumeration->payload, pos); |
| 1623 | } |
| 1624 | |
| 1625 | static |
| 1626 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, |
| 1627 | struct ctf_stream_pos *pos) |
| 1628 | { |
| 1629 | int ret = 0; |
| 1630 | struct bt_ctf_field_floating_point *floating_point = container_of(field, |
| 1631 | struct bt_ctf_field_floating_point, parent); |
| 1632 | |
| 1633 | retry: |
| 1634 | ret = ctf_float_write(&pos->parent, &floating_point->definition.p); |
| 1635 | if (ret == -EFAULT) { |
| 1636 | /* |
| 1637 | * The field is too large to fit in the current packet's |
| 1638 | * remaining space. Bump the packet size and retry. |
| 1639 | */ |
| 1640 | ret = increase_packet_size(pos); |
| 1641 | if (ret) { |
| 1642 | goto end; |
| 1643 | } |
| 1644 | goto retry; |
| 1645 | } |
| 1646 | end: |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | static |
| 1651 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, |
| 1652 | struct ctf_stream_pos *pos) |
| 1653 | { |
| 1654 | size_t i; |
| 1655 | int ret = 0; |
| 1656 | struct bt_ctf_field_structure *structure = container_of( |
| 1657 | field, struct bt_ctf_field_structure, parent); |
| 1658 | |
| 1659 | while (!ctf_pos_access_ok(pos, |
| 1660 | offset_align(pos->offset, |
| 1661 | field->type->declaration->alignment))) { |
| 1662 | ret = increase_packet_size(pos); |
| 1663 | if (ret) { |
| 1664 | goto end; |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | if (!ctf_align_pos(pos, field->type->declaration->alignment)) { |
| 1669 | ret = -1; |
| 1670 | goto end; |
| 1671 | } |
| 1672 | |
| 1673 | for (i = 0; i < structure->fields->len; i++) { |
| 1674 | struct bt_ctf_field *field = g_ptr_array_index( |
| 1675 | structure->fields, i); |
| 1676 | |
| 1677 | ret = bt_ctf_field_serialize(field, pos); |
| 1678 | if (ret) { |
| 1679 | break; |
| 1680 | } |
| 1681 | } |
| 1682 | end: |
| 1683 | return ret; |
| 1684 | } |
| 1685 | |
| 1686 | static |
| 1687 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, |
| 1688 | struct ctf_stream_pos *pos) |
| 1689 | { |
| 1690 | struct bt_ctf_field_variant *variant = container_of( |
| 1691 | field, struct bt_ctf_field_variant, parent); |
| 1692 | |
| 1693 | return bt_ctf_field_serialize(variant->payload, pos); |
| 1694 | } |
| 1695 | |
| 1696 | static |
| 1697 | int bt_ctf_field_array_serialize(struct bt_ctf_field *field, |
| 1698 | struct ctf_stream_pos *pos) |
| 1699 | { |
| 1700 | size_t i; |
| 1701 | int ret = 0; |
| 1702 | struct bt_ctf_field_array *array = container_of( |
| 1703 | field, struct bt_ctf_field_array, parent); |
| 1704 | |
| 1705 | for (i = 0; i < array->elements->len; i++) { |
| 1706 | ret = bt_ctf_field_serialize( |
| 1707 | g_ptr_array_index(array->elements, i), pos); |
| 1708 | if (ret) { |
| 1709 | goto end; |
| 1710 | } |
| 1711 | } |
| 1712 | end: |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | static |
| 1717 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, |
| 1718 | struct ctf_stream_pos *pos) |
| 1719 | { |
| 1720 | size_t i; |
| 1721 | int ret = 0; |
| 1722 | struct bt_ctf_field_sequence *sequence = container_of( |
| 1723 | field, struct bt_ctf_field_sequence, parent); |
| 1724 | |
| 1725 | for (i = 0; i < sequence->elements->len; i++) { |
| 1726 | ret = bt_ctf_field_serialize( |
| 1727 | g_ptr_array_index(sequence->elements, i), pos); |
| 1728 | if (ret) { |
| 1729 | goto end; |
| 1730 | } |
| 1731 | } |
| 1732 | end: |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
| 1736 | static |
| 1737 | int bt_ctf_field_string_serialize(struct bt_ctf_field *field, |
| 1738 | struct ctf_stream_pos *pos) |
| 1739 | { |
| 1740 | size_t i; |
| 1741 | int ret = 0; |
| 1742 | struct bt_ctf_field_string *string = container_of(field, |
| 1743 | struct bt_ctf_field_string, parent); |
| 1744 | struct bt_ctf_field_type *character_type = |
| 1745 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 1746 | struct bt_ctf_field *character = bt_ctf_field_create(character_type); |
| 1747 | |
| 1748 | for (i = 0; i < string->payload->len + 1; i++) { |
| 1749 | ret = bt_ctf_field_unsigned_integer_set_value(character, |
| 1750 | (uint64_t) string->payload->str[i]); |
| 1751 | if (ret) { |
| 1752 | goto end; |
| 1753 | } |
| 1754 | |
| 1755 | ret = bt_ctf_field_integer_serialize(character, pos); |
| 1756 | if (ret) { |
| 1757 | goto end; |
| 1758 | } |
| 1759 | } |
| 1760 | end: |
| 1761 | bt_ctf_field_put(character); |
| 1762 | bt_ctf_field_type_put(character_type); |
| 1763 | return ret; |
| 1764 | } |
| 1765 | |
| 1766 | static |
| 1767 | int bt_ctf_field_integer_copy(struct bt_ctf_field *src, |
| 1768 | struct bt_ctf_field *dst) |
| 1769 | { |
| 1770 | struct bt_ctf_field_integer *integer_src, *integer_dst; |
| 1771 | |
| 1772 | integer_src = container_of(src, struct bt_ctf_field_integer, parent); |
| 1773 | integer_dst = container_of(src, struct bt_ctf_field_integer, parent); |
| 1774 | |
| 1775 | memcpy(&integer_dst->definition, &integer_src->definition, |
| 1776 | sizeof(struct definition_integer)); |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | static |
| 1781 | int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src, |
| 1782 | struct bt_ctf_field *dst) |
| 1783 | { |
| 1784 | int ret = 0; |
| 1785 | struct bt_ctf_field_enumeration *enum_src, *enum_dst; |
| 1786 | |
| 1787 | enum_src = container_of(src, struct bt_ctf_field_enumeration, parent); |
| 1788 | enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent); |
| 1789 | |
| 1790 | if (enum_src->payload) { |
| 1791 | enum_dst->payload = bt_ctf_field_copy(enum_src->payload); |
| 1792 | if (!enum_dst->payload) { |
| 1793 | ret = -1; |
| 1794 | goto end; |
| 1795 | } |
| 1796 | } |
| 1797 | end: |
| 1798 | return ret; |
| 1799 | } |
| 1800 | |
| 1801 | static |
| 1802 | int bt_ctf_field_floating_point_copy( |
| 1803 | struct bt_ctf_field *src, struct bt_ctf_field *dst) |
| 1804 | { |
| 1805 | struct bt_ctf_field_floating_point *float_src, *float_dst; |
| 1806 | |
| 1807 | float_src = container_of(src, struct bt_ctf_field_floating_point, |
| 1808 | parent); |
| 1809 | float_dst = container_of(dst, struct bt_ctf_field_floating_point, |
| 1810 | parent); |
| 1811 | |
| 1812 | memcpy(&float_dst->definition, &float_src->definition, |
| 1813 | sizeof(struct definition_float)); |
| 1814 | memcpy(&float_dst->sign, &float_src->sign, |
| 1815 | sizeof(struct definition_integer)); |
| 1816 | memcpy(&float_dst->mantissa, &float_src->mantissa, |
| 1817 | sizeof(struct definition_integer)); |
| 1818 | memcpy(&float_dst->exp, &float_src->exp, |
| 1819 | sizeof(struct definition_integer)); |
| 1820 | return 0; |
| 1821 | } |
| 1822 | |
| 1823 | static |
| 1824 | int bt_ctf_field_structure_copy(struct bt_ctf_field *src, |
| 1825 | struct bt_ctf_field *dst) |
| 1826 | { |
| 1827 | int ret, i; |
| 1828 | struct bt_ctf_field_structure *struct_src, *struct_dst; |
| 1829 | |
| 1830 | struct_src = container_of(src, struct bt_ctf_field_structure, parent); |
| 1831 | struct_dst = container_of(dst, struct bt_ctf_field_structure, parent); |
| 1832 | |
| 1833 | struct_dst->field_name_to_index = struct_src->field_name_to_index; |
| 1834 | struct_dst->fields = g_ptr_array_sized_new(struct_src->fields->len); |
| 1835 | if (!struct_dst->fields) { |
| 1836 | ret = -1; |
| 1837 | goto end; |
| 1838 | } |
| 1839 | |
| 1840 | for (i = 0; i < struct_src->fields->len; i++) { |
| 1841 | struct bt_ctf_field *field_copy = bt_ctf_field_copy( |
| 1842 | g_ptr_array_index(struct_src->fields, i)); |
| 1843 | |
| 1844 | if (!field_copy) { |
| 1845 | ret = -1; |
| 1846 | goto end; |
| 1847 | } |
| 1848 | g_ptr_array_add(struct_dst->fields, field_copy); |
| 1849 | } |
| 1850 | end: |
| 1851 | return ret; |
| 1852 | } |
| 1853 | |
| 1854 | static |
| 1855 | int bt_ctf_field_variant_copy(struct bt_ctf_field *src, |
| 1856 | struct bt_ctf_field *dst) |
| 1857 | { |
| 1858 | int ret = 0; |
| 1859 | struct bt_ctf_field_variant *variant_src, *variant_dst; |
| 1860 | |
| 1861 | variant_src = container_of(src, struct bt_ctf_field_variant, parent); |
| 1862 | variant_dst = container_of(dst, struct bt_ctf_field_variant, parent); |
| 1863 | |
| 1864 | if (variant_src->tag) { |
| 1865 | variant_dst->tag = bt_ctf_field_copy(variant_src->tag); |
| 1866 | if (!variant_dst->tag) { |
| 1867 | ret = -1; |
| 1868 | goto end; |
| 1869 | } |
| 1870 | } |
| 1871 | if (variant_src->payload) { |
| 1872 | variant_dst->payload = bt_ctf_field_copy(variant_src->payload); |
| 1873 | if (!variant_dst->payload) { |
| 1874 | ret = -1; |
| 1875 | goto end; |
| 1876 | } |
| 1877 | } |
| 1878 | end: |
| 1879 | return ret; |
| 1880 | } |
| 1881 | |
| 1882 | static |
| 1883 | int bt_ctf_field_array_copy(struct bt_ctf_field *src, |
| 1884 | struct bt_ctf_field *dst) |
| 1885 | { |
| 1886 | int ret = 0, i; |
| 1887 | struct bt_ctf_field_array *array_src, *array_dst; |
| 1888 | |
| 1889 | array_src = container_of(src, struct bt_ctf_field_array, parent); |
| 1890 | array_dst = container_of(dst, struct bt_ctf_field_array, parent); |
| 1891 | |
| 1892 | array_dst->elements = g_ptr_array_sized_new(array_src->elements->len); |
| 1893 | for (i = 0; i < array_src->elements->len; i++) { |
| 1894 | struct bt_ctf_field *field_copy = bt_ctf_field_copy( |
| 1895 | g_ptr_array_index(array_src->elements, i)); |
| 1896 | |
| 1897 | if (!field_copy) { |
| 1898 | ret = -1; |
| 1899 | goto end; |
| 1900 | } |
| 1901 | g_ptr_array_add(array_dst->elements, field_copy); |
| 1902 | } |
| 1903 | end: |
| 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | static |
| 1908 | int bt_ctf_field_sequence_copy(struct bt_ctf_field *src, |
| 1909 | struct bt_ctf_field *dst) |
| 1910 | { |
| 1911 | int ret = 0, i; |
| 1912 | struct bt_ctf_field_sequence *sequence_src, *sequence_dst; |
| 1913 | |
| 1914 | sequence_src = container_of(src, struct bt_ctf_field_sequence, parent); |
| 1915 | sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent); |
| 1916 | |
| 1917 | sequence_dst->elements = g_ptr_array_sized_new( |
| 1918 | sequence_src->elements->len); |
| 1919 | for (i = 0; i < sequence_src->elements->len; i++) { |
| 1920 | struct bt_ctf_field *field_copy = bt_ctf_field_copy( |
| 1921 | g_ptr_array_index(sequence_src->elements, i)); |
| 1922 | |
| 1923 | if (!field_copy) { |
| 1924 | ret = -1; |
| 1925 | goto end; |
| 1926 | } |
| 1927 | g_ptr_array_add(sequence_dst->elements, field_copy); |
| 1928 | } |
| 1929 | end: |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
| 1933 | static |
| 1934 | int bt_ctf_field_string_copy(struct bt_ctf_field *src, |
| 1935 | struct bt_ctf_field *dst) |
| 1936 | { |
| 1937 | int ret = 0; |
| 1938 | struct bt_ctf_field_string *string_src, *string_dst; |
| 1939 | |
| 1940 | string_src = container_of(src, struct bt_ctf_field_string, parent); |
| 1941 | string_dst = container_of(dst, struct bt_ctf_field_string, parent); |
| 1942 | |
| 1943 | if (string_src->payload) { |
| 1944 | string_dst->payload = g_string_new(string_src->payload->str); |
| 1945 | if (!string_dst->payload) { |
| 1946 | ret = -1; |
| 1947 | goto end; |
| 1948 | } |
| 1949 | } |
| 1950 | end: |
| 1951 | return ret; |
| 1952 | } |
| 1953 | |
| 1954 | static |
| 1955 | int increase_packet_size(struct ctf_stream_pos *pos) |
| 1956 | { |
| 1957 | int ret; |
| 1958 | |
| 1959 | assert(pos); |
| 1960 | ret = munmap_align(pos->base_mma); |
| 1961 | if (ret) { |
| 1962 | goto end; |
| 1963 | } |
| 1964 | |
| 1965 | pos->packet_size += PACKET_LEN_INCREMENT; |
| 1966 | ret = posix_fallocate(pos->fd, pos->mmap_offset, |
| 1967 | pos->packet_size / CHAR_BIT); |
| 1968 | if (ret) { |
| 1969 | goto end; |
| 1970 | } |
| 1971 | |
| 1972 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
| 1973 | pos->flags, pos->fd, pos->mmap_offset); |
| 1974 | if (pos->base_mma == MAP_FAILED) { |
| 1975 | ret = -1; |
| 1976 | } |
| 1977 | end: |
| 1978 | return ret; |
| 1979 | } |