| 1 | /* |
| 2 | * event-fields.c |
| 3 | * |
| 4 | * Babeltrace CTF Writer |
| 5 | * |
| 6 | * Copyright 2013 EfficiOS Inc. |
| 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-writer/event-fields-internal.h> |
| 31 | #include <babeltrace/ctf-writer/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 *field); |
| 80 | static |
| 81 | int bt_ctf_field_structure_validate(struct bt_ctf_field *field); |
| 82 | static |
| 83 | int bt_ctf_field_variant_validate(struct bt_ctf_field *field); |
| 84 | static |
| 85 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field); |
| 86 | static |
| 87 | int bt_ctf_field_array_validate(struct bt_ctf_field *field); |
| 88 | static |
| 89 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *field); |
| 90 | |
| 91 | static |
| 92 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *, |
| 93 | struct ctf_stream_pos *); |
| 94 | static |
| 95 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *, |
| 96 | struct ctf_stream_pos *); |
| 97 | static |
| 98 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *, |
| 99 | struct ctf_stream_pos *); |
| 100 | static |
| 101 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *, |
| 102 | struct ctf_stream_pos *); |
| 103 | static |
| 104 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *, |
| 105 | struct ctf_stream_pos *); |
| 106 | static |
| 107 | int bt_ctf_field_array_serialize(struct bt_ctf_field *, |
| 108 | struct ctf_stream_pos *); |
| 109 | static |
| 110 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *, |
| 111 | struct ctf_stream_pos *); |
| 112 | static |
| 113 | int bt_ctf_field_string_serialize(struct bt_ctf_field *, |
| 114 | struct ctf_stream_pos *); |
| 115 | |
| 116 | static |
| 117 | int increase_packet_size(struct ctf_stream_pos *pos); |
| 118 | |
| 119 | static |
| 120 | struct bt_ctf_field *(*field_create_funcs[])( |
| 121 | struct bt_ctf_field_type *) = { |
| 122 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create, |
| 123 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create, |
| 124 | [CTF_TYPE_FLOAT] = |
| 125 | bt_ctf_field_floating_point_create, |
| 126 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create, |
| 127 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create, |
| 128 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_create, |
| 129 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create, |
| 130 | [CTF_TYPE_STRING] = bt_ctf_field_string_create, |
| 131 | }; |
| 132 | |
| 133 | static |
| 134 | void (*field_destroy_funcs[])(struct bt_ctf_field *) = { |
| 135 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy, |
| 136 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy, |
| 137 | [CTF_TYPE_FLOAT] = |
| 138 | bt_ctf_field_floating_point_destroy, |
| 139 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy, |
| 140 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy, |
| 141 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy, |
| 142 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy, |
| 143 | [CTF_TYPE_STRING] = bt_ctf_field_string_destroy, |
| 144 | }; |
| 145 | |
| 146 | static |
| 147 | int (*field_validate_funcs[])(struct bt_ctf_field *) = { |
| 148 | [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate, |
| 149 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate, |
| 150 | [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate, |
| 151 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate, |
| 152 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate, |
| 153 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate, |
| 154 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate, |
| 155 | [CTF_TYPE_STRING] = bt_ctf_field_generic_validate, |
| 156 | }; |
| 157 | |
| 158 | static |
| 159 | int (*field_serialize_funcs[])(struct bt_ctf_field *, |
| 160 | struct ctf_stream_pos *) = { |
| 161 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize, |
| 162 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize, |
| 163 | [CTF_TYPE_FLOAT] = |
| 164 | bt_ctf_field_floating_point_serialize, |
| 165 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize, |
| 166 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize, |
| 167 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize, |
| 168 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize, |
| 169 | [CTF_TYPE_STRING] = bt_ctf_field_string_serialize, |
| 170 | }; |
| 171 | |
| 172 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) |
| 173 | { |
| 174 | struct bt_ctf_field *field = NULL; |
| 175 | enum ctf_type_id type_id; |
| 176 | |
| 177 | if (!type) { |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | type_id = bt_ctf_field_type_get_type_id(type); |
| 182 | if (type_id <= CTF_TYPE_UNKNOWN || |
| 183 | type_id >= NR_CTF_TYPES) { |
| 184 | goto error; |
| 185 | } |
| 186 | |
| 187 | field = field_create_funcs[type_id](type); |
| 188 | if (!field) { |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | /* The type's declaration can't change after this point */ |
| 193 | bt_ctf_field_type_freeze(type); |
| 194 | bt_ctf_field_type_get(type); |
| 195 | bt_ctf_ref_init(&field->ref_count); |
| 196 | field->type = type; |
| 197 | error: |
| 198 | return field; |
| 199 | } |
| 200 | |
| 201 | void bt_ctf_field_get(struct bt_ctf_field *field) |
| 202 | { |
| 203 | if (field) { |
| 204 | bt_ctf_ref_get(&field->ref_count); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void bt_ctf_field_put(struct bt_ctf_field *field) |
| 209 | { |
| 210 | if (field) { |
| 211 | bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
| 216 | struct bt_ctf_field *length_field) |
| 217 | { |
| 218 | int ret = 0; |
| 219 | struct bt_ctf_field_type_integer *length_type; |
| 220 | struct bt_ctf_field_integer *length; |
| 221 | struct bt_ctf_field_sequence *sequence; |
| 222 | uint64_t sequence_length; |
| 223 | |
| 224 | if (!field || !length_field) { |
| 225 | ret = -1; |
| 226 | goto end; |
| 227 | } |
| 228 | if (bt_ctf_field_type_get_type_id(length_field->type) != |
| 229 | CTF_TYPE_INTEGER) { |
| 230 | ret = -1; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | length_type = container_of(length_field->type, |
| 235 | struct bt_ctf_field_type_integer, parent); |
| 236 | if (length_type->declaration.signedness) { |
| 237 | ret = -1; |
| 238 | goto end; |
| 239 | } |
| 240 | |
| 241 | length = container_of(length_field, struct bt_ctf_field_integer, |
| 242 | parent); |
| 243 | sequence_length = length->definition.value._unsigned; |
| 244 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 245 | if (sequence->elements) { |
| 246 | g_ptr_array_free(sequence->elements, TRUE); |
| 247 | bt_ctf_field_put(sequence->length); |
| 248 | } |
| 249 | |
| 250 | sequence->elements = g_ptr_array_new_full((size_t)sequence_length, |
| 251 | (GDestroyNotify)bt_ctf_field_put); |
| 252 | if (!sequence->elements) { |
| 253 | ret = -1; |
| 254 | goto end; |
| 255 | } |
| 256 | |
| 257 | g_ptr_array_set_size(sequence->elements, (size_t)sequence_length); |
| 258 | bt_ctf_field_get(length_field); |
| 259 | sequence->length = length_field; |
| 260 | end: |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | struct bt_ctf_field *bt_ctf_field_structure_get_field( |
| 265 | struct bt_ctf_field *field, const char *name) |
| 266 | { |
| 267 | struct bt_ctf_field *new_field = NULL; |
| 268 | GQuark field_quark; |
| 269 | struct bt_ctf_field_structure *structure; |
| 270 | struct bt_ctf_field_type_structure *structure_type; |
| 271 | struct bt_ctf_field_type *field_type; |
| 272 | size_t index; |
| 273 | |
| 274 | if (!field || !name || |
| 275 | bt_ctf_field_type_get_type_id(field->type) != |
| 276 | CTF_TYPE_STRUCT) { |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | field_quark = g_quark_from_string(name); |
| 281 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 282 | structure_type = container_of(field->type, |
| 283 | struct bt_ctf_field_type_structure, parent); |
| 284 | field_type = bt_ctf_field_type_structure_get_type(structure_type, name); |
| 285 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 286 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) { |
| 287 | goto error; |
| 288 | } |
| 289 | |
| 290 | if (structure->fields->pdata[index]) { |
| 291 | new_field = structure->fields->pdata[index]; |
| 292 | goto end; |
| 293 | } |
| 294 | |
| 295 | new_field = bt_ctf_field_create(field_type); |
| 296 | if (!new_field) { |
| 297 | goto error; |
| 298 | } |
| 299 | |
| 300 | structure->fields->pdata[index] = new_field; |
| 301 | end: |
| 302 | bt_ctf_field_get(new_field); |
| 303 | error: |
| 304 | return new_field; |
| 305 | } |
| 306 | |
| 307 | BT_HIDDEN |
| 308 | int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, |
| 309 | const char *name, struct bt_ctf_field *value) |
| 310 | { |
| 311 | int ret = 0; |
| 312 | GQuark field_quark; |
| 313 | struct bt_ctf_field_structure *structure; |
| 314 | struct bt_ctf_field_type_structure *structure_type; |
| 315 | struct bt_ctf_field_type *expected_field_type; |
| 316 | size_t index; |
| 317 | |
| 318 | if (!field || !name || !value || |
| 319 | bt_ctf_field_type_get_type_id(field->type) != |
| 320 | CTF_TYPE_STRUCT) { |
| 321 | ret = -1; |
| 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | field_quark = g_quark_from_string(name); |
| 326 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 327 | structure_type = container_of(field->type, |
| 328 | struct bt_ctf_field_type_structure, parent); |
| 329 | expected_field_type = bt_ctf_field_type_structure_get_type( |
| 330 | structure_type, name); |
| 331 | if (expected_field_type != value->type) { |
| 332 | ret = -1; |
| 333 | goto end; |
| 334 | } |
| 335 | |
| 336 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 337 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) { |
| 338 | goto end; |
| 339 | } |
| 340 | |
| 341 | if (structure->fields->pdata[index]) { |
| 342 | bt_ctf_field_put(structure->fields->pdata[index]); |
| 343 | } |
| 344 | |
| 345 | structure->fields->pdata[index] = value; |
| 346 | bt_ctf_field_get(value); |
| 347 | end: |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field, |
| 352 | uint64_t index) |
| 353 | { |
| 354 | struct bt_ctf_field *new_field = NULL; |
| 355 | struct bt_ctf_field_array *array; |
| 356 | struct bt_ctf_field_type_array *array_type; |
| 357 | struct bt_ctf_field_type *field_type; |
| 358 | |
| 359 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 360 | CTF_TYPE_ARRAY) { |
| 361 | goto end; |
| 362 | } |
| 363 | |
| 364 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 365 | if (index >= array->elements->len) { |
| 366 | goto end; |
| 367 | } |
| 368 | |
| 369 | array_type = container_of(field->type, struct bt_ctf_field_type_array, |
| 370 | parent); |
| 371 | field_type = bt_ctf_field_type_array_get_element_type(array_type); |
| 372 | if (array->elements->pdata[(size_t)index]) { |
| 373 | new_field = array->elements->pdata[(size_t)index]; |
| 374 | goto end; |
| 375 | } |
| 376 | |
| 377 | new_field = bt_ctf_field_create(field_type); |
| 378 | bt_ctf_field_get(new_field); |
| 379 | array->elements->pdata[(size_t)index] = new_field; |
| 380 | end: |
| 381 | return new_field; |
| 382 | } |
| 383 | |
| 384 | struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field, |
| 385 | uint64_t index) |
| 386 | { |
| 387 | struct bt_ctf_field *new_field = NULL; |
| 388 | struct bt_ctf_field_sequence *sequence; |
| 389 | struct bt_ctf_field_type_sequence *sequence_type; |
| 390 | struct bt_ctf_field_type *field_type; |
| 391 | |
| 392 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 393 | CTF_TYPE_SEQUENCE) { |
| 394 | goto end; |
| 395 | } |
| 396 | |
| 397 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 398 | if (!sequence->elements || sequence->elements->len <= index) { |
| 399 | goto end; |
| 400 | } |
| 401 | |
| 402 | sequence_type = container_of(field->type, |
| 403 | struct bt_ctf_field_type_sequence, parent); |
| 404 | field_type = bt_ctf_field_type_sequence_get_element_type(sequence_type); |
| 405 | if (sequence->elements->pdata[(size_t)index]) { |
| 406 | new_field = sequence->elements->pdata[(size_t)index]; |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | new_field = bt_ctf_field_create(field_type); |
| 411 | bt_ctf_field_get(new_field); |
| 412 | sequence->elements->pdata[(size_t)index] = new_field; |
| 413 | end: |
| 414 | return new_field; |
| 415 | } |
| 416 | |
| 417 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, |
| 418 | struct bt_ctf_field *tag_field) |
| 419 | { |
| 420 | struct bt_ctf_field *new_field = NULL; |
| 421 | struct bt_ctf_field_variant *variant; |
| 422 | struct bt_ctf_field_type_variant *variant_type; |
| 423 | struct bt_ctf_field_type *field_type; |
| 424 | struct bt_ctf_field *tag_enum = NULL; |
| 425 | struct bt_ctf_field_integer *tag_enum_integer; |
| 426 | int64_t tag_enum_value; |
| 427 | |
| 428 | if (!field || !tag_field || |
| 429 | bt_ctf_field_type_get_type_id(field->type) != |
| 430 | CTF_TYPE_VARIANT || |
| 431 | bt_ctf_field_type_get_type_id(tag_field->type) != |
| 432 | CTF_TYPE_ENUM) { |
| 433 | goto end; |
| 434 | } |
| 435 | |
| 436 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 437 | variant_type = container_of(field->type, |
| 438 | struct bt_ctf_field_type_variant, parent); |
| 439 | tag_enum = bt_ctf_field_enumeration_get_container(tag_field); |
| 440 | if (!tag_enum) { |
| 441 | goto end; |
| 442 | } |
| 443 | |
| 444 | tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer, |
| 445 | parent); |
| 446 | |
| 447 | if (!bt_ctf_field_validate(variant->tag)) { |
| 448 | goto end; |
| 449 | } |
| 450 | |
| 451 | tag_enum_value = tag_enum_integer->definition.value._signed; |
| 452 | field_type = bt_ctf_field_type_variant_get_field_type(variant_type, |
| 453 | tag_enum_value); |
| 454 | if (!field_type) { |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | new_field = bt_ctf_field_create(field_type); |
| 459 | if (!new_field) { |
| 460 | goto end; |
| 461 | } |
| 462 | |
| 463 | bt_ctf_field_put(variant->tag); |
| 464 | bt_ctf_field_put(variant->payload); |
| 465 | bt_ctf_field_get(new_field); |
| 466 | bt_ctf_field_get(tag_field); |
| 467 | variant->tag = tag_field; |
| 468 | variant->payload = new_field; |
| 469 | end: |
| 470 | bt_ctf_field_put(tag_enum); |
| 471 | return new_field; |
| 472 | } |
| 473 | |
| 474 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( |
| 475 | struct bt_ctf_field *field) |
| 476 | { |
| 477 | struct bt_ctf_field *container = NULL; |
| 478 | struct bt_ctf_field_enumeration *enumeration; |
| 479 | |
| 480 | if (!field) { |
| 481 | goto end; |
| 482 | } |
| 483 | |
| 484 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 485 | parent); |
| 486 | if (!enumeration->payload) { |
| 487 | struct bt_ctf_field_type_enumeration *enumeration_type = |
| 488 | container_of(field->type, |
| 489 | struct bt_ctf_field_type_enumeration, parent); |
| 490 | enumeration->payload = |
| 491 | bt_ctf_field_create(enumeration_type->container); |
| 492 | } |
| 493 | |
| 494 | container = enumeration->payload; |
| 495 | bt_ctf_field_get(container); |
| 496 | end: |
| 497 | return container; |
| 498 | } |
| 499 | |
| 500 | int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field, |
| 501 | int64_t value) |
| 502 | { |
| 503 | int ret = 0; |
| 504 | struct bt_ctf_field_integer *integer; |
| 505 | struct bt_ctf_field_type_integer *integer_type; |
| 506 | unsigned int size; |
| 507 | int64_t min_value, max_value; |
| 508 | |
| 509 | if (!field || |
| 510 | bt_ctf_field_type_get_type_id(field->type) != |
| 511 | CTF_TYPE_INTEGER) { |
| 512 | ret = -1; |
| 513 | goto end; |
| 514 | } |
| 515 | |
| 516 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 517 | integer_type = container_of(field->type, |
| 518 | struct bt_ctf_field_type_integer, parent); |
| 519 | if (!integer_type->declaration.signedness) { |
| 520 | ret = -1; |
| 521 | goto end; |
| 522 | } |
| 523 | |
| 524 | size = integer_type->declaration.len; |
| 525 | min_value = -((int64_t)1 << (size - 1)); |
| 526 | max_value = ((int64_t)1 << (size - 1)) - 1; |
| 527 | if (value < min_value || value > max_value) { |
| 528 | ret = -1; |
| 529 | goto end; |
| 530 | } |
| 531 | |
| 532 | integer->definition.value._signed = value; |
| 533 | integer->parent.payload_set = 1; |
| 534 | end: |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field, |
| 539 | uint64_t value) |
| 540 | { |
| 541 | int ret = 0; |
| 542 | struct bt_ctf_field_integer *integer; |
| 543 | struct bt_ctf_field_type_integer *integer_type; |
| 544 | unsigned int size; |
| 545 | uint64_t max_value; |
| 546 | |
| 547 | if (!field || |
| 548 | bt_ctf_field_type_get_type_id(field->type) != |
| 549 | CTF_TYPE_INTEGER) { |
| 550 | ret = -1; |
| 551 | goto end; |
| 552 | } |
| 553 | |
| 554 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 555 | integer_type = container_of(field->type, |
| 556 | struct bt_ctf_field_type_integer, parent); |
| 557 | if (integer_type->declaration.signedness) { |
| 558 | ret = -1; |
| 559 | goto end; |
| 560 | } |
| 561 | |
| 562 | size = integer_type->declaration.len; |
| 563 | max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1; |
| 564 | if (value > max_value) { |
| 565 | ret = -1; |
| 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | integer->definition.value._unsigned = value; |
| 570 | integer->parent.payload_set = 1; |
| 571 | end: |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, |
| 576 | double value) |
| 577 | { |
| 578 | int ret = 0; |
| 579 | struct bt_ctf_field_floating_point *floating_point; |
| 580 | |
| 581 | if (!field || |
| 582 | bt_ctf_field_type_get_type_id(field->type) != |
| 583 | CTF_TYPE_FLOAT) { |
| 584 | ret = -1; |
| 585 | goto end; |
| 586 | } |
| 587 | floating_point = container_of(field, struct bt_ctf_field_floating_point, |
| 588 | parent); |
| 589 | floating_point->definition.value = value; |
| 590 | floating_point->parent.payload_set = 1; |
| 591 | end: |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, |
| 596 | const char *value) |
| 597 | { |
| 598 | int ret = 0; |
| 599 | struct bt_ctf_field_string *string; |
| 600 | |
| 601 | if (!field || !value || |
| 602 | bt_ctf_field_type_get_type_id(field->type) != |
| 603 | CTF_TYPE_STRING) { |
| 604 | ret = -1; |
| 605 | goto end; |
| 606 | } |
| 607 | |
| 608 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 609 | if (string->payload) { |
| 610 | g_string_free(string->payload, TRUE); |
| 611 | } |
| 612 | |
| 613 | string->payload = g_string_new(value); |
| 614 | string->parent.payload_set = 1; |
| 615 | end: |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | BT_HIDDEN |
| 620 | int bt_ctf_field_validate(struct bt_ctf_field *field) |
| 621 | { |
| 622 | int ret = 0; |
| 623 | enum ctf_type_id type_id; |
| 624 | |
| 625 | if (!field) { |
| 626 | ret = -1; |
| 627 | goto end; |
| 628 | } |
| 629 | |
| 630 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 631 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 632 | ret = -1; |
| 633 | goto end; |
| 634 | } |
| 635 | |
| 636 | ret = field_validate_funcs[type_id](field); |
| 637 | end: |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | BT_HIDDEN |
| 642 | int bt_ctf_field_serialize(struct bt_ctf_field *field, |
| 643 | struct ctf_stream_pos *pos) |
| 644 | { |
| 645 | int ret = 0; |
| 646 | enum ctf_type_id type_id; |
| 647 | |
| 648 | if (!field || !pos) { |
| 649 | ret = -1; |
| 650 | goto end; |
| 651 | } |
| 652 | |
| 653 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 654 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 655 | ret = -1; |
| 656 | goto end; |
| 657 | } |
| 658 | |
| 659 | ret = field_serialize_funcs[type_id](field, pos); |
| 660 | end: |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static |
| 665 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) |
| 666 | { |
| 667 | struct bt_ctf_field_type_integer *integer_type = container_of(type, |
| 668 | struct bt_ctf_field_type_integer, parent); |
| 669 | struct bt_ctf_field_integer *integer = g_new0( |
| 670 | struct bt_ctf_field_integer, 1); |
| 671 | |
| 672 | if (integer) { |
| 673 | integer->definition.declaration = &integer_type->declaration; |
| 674 | } |
| 675 | |
| 676 | return integer ? &integer->parent : NULL; |
| 677 | } |
| 678 | |
| 679 | static |
| 680 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 681 | struct bt_ctf_field_type *type) |
| 682 | { |
| 683 | struct bt_ctf_field_enumeration *enumeration = g_new0( |
| 684 | struct bt_ctf_field_enumeration, 1); |
| 685 | |
| 686 | return enumeration ? &enumeration->parent : NULL; |
| 687 | } |
| 688 | |
| 689 | static |
| 690 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 691 | struct bt_ctf_field_type *type) |
| 692 | { |
| 693 | struct bt_ctf_field_floating_point *floating_point; |
| 694 | struct bt_ctf_field_type_floating_point *floating_point_type; |
| 695 | |
| 696 | floating_point = g_new0(struct bt_ctf_field_floating_point, 1); |
| 697 | if (!floating_point) { |
| 698 | goto end; |
| 699 | } |
| 700 | |
| 701 | floating_point_type = container_of(type, |
| 702 | struct bt_ctf_field_type_floating_point, parent); |
| 703 | floating_point->definition.declaration = container_of( |
| 704 | type->declaration, struct declaration_float, p); |
| 705 | |
| 706 | |
| 707 | floating_point->definition.sign = &floating_point->sign; |
| 708 | floating_point->sign.declaration = &floating_point_type->sign; |
| 709 | floating_point->definition.sign->p.declaration = |
| 710 | &floating_point_type->sign.p; |
| 711 | |
| 712 | floating_point->definition.mantissa = &floating_point->mantissa; |
| 713 | floating_point->mantissa.declaration = &floating_point_type->mantissa; |
| 714 | floating_point->definition.mantissa->p.declaration = |
| 715 | &floating_point_type->mantissa.p; |
| 716 | |
| 717 | floating_point->definition.exp = &floating_point->exp; |
| 718 | floating_point->exp.declaration = &floating_point_type->exp; |
| 719 | floating_point->definition.exp->p.declaration = |
| 720 | &floating_point_type->exp.p; |
| 721 | |
| 722 | end: |
| 723 | return floating_point ? &floating_point->parent : NULL; |
| 724 | } |
| 725 | |
| 726 | static |
| 727 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 728 | struct bt_ctf_field_type *type) |
| 729 | { |
| 730 | struct bt_ctf_field_type_structure *structure_type = container_of(type, |
| 731 | struct bt_ctf_field_type_structure, parent); |
| 732 | struct bt_ctf_field_structure *structure = g_new0( |
| 733 | struct bt_ctf_field_structure, 1); |
| 734 | struct bt_ctf_field *field = NULL; |
| 735 | |
| 736 | if (!structure || !structure_type->fields->len) { |
| 737 | goto end; |
| 738 | } |
| 739 | |
| 740 | structure->field_name_to_index = structure_type->field_name_to_index; |
| 741 | structure->fields = g_ptr_array_new_with_free_func( |
| 742 | (GDestroyNotify)bt_ctf_field_put); |
| 743 | g_ptr_array_set_size(structure->fields, |
| 744 | g_hash_table_size(structure->field_name_to_index)); |
| 745 | field = &structure->parent; |
| 746 | end: |
| 747 | return field; |
| 748 | } |
| 749 | |
| 750 | static |
| 751 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) |
| 752 | { |
| 753 | struct bt_ctf_field_variant *variant = g_new0( |
| 754 | struct bt_ctf_field_variant, 1); |
| 755 | return variant ? &variant->parent : NULL; |
| 756 | } |
| 757 | |
| 758 | static |
| 759 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) |
| 760 | { |
| 761 | struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1); |
| 762 | struct bt_ctf_field_type_array *array_type; |
| 763 | unsigned int array_length; |
| 764 | |
| 765 | if (!array || !type) { |
| 766 | goto error; |
| 767 | } |
| 768 | |
| 769 | array_type = container_of(type, struct bt_ctf_field_type_array, parent); |
| 770 | array_length = array_type->length; |
| 771 | array->elements = g_ptr_array_new_full(array_length, |
| 772 | (GDestroyNotify)bt_ctf_field_put); |
| 773 | if (!array->elements) { |
| 774 | goto error; |
| 775 | } |
| 776 | |
| 777 | g_ptr_array_set_size(array->elements, array_length); |
| 778 | return &array->parent; |
| 779 | error: |
| 780 | g_free(array); |
| 781 | return NULL; |
| 782 | } |
| 783 | |
| 784 | static |
| 785 | struct bt_ctf_field *bt_ctf_field_sequence_create( |
| 786 | struct bt_ctf_field_type *type) |
| 787 | { |
| 788 | struct bt_ctf_field_sequence *sequence = g_new0( |
| 789 | struct bt_ctf_field_sequence, 1); |
| 790 | return sequence ? &sequence->parent : NULL; |
| 791 | } |
| 792 | |
| 793 | static |
| 794 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) |
| 795 | { |
| 796 | struct bt_ctf_field_string *string = g_new0( |
| 797 | struct bt_ctf_field_string, 1); |
| 798 | return string ? &string->parent : NULL; |
| 799 | } |
| 800 | |
| 801 | static |
| 802 | void bt_ctf_field_destroy(struct bt_ctf_ref *ref) |
| 803 | { |
| 804 | struct bt_ctf_field *field; |
| 805 | struct bt_ctf_field_type *type; |
| 806 | enum ctf_type_id type_id; |
| 807 | |
| 808 | if (!ref) { |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | field = container_of(ref, struct bt_ctf_field, ref_count); |
| 813 | type = field->type; |
| 814 | type_id = bt_ctf_field_type_get_type_id(type); |
| 815 | if (type_id <= CTF_TYPE_UNKNOWN || |
| 816 | type_id >= NR_CTF_TYPES) { |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | field_destroy_funcs[type_id](field); |
| 821 | if (type) { |
| 822 | bt_ctf_field_type_put(type); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | static |
| 827 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) |
| 828 | { |
| 829 | struct bt_ctf_field_integer *integer; |
| 830 | |
| 831 | if (!field) { |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 836 | g_free(integer); |
| 837 | } |
| 838 | |
| 839 | static |
| 840 | void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field) |
| 841 | { |
| 842 | struct bt_ctf_field_enumeration *enumeration; |
| 843 | |
| 844 | if (!field) { |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 849 | parent); |
| 850 | bt_ctf_field_put(enumeration->payload); |
| 851 | g_free(enumeration); |
| 852 | } |
| 853 | |
| 854 | static |
| 855 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) |
| 856 | { |
| 857 | struct bt_ctf_field_floating_point *floating_point; |
| 858 | |
| 859 | if (!field) { |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | floating_point = container_of(field, struct bt_ctf_field_floating_point, |
| 864 | parent); |
| 865 | g_free(floating_point); |
| 866 | } |
| 867 | |
| 868 | static |
| 869 | void bt_ctf_field_structure_destroy(struct bt_ctf_field *field) |
| 870 | { |
| 871 | struct bt_ctf_field_structure *structure; |
| 872 | |
| 873 | if (!field) { |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 878 | g_ptr_array_free(structure->fields, TRUE); |
| 879 | g_free(structure); |
| 880 | } |
| 881 | |
| 882 | static |
| 883 | void bt_ctf_field_variant_destroy(struct bt_ctf_field *field) |
| 884 | { |
| 885 | struct bt_ctf_field_variant *variant; |
| 886 | |
| 887 | if (!field) { |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 892 | bt_ctf_field_put(variant->tag); |
| 893 | bt_ctf_field_put(variant->payload); |
| 894 | g_free(variant); |
| 895 | } |
| 896 | |
| 897 | static |
| 898 | void bt_ctf_field_array_destroy(struct bt_ctf_field *field) |
| 899 | { |
| 900 | struct bt_ctf_field_array *array; |
| 901 | |
| 902 | if (!field) { |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 907 | g_ptr_array_free(array->elements, TRUE); |
| 908 | g_free(array); |
| 909 | } |
| 910 | |
| 911 | static |
| 912 | void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field) |
| 913 | { |
| 914 | struct bt_ctf_field_sequence *sequence; |
| 915 | |
| 916 | if (!field) { |
| 917 | return; |
| 918 | } |
| 919 | |
| 920 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 921 | g_ptr_array_free(sequence->elements, TRUE); |
| 922 | bt_ctf_field_put(sequence->length); |
| 923 | g_free(sequence); |
| 924 | } |
| 925 | |
| 926 | static |
| 927 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) |
| 928 | { |
| 929 | struct bt_ctf_field_string *string; |
| 930 | if (!field) { |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 935 | g_string_free(string->payload, TRUE); |
| 936 | g_free(string); |
| 937 | } |
| 938 | |
| 939 | static |
| 940 | int bt_ctf_field_generic_validate(struct bt_ctf_field *field) |
| 941 | { |
| 942 | return !(field && field->payload_set); |
| 943 | } |
| 944 | |
| 945 | static |
| 946 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) |
| 947 | { |
| 948 | int ret; |
| 949 | struct bt_ctf_field_enumeration *enumeration; |
| 950 | |
| 951 | if (!field) { |
| 952 | ret = -1; |
| 953 | goto end; |
| 954 | } |
| 955 | |
| 956 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 957 | parent); |
| 958 | if (!enumeration->payload) { |
| 959 | ret = -1; |
| 960 | goto end; |
| 961 | } |
| 962 | |
| 963 | ret = bt_ctf_field_validate(enumeration->payload); |
| 964 | end: |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | static |
| 969 | int bt_ctf_field_structure_validate(struct bt_ctf_field *field) |
| 970 | { |
| 971 | size_t i; |
| 972 | int ret = 0; |
| 973 | struct bt_ctf_field_structure *structure; |
| 974 | |
| 975 | if (!field) { |
| 976 | ret = -1; |
| 977 | goto end; |
| 978 | } |
| 979 | |
| 980 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 981 | for (i = 0; i < structure->fields->len; i++) { |
| 982 | ret = bt_ctf_field_validate(structure->fields->pdata[i]); |
| 983 | if (ret) { |
| 984 | goto end; |
| 985 | } |
| 986 | } |
| 987 | end: |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | static |
| 992 | int bt_ctf_field_variant_validate(struct bt_ctf_field *field) |
| 993 | { |
| 994 | int ret = 0; |
| 995 | struct bt_ctf_field_variant *variant; |
| 996 | |
| 997 | if (!field) { |
| 998 | ret = -1; |
| 999 | goto end; |
| 1000 | } |
| 1001 | |
| 1002 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 1003 | ret = bt_ctf_field_validate(variant->payload); |
| 1004 | end: |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | static |
| 1009 | int bt_ctf_field_array_validate(struct bt_ctf_field *field) |
| 1010 | { |
| 1011 | size_t i; |
| 1012 | int ret = 0; |
| 1013 | struct bt_ctf_field_array *array; |
| 1014 | |
| 1015 | if (!field) { |
| 1016 | ret = -1; |
| 1017 | goto end; |
| 1018 | } |
| 1019 | |
| 1020 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 1021 | for (i = 0; i < array->elements->len; i++) { |
| 1022 | ret = bt_ctf_field_validate(array->elements->pdata[i]); |
| 1023 | if (ret) { |
| 1024 | goto end; |
| 1025 | } |
| 1026 | } |
| 1027 | end: |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | static |
| 1032 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) |
| 1033 | { |
| 1034 | size_t i; |
| 1035 | int ret = 0; |
| 1036 | struct bt_ctf_field_sequence *sequence; |
| 1037 | |
| 1038 | if (!field) { |
| 1039 | ret = -1; |
| 1040 | goto end; |
| 1041 | } |
| 1042 | |
| 1043 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 1044 | for (i = 0; i < sequence->elements->len; i++) { |
| 1045 | ret = bt_ctf_field_validate(sequence->elements->pdata[i]); |
| 1046 | if (ret) { |
| 1047 | goto end; |
| 1048 | } |
| 1049 | } |
| 1050 | end: |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
| 1054 | static |
| 1055 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, |
| 1056 | struct ctf_stream_pos *pos) |
| 1057 | { |
| 1058 | int ret = 0; |
| 1059 | struct bt_ctf_field_integer *integer = container_of(field, |
| 1060 | struct bt_ctf_field_integer, parent); |
| 1061 | |
| 1062 | retry: |
| 1063 | ret = ctf_integer_write(&pos->parent, &integer->definition.p); |
| 1064 | if (ret == -EFAULT) { |
| 1065 | /* |
| 1066 | * The field is too large to fit in the current packet's |
| 1067 | * remaining space. Bump the packet size and retry. |
| 1068 | */ |
| 1069 | ret = increase_packet_size(pos); |
| 1070 | if (ret) { |
| 1071 | goto end; |
| 1072 | } |
| 1073 | goto retry; |
| 1074 | } |
| 1075 | end: |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
| 1079 | static |
| 1080 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, |
| 1081 | struct ctf_stream_pos *pos) |
| 1082 | { |
| 1083 | struct bt_ctf_field_enumeration *enumeration = container_of( |
| 1084 | field, struct bt_ctf_field_enumeration, parent); |
| 1085 | |
| 1086 | return bt_ctf_field_serialize(enumeration->payload, pos); |
| 1087 | } |
| 1088 | |
| 1089 | static |
| 1090 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, |
| 1091 | struct ctf_stream_pos *pos) |
| 1092 | { |
| 1093 | int ret = 0; |
| 1094 | struct bt_ctf_field_floating_point *floating_point = container_of(field, |
| 1095 | struct bt_ctf_field_floating_point, parent); |
| 1096 | |
| 1097 | retry: |
| 1098 | ret = ctf_float_write(&pos->parent, &floating_point->definition.p); |
| 1099 | if (ret == -EFAULT) { |
| 1100 | /* |
| 1101 | * The field is too large to fit in the current packet's |
| 1102 | * remaining space. Bump the packet size and retry. |
| 1103 | */ |
| 1104 | ret = increase_packet_size(pos); |
| 1105 | if (ret) { |
| 1106 | goto end; |
| 1107 | } |
| 1108 | goto retry; |
| 1109 | } |
| 1110 | end: |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | static |
| 1115 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, |
| 1116 | struct ctf_stream_pos *pos) |
| 1117 | { |
| 1118 | size_t i; |
| 1119 | int ret = 0; |
| 1120 | struct bt_ctf_field_structure *structure = container_of( |
| 1121 | field, struct bt_ctf_field_structure, parent); |
| 1122 | |
| 1123 | while (!ctf_pos_access_ok(pos, |
| 1124 | offset_align(pos->offset, |
| 1125 | field->type->declaration->alignment))) { |
| 1126 | increase_packet_size(pos); |
| 1127 | } |
| 1128 | |
| 1129 | ctf_align_pos(pos, field->type->declaration->alignment); |
| 1130 | |
| 1131 | for (i = 0; i < structure->fields->len; i++) { |
| 1132 | struct bt_ctf_field *field = g_ptr_array_index( |
| 1133 | structure->fields, i); |
| 1134 | |
| 1135 | ret = bt_ctf_field_serialize(field, pos); |
| 1136 | if (ret) { |
| 1137 | break; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | return ret; |
| 1142 | } |
| 1143 | |
| 1144 | static |
| 1145 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, |
| 1146 | struct ctf_stream_pos *pos) |
| 1147 | { |
| 1148 | struct bt_ctf_field_variant *variant = container_of( |
| 1149 | field, struct bt_ctf_field_variant, parent); |
| 1150 | |
| 1151 | return bt_ctf_field_serialize(variant->payload, pos); |
| 1152 | } |
| 1153 | |
| 1154 | static |
| 1155 | int bt_ctf_field_array_serialize(struct bt_ctf_field *field, |
| 1156 | struct ctf_stream_pos *pos) |
| 1157 | { |
| 1158 | size_t i; |
| 1159 | int ret = 0; |
| 1160 | struct bt_ctf_field_array *array = container_of( |
| 1161 | field, struct bt_ctf_field_array, parent); |
| 1162 | |
| 1163 | for (i = 0; i < array->elements->len; i++) { |
| 1164 | ret = bt_ctf_field_serialize( |
| 1165 | g_ptr_array_index(array->elements, i), pos); |
| 1166 | if (ret) { |
| 1167 | goto end; |
| 1168 | } |
| 1169 | } |
| 1170 | end: |
| 1171 | return ret; |
| 1172 | } |
| 1173 | |
| 1174 | static |
| 1175 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, |
| 1176 | struct ctf_stream_pos *pos) |
| 1177 | { |
| 1178 | size_t i; |
| 1179 | int ret = 0; |
| 1180 | struct bt_ctf_field_sequence *sequence = container_of( |
| 1181 | field, struct bt_ctf_field_sequence, parent); |
| 1182 | |
| 1183 | for (i = 0; i < sequence->elements->len; i++) { |
| 1184 | ret = bt_ctf_field_serialize( |
| 1185 | g_ptr_array_index(sequence->elements, i), pos); |
| 1186 | if (ret) { |
| 1187 | goto end; |
| 1188 | } |
| 1189 | } |
| 1190 | end: |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | static |
| 1195 | int bt_ctf_field_string_serialize(struct bt_ctf_field *field, |
| 1196 | struct ctf_stream_pos *pos) |
| 1197 | { |
| 1198 | size_t i; |
| 1199 | int ret = 0; |
| 1200 | struct bt_ctf_field_string *string = container_of(field, |
| 1201 | struct bt_ctf_field_string, parent); |
| 1202 | struct bt_ctf_field_type *character_type = |
| 1203 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 1204 | struct bt_ctf_field *character = bt_ctf_field_create(character_type); |
| 1205 | |
| 1206 | for (i = 0; i < string->payload->len + 1; i++) { |
| 1207 | ret = bt_ctf_field_unsigned_integer_set_value(character, |
| 1208 | (uint64_t) string->payload->str[i]); |
| 1209 | if (ret) { |
| 1210 | goto end; |
| 1211 | } |
| 1212 | |
| 1213 | ret = bt_ctf_field_integer_serialize(character, pos); |
| 1214 | if (ret) { |
| 1215 | goto end; |
| 1216 | } |
| 1217 | } |
| 1218 | end: |
| 1219 | bt_ctf_field_put(character); |
| 1220 | bt_ctf_field_type_put(character_type); |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
| 1224 | static |
| 1225 | int increase_packet_size(struct ctf_stream_pos *pos) |
| 1226 | { |
| 1227 | int ret; |
| 1228 | |
| 1229 | assert(pos); |
| 1230 | ret = munmap_align(pos->base_mma); |
| 1231 | if (ret) { |
| 1232 | goto end; |
| 1233 | } |
| 1234 | |
| 1235 | pos->packet_size += PACKET_LEN_INCREMENT; |
| 1236 | ret = posix_fallocate(pos->fd, pos->mmap_offset, |
| 1237 | pos->packet_size / CHAR_BIT); |
| 1238 | if (ret) { |
| 1239 | goto end; |
| 1240 | } |
| 1241 | |
| 1242 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
| 1243 | pos->flags, pos->fd, pos->mmap_offset); |
| 1244 | if (pos->base_mma == MAP_FAILED) { |
| 1245 | ret = -1; |
| 1246 | } |
| 1247 | end: |
| 1248 | return ret; |
| 1249 | } |