| 1 | /* |
| 2 | * event-fields.c |
| 3 | * |
| 4 | * Babeltrace CTF Writer |
| 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 *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 || type_id >= NR_CTF_TYPES || |
| 183 | bt_ctf_field_type_validate(type)) { |
| 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 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) |
| 216 | { |
| 217 | struct bt_ctf_field_type *ret = NULL; |
| 218 | |
| 219 | if (!field) { |
| 220 | goto end; |
| 221 | } |
| 222 | |
| 223 | ret = field->type; |
| 224 | bt_ctf_field_type_get(ret); |
| 225 | end: |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | struct bt_ctf_field *bt_ctf_field_sequence_get_length( |
| 230 | struct bt_ctf_field *field) |
| 231 | { |
| 232 | struct bt_ctf_field *ret = NULL; |
| 233 | struct bt_ctf_field_sequence *sequence; |
| 234 | |
| 235 | if (!field) { |
| 236 | goto end; |
| 237 | } |
| 238 | |
| 239 | if (bt_ctf_field_type_get_type_id(field->type) != |
| 240 | CTF_TYPE_SEQUENCE) { |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 245 | ret = sequence->length; |
| 246 | bt_ctf_field_get(ret); |
| 247 | end: |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
| 252 | struct bt_ctf_field *length_field) |
| 253 | { |
| 254 | int ret = 0; |
| 255 | struct bt_ctf_field_type_integer *length_type; |
| 256 | struct bt_ctf_field_integer *length; |
| 257 | struct bt_ctf_field_sequence *sequence; |
| 258 | uint64_t sequence_length; |
| 259 | |
| 260 | if (!field || !length_field) { |
| 261 | ret = -1; |
| 262 | goto end; |
| 263 | } |
| 264 | if (bt_ctf_field_type_get_type_id(length_field->type) != |
| 265 | CTF_TYPE_INTEGER) { |
| 266 | ret = -1; |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | length_type = container_of(length_field->type, |
| 271 | struct bt_ctf_field_type_integer, parent); |
| 272 | /* The length field must be unsigned */ |
| 273 | if (length_type->declaration.signedness) { |
| 274 | ret = -1; |
| 275 | goto end; |
| 276 | } |
| 277 | |
| 278 | length = container_of(length_field, struct bt_ctf_field_integer, |
| 279 | parent); |
| 280 | sequence_length = length->definition.value._unsigned; |
| 281 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 282 | if (sequence->elements) { |
| 283 | g_ptr_array_free(sequence->elements, TRUE); |
| 284 | bt_ctf_field_put(sequence->length); |
| 285 | } |
| 286 | |
| 287 | sequence->elements = g_ptr_array_sized_new((size_t)sequence_length); |
| 288 | if (!sequence->elements) { |
| 289 | ret = -1; |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | g_ptr_array_set_free_func(sequence->elements, |
| 294 | (GDestroyNotify)bt_ctf_field_put); |
| 295 | g_ptr_array_set_size(sequence->elements, (size_t)sequence_length); |
| 296 | bt_ctf_field_get(length_field); |
| 297 | sequence->length = length_field; |
| 298 | end: |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | struct bt_ctf_field *bt_ctf_field_structure_get_field( |
| 303 | struct bt_ctf_field *field, const char *name) |
| 304 | { |
| 305 | struct bt_ctf_field *new_field = NULL; |
| 306 | GQuark field_quark; |
| 307 | struct bt_ctf_field_structure *structure; |
| 308 | struct bt_ctf_field_type *field_type = NULL; |
| 309 | size_t index; |
| 310 | |
| 311 | if (!field || !name || |
| 312 | bt_ctf_field_type_get_type_id(field->type) != |
| 313 | CTF_TYPE_STRUCT) { |
| 314 | goto error; |
| 315 | } |
| 316 | |
| 317 | field_quark = g_quark_from_string(name); |
| 318 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 319 | field_type = |
| 320 | bt_ctf_field_type_structure_get_field_type_by_name(field->type, |
| 321 | name); |
| 322 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 323 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) { |
| 324 | goto error; |
| 325 | } |
| 326 | |
| 327 | if (structure->fields->pdata[index]) { |
| 328 | new_field = structure->fields->pdata[index]; |
| 329 | goto end; |
| 330 | } |
| 331 | |
| 332 | new_field = bt_ctf_field_create(field_type); |
| 333 | if (!new_field) { |
| 334 | goto error; |
| 335 | } |
| 336 | |
| 337 | structure->fields->pdata[index] = new_field; |
| 338 | end: |
| 339 | bt_ctf_field_get(new_field); |
| 340 | error: |
| 341 | if (field_type) { |
| 342 | bt_ctf_field_type_put(field_type); |
| 343 | } |
| 344 | return new_field; |
| 345 | } |
| 346 | |
| 347 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( |
| 348 | struct bt_ctf_field *field, size_t index) |
| 349 | { |
| 350 | int ret; |
| 351 | const char *field_name; |
| 352 | struct bt_ctf_field_structure *structure; |
| 353 | struct bt_ctf_field_type *structure_type; |
| 354 | struct bt_ctf_field_type *field_type = NULL; |
| 355 | struct bt_ctf_field *ret_field = NULL; |
| 356 | |
| 357 | if (!field || |
| 358 | bt_ctf_field_type_get_type_id(field->type) != CTF_TYPE_STRUCT) { |
| 359 | goto end; |
| 360 | } |
| 361 | |
| 362 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 363 | if (index >= structure->fields->len) { |
| 364 | goto error; |
| 365 | } |
| 366 | |
| 367 | ret_field = structure->fields->pdata[index]; |
| 368 | if (ret_field) { |
| 369 | goto end; |
| 370 | } |
| 371 | |
| 372 | /* Field has not been instanciated yet, create it */ |
| 373 | structure_type = bt_ctf_field_get_type(field); |
| 374 | if (!structure_type) { |
| 375 | goto error; |
| 376 | } |
| 377 | |
| 378 | ret = bt_ctf_field_type_structure_get_field(structure_type, |
| 379 | &field_name, &field_type, index); |
| 380 | bt_ctf_field_type_put(structure_type); |
| 381 | if (ret) { |
| 382 | goto error; |
| 383 | } |
| 384 | |
| 385 | ret_field = bt_ctf_field_create(field_type); |
| 386 | if (!ret_field) { |
| 387 | goto error; |
| 388 | } |
| 389 | |
| 390 | structure->fields->pdata[index] = ret_field; |
| 391 | end: |
| 392 | bt_ctf_field_get(ret_field); |
| 393 | error: |
| 394 | if (field_type) { |
| 395 | bt_ctf_field_type_put(field_type); |
| 396 | } |
| 397 | return ret_field; |
| 398 | } |
| 399 | |
| 400 | BT_HIDDEN |
| 401 | int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, |
| 402 | const char *name, struct bt_ctf_field *value) |
| 403 | { |
| 404 | int ret = 0; |
| 405 | GQuark field_quark; |
| 406 | struct bt_ctf_field_structure *structure; |
| 407 | struct bt_ctf_field_type *expected_field_type = NULL; |
| 408 | size_t index; |
| 409 | |
| 410 | if (!field || !name || !value || |
| 411 | bt_ctf_field_type_get_type_id(field->type) != |
| 412 | CTF_TYPE_STRUCT) { |
| 413 | ret = -1; |
| 414 | goto end; |
| 415 | } |
| 416 | |
| 417 | field_quark = g_quark_from_string(name); |
| 418 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 419 | expected_field_type = |
| 420 | bt_ctf_field_type_structure_get_field_type_by_name(field->type, |
| 421 | name); |
| 422 | if (expected_field_type != value->type) { |
| 423 | ret = -1; |
| 424 | goto end; |
| 425 | } |
| 426 | |
| 427 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 428 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) { |
| 429 | goto end; |
| 430 | } |
| 431 | |
| 432 | if (structure->fields->pdata[index]) { |
| 433 | bt_ctf_field_put(structure->fields->pdata[index]); |
| 434 | } |
| 435 | |
| 436 | structure->fields->pdata[index] = value; |
| 437 | bt_ctf_field_get(value); |
| 438 | end: |
| 439 | if (expected_field_type) { |
| 440 | bt_ctf_field_type_put(expected_field_type); |
| 441 | } |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field, |
| 446 | uint64_t index) |
| 447 | { |
| 448 | struct bt_ctf_field *new_field = NULL; |
| 449 | struct bt_ctf_field_type *field_type = NULL; |
| 450 | struct bt_ctf_field_array *array; |
| 451 | |
| 452 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 453 | CTF_TYPE_ARRAY) { |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 458 | if (index >= array->elements->len) { |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | field_type = bt_ctf_field_type_array_get_element_type(field->type); |
| 463 | if (array->elements->pdata[(size_t)index]) { |
| 464 | new_field = array->elements->pdata[(size_t)index]; |
| 465 | goto end; |
| 466 | } |
| 467 | |
| 468 | new_field = bt_ctf_field_create(field_type); |
| 469 | bt_ctf_field_get(new_field); |
| 470 | array->elements->pdata[(size_t)index] = new_field; |
| 471 | end: |
| 472 | if (field_type) { |
| 473 | bt_ctf_field_type_put(field_type); |
| 474 | } |
| 475 | return new_field; |
| 476 | } |
| 477 | |
| 478 | struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field, |
| 479 | uint64_t index) |
| 480 | { |
| 481 | struct bt_ctf_field *new_field = NULL; |
| 482 | struct bt_ctf_field_type *field_type = NULL; |
| 483 | struct bt_ctf_field_sequence *sequence; |
| 484 | |
| 485 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 486 | CTF_TYPE_SEQUENCE) { |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 491 | if (!sequence->elements || sequence->elements->len <= index) { |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | field_type = bt_ctf_field_type_sequence_get_element_type(field->type); |
| 496 | if (sequence->elements->pdata[(size_t)index]) { |
| 497 | new_field = sequence->elements->pdata[(size_t)index]; |
| 498 | goto end; |
| 499 | } |
| 500 | |
| 501 | new_field = bt_ctf_field_create(field_type); |
| 502 | bt_ctf_field_get(new_field); |
| 503 | sequence->elements->pdata[(size_t)index] = new_field; |
| 504 | end: |
| 505 | if (field_type) { |
| 506 | bt_ctf_field_type_put(field_type); |
| 507 | } |
| 508 | return new_field; |
| 509 | } |
| 510 | |
| 511 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, |
| 512 | struct bt_ctf_field *tag_field) |
| 513 | { |
| 514 | struct bt_ctf_field *new_field = NULL; |
| 515 | struct bt_ctf_field_variant *variant; |
| 516 | struct bt_ctf_field_type_variant *variant_type; |
| 517 | struct bt_ctf_field_type *field_type; |
| 518 | struct bt_ctf_field *tag_enum = NULL; |
| 519 | struct bt_ctf_field_integer *tag_enum_integer; |
| 520 | int64_t tag_enum_value; |
| 521 | |
| 522 | if (!field || !tag_field || |
| 523 | bt_ctf_field_type_get_type_id(field->type) != |
| 524 | CTF_TYPE_VARIANT || |
| 525 | bt_ctf_field_type_get_type_id(tag_field->type) != |
| 526 | CTF_TYPE_ENUM) { |
| 527 | goto end; |
| 528 | } |
| 529 | |
| 530 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 531 | variant_type = container_of(field->type, |
| 532 | struct bt_ctf_field_type_variant, parent); |
| 533 | tag_enum = bt_ctf_field_enumeration_get_container(tag_field); |
| 534 | if (!tag_enum) { |
| 535 | goto end; |
| 536 | } |
| 537 | |
| 538 | tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer, |
| 539 | parent); |
| 540 | |
| 541 | if (!bt_ctf_field_validate(variant->tag)) { |
| 542 | goto end; |
| 543 | } |
| 544 | |
| 545 | tag_enum_value = tag_enum_integer->definition.value._signed; |
| 546 | field_type = bt_ctf_field_type_variant_get_field_type_signed( |
| 547 | variant_type, tag_enum_value); |
| 548 | if (!field_type) { |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | new_field = bt_ctf_field_create(field_type); |
| 553 | if (!new_field) { |
| 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | bt_ctf_field_put(variant->tag); |
| 558 | bt_ctf_field_put(variant->payload); |
| 559 | bt_ctf_field_get(new_field); |
| 560 | bt_ctf_field_get(tag_field); |
| 561 | variant->tag = tag_field; |
| 562 | variant->payload = new_field; |
| 563 | end: |
| 564 | bt_ctf_field_put(tag_enum); |
| 565 | return new_field; |
| 566 | } |
| 567 | |
| 568 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( |
| 569 | struct bt_ctf_field *field) |
| 570 | { |
| 571 | struct bt_ctf_field *container = NULL; |
| 572 | struct bt_ctf_field_enumeration *enumeration; |
| 573 | |
| 574 | if (!field || bt_ctf_field_type_get_type_id(field->type) != |
| 575 | CTF_TYPE_ENUM) { |
| 576 | goto end; |
| 577 | } |
| 578 | |
| 579 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 580 | parent); |
| 581 | if (!enumeration->payload) { |
| 582 | struct bt_ctf_field_type_enumeration *enumeration_type = |
| 583 | container_of(field->type, |
| 584 | struct bt_ctf_field_type_enumeration, parent); |
| 585 | enumeration->payload = |
| 586 | bt_ctf_field_create(enumeration_type->container); |
| 587 | } |
| 588 | |
| 589 | container = enumeration->payload; |
| 590 | bt_ctf_field_get(container); |
| 591 | end: |
| 592 | return container; |
| 593 | } |
| 594 | |
| 595 | const char *bt_ctf_field_enumeration_get_mapping_name( |
| 596 | struct bt_ctf_field *field) |
| 597 | { |
| 598 | int ret; |
| 599 | const char *name = NULL; |
| 600 | struct bt_ctf_field *container = NULL; |
| 601 | struct bt_ctf_field_type *container_type = NULL; |
| 602 | struct bt_ctf_field_type_integer *integer_type = NULL; |
| 603 | struct bt_ctf_field_type_enumeration *enumeration_type = NULL; |
| 604 | |
| 605 | container = bt_ctf_field_enumeration_get_container(field); |
| 606 | if (!container) { |
| 607 | goto end; |
| 608 | } |
| 609 | |
| 610 | container_type = bt_ctf_field_get_type(container); |
| 611 | if (!container_type) { |
| 612 | goto error_put_container; |
| 613 | } |
| 614 | |
| 615 | integer_type = container_of(container_type, |
| 616 | struct bt_ctf_field_type_integer, parent); |
| 617 | enumeration_type = container_of(field->type, |
| 618 | struct bt_ctf_field_type_enumeration, parent); |
| 619 | |
| 620 | if (!integer_type->declaration.signedness) { |
| 621 | uint64_t value; |
| 622 | ret = bt_ctf_field_unsigned_integer_get_value(container, |
| 623 | &value); |
| 624 | if (ret) { |
| 625 | goto error_put_container_type; |
| 626 | } |
| 627 | |
| 628 | name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned( |
| 629 | enumeration_type, value); |
| 630 | } else { |
| 631 | int64_t value; |
| 632 | ret = bt_ctf_field_signed_integer_get_value(container, |
| 633 | &value); |
| 634 | if (ret) { |
| 635 | goto error_put_container_type; |
| 636 | } |
| 637 | |
| 638 | name = bt_ctf_field_type_enumeration_get_mapping_name_signed( |
| 639 | enumeration_type, value); |
| 640 | } |
| 641 | |
| 642 | error_put_container_type: |
| 643 | bt_ctf_field_type_put(container_type); |
| 644 | error_put_container: |
| 645 | bt_ctf_field_put(container); |
| 646 | end: |
| 647 | return name; |
| 648 | } |
| 649 | |
| 650 | int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field, |
| 651 | int64_t *value) |
| 652 | { |
| 653 | int ret = 0; |
| 654 | struct bt_ctf_field_integer *integer; |
| 655 | struct bt_ctf_field_type_integer *integer_type; |
| 656 | |
| 657 | if (!field || !value || !field->payload_set || |
| 658 | bt_ctf_field_type_get_type_id(field->type) != |
| 659 | CTF_TYPE_INTEGER) { |
| 660 | ret = -1; |
| 661 | goto end; |
| 662 | } |
| 663 | |
| 664 | integer_type = container_of(field->type, |
| 665 | struct bt_ctf_field_type_integer, parent); |
| 666 | if (!integer_type->declaration.signedness) { |
| 667 | ret = -1; |
| 668 | goto end; |
| 669 | } |
| 670 | |
| 671 | integer = container_of(field, |
| 672 | struct bt_ctf_field_integer, parent); |
| 673 | *value = integer->definition.value._signed; |
| 674 | end: |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field, |
| 679 | int64_t value) |
| 680 | { |
| 681 | int ret = 0; |
| 682 | struct bt_ctf_field_integer *integer; |
| 683 | struct bt_ctf_field_type_integer *integer_type; |
| 684 | unsigned int size; |
| 685 | int64_t min_value, max_value; |
| 686 | |
| 687 | if (!field || |
| 688 | bt_ctf_field_type_get_type_id(field->type) != |
| 689 | CTF_TYPE_INTEGER) { |
| 690 | ret = -1; |
| 691 | goto end; |
| 692 | } |
| 693 | |
| 694 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 695 | integer_type = container_of(field->type, |
| 696 | struct bt_ctf_field_type_integer, parent); |
| 697 | if (!integer_type->declaration.signedness) { |
| 698 | ret = -1; |
| 699 | goto end; |
| 700 | } |
| 701 | |
| 702 | size = integer_type->declaration.len; |
| 703 | min_value = -((int64_t)1 << (size - 1)); |
| 704 | max_value = ((int64_t)1 << (size - 1)) - 1; |
| 705 | if (value < min_value || value > max_value) { |
| 706 | ret = -1; |
| 707 | goto end; |
| 708 | } |
| 709 | |
| 710 | integer->definition.value._signed = value; |
| 711 | integer->parent.payload_set = 1; |
| 712 | end: |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field, |
| 717 | uint64_t *value) |
| 718 | { |
| 719 | int ret = 0; |
| 720 | struct bt_ctf_field_integer *integer; |
| 721 | struct bt_ctf_field_type_integer *integer_type; |
| 722 | |
| 723 | if (!field || !value || !field->payload_set || |
| 724 | bt_ctf_field_type_get_type_id(field->type) != |
| 725 | CTF_TYPE_INTEGER) { |
| 726 | ret = -1; |
| 727 | goto end; |
| 728 | } |
| 729 | |
| 730 | integer_type = container_of(field->type, |
| 731 | struct bt_ctf_field_type_integer, parent); |
| 732 | if (integer_type->declaration.signedness) { |
| 733 | ret = -1; |
| 734 | goto end; |
| 735 | } |
| 736 | |
| 737 | integer = container_of(field, |
| 738 | struct bt_ctf_field_integer, parent); |
| 739 | *value = integer->definition.value._unsigned; |
| 740 | end: |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field, |
| 745 | uint64_t value) |
| 746 | { |
| 747 | int ret = 0; |
| 748 | struct bt_ctf_field_integer *integer; |
| 749 | struct bt_ctf_field_type_integer *integer_type; |
| 750 | unsigned int size; |
| 751 | uint64_t max_value; |
| 752 | |
| 753 | if (!field || |
| 754 | bt_ctf_field_type_get_type_id(field->type) != |
| 755 | CTF_TYPE_INTEGER) { |
| 756 | ret = -1; |
| 757 | goto end; |
| 758 | } |
| 759 | |
| 760 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 761 | integer_type = container_of(field->type, |
| 762 | struct bt_ctf_field_type_integer, parent); |
| 763 | if (integer_type->declaration.signedness) { |
| 764 | ret = -1; |
| 765 | goto end; |
| 766 | } |
| 767 | |
| 768 | size = integer_type->declaration.len; |
| 769 | max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1; |
| 770 | if (value > max_value) { |
| 771 | ret = -1; |
| 772 | goto end; |
| 773 | } |
| 774 | |
| 775 | integer->definition.value._unsigned = value; |
| 776 | integer->parent.payload_set = 1; |
| 777 | end: |
| 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, |
| 782 | double *value) |
| 783 | { |
| 784 | int ret = 0; |
| 785 | struct bt_ctf_field_floating_point *floating_point; |
| 786 | |
| 787 | if (!field || !value || !field->payload_set || |
| 788 | bt_ctf_field_type_get_type_id(field->type) != |
| 789 | CTF_TYPE_FLOAT) { |
| 790 | ret = -1; |
| 791 | goto end; |
| 792 | } |
| 793 | |
| 794 | floating_point = container_of(field, |
| 795 | struct bt_ctf_field_floating_point, parent); |
| 796 | *value = floating_point->definition.value; |
| 797 | end: |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, |
| 802 | double value) |
| 803 | { |
| 804 | int ret = 0; |
| 805 | struct bt_ctf_field_floating_point *floating_point; |
| 806 | |
| 807 | if (!field || |
| 808 | bt_ctf_field_type_get_type_id(field->type) != |
| 809 | CTF_TYPE_FLOAT) { |
| 810 | ret = -1; |
| 811 | goto end; |
| 812 | } |
| 813 | floating_point = container_of(field, struct bt_ctf_field_floating_point, |
| 814 | parent); |
| 815 | floating_point->definition.value = value; |
| 816 | floating_point->parent.payload_set = 1; |
| 817 | end: |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) |
| 822 | { |
| 823 | const char *ret = NULL; |
| 824 | struct bt_ctf_field_string *string; |
| 825 | |
| 826 | if (!field || !field->payload_set || |
| 827 | bt_ctf_field_type_get_type_id(field->type) != |
| 828 | CTF_TYPE_STRING) { |
| 829 | goto end; |
| 830 | } |
| 831 | |
| 832 | string = container_of(field, |
| 833 | struct bt_ctf_field_string, parent); |
| 834 | ret = string->payload->str; |
| 835 | end: |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, |
| 840 | const char *value) |
| 841 | { |
| 842 | int ret = 0; |
| 843 | struct bt_ctf_field_string *string; |
| 844 | |
| 845 | if (!field || !value || |
| 846 | bt_ctf_field_type_get_type_id(field->type) != |
| 847 | CTF_TYPE_STRING) { |
| 848 | ret = -1; |
| 849 | goto end; |
| 850 | } |
| 851 | |
| 852 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 853 | if (string->payload) { |
| 854 | g_string_free(string->payload, TRUE); |
| 855 | } |
| 856 | |
| 857 | string->payload = g_string_new(value); |
| 858 | string->parent.payload_set = 1; |
| 859 | end: |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | BT_HIDDEN |
| 864 | int bt_ctf_field_validate(struct bt_ctf_field *field) |
| 865 | { |
| 866 | int ret = 0; |
| 867 | enum ctf_type_id type_id; |
| 868 | |
| 869 | if (!field) { |
| 870 | ret = -1; |
| 871 | goto end; |
| 872 | } |
| 873 | |
| 874 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 875 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 876 | ret = -1; |
| 877 | goto end; |
| 878 | } |
| 879 | |
| 880 | ret = field_validate_funcs[type_id](field); |
| 881 | end: |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | BT_HIDDEN |
| 886 | int bt_ctf_field_serialize(struct bt_ctf_field *field, |
| 887 | struct ctf_stream_pos *pos) |
| 888 | { |
| 889 | int ret = 0; |
| 890 | enum ctf_type_id type_id; |
| 891 | |
| 892 | if (!field || !pos) { |
| 893 | ret = -1; |
| 894 | goto end; |
| 895 | } |
| 896 | |
| 897 | type_id = bt_ctf_field_type_get_type_id(field->type); |
| 898 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { |
| 899 | ret = -1; |
| 900 | goto end; |
| 901 | } |
| 902 | |
| 903 | ret = field_serialize_funcs[type_id](field, pos); |
| 904 | end: |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | static |
| 909 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) |
| 910 | { |
| 911 | struct bt_ctf_field_type_integer *integer_type = container_of(type, |
| 912 | struct bt_ctf_field_type_integer, parent); |
| 913 | struct bt_ctf_field_integer *integer = g_new0( |
| 914 | struct bt_ctf_field_integer, 1); |
| 915 | |
| 916 | if (integer) { |
| 917 | integer->definition.declaration = &integer_type->declaration; |
| 918 | } |
| 919 | |
| 920 | return integer ? &integer->parent : NULL; |
| 921 | } |
| 922 | |
| 923 | static |
| 924 | struct bt_ctf_field *bt_ctf_field_enumeration_create( |
| 925 | struct bt_ctf_field_type *type) |
| 926 | { |
| 927 | struct bt_ctf_field_enumeration *enumeration = g_new0( |
| 928 | struct bt_ctf_field_enumeration, 1); |
| 929 | |
| 930 | return enumeration ? &enumeration->parent : NULL; |
| 931 | } |
| 932 | |
| 933 | static |
| 934 | struct bt_ctf_field *bt_ctf_field_floating_point_create( |
| 935 | struct bt_ctf_field_type *type) |
| 936 | { |
| 937 | struct bt_ctf_field_floating_point *floating_point; |
| 938 | struct bt_ctf_field_type_floating_point *floating_point_type; |
| 939 | |
| 940 | floating_point = g_new0(struct bt_ctf_field_floating_point, 1); |
| 941 | if (!floating_point) { |
| 942 | goto end; |
| 943 | } |
| 944 | |
| 945 | floating_point_type = container_of(type, |
| 946 | struct bt_ctf_field_type_floating_point, parent); |
| 947 | floating_point->definition.declaration = container_of( |
| 948 | type->declaration, struct declaration_float, p); |
| 949 | |
| 950 | |
| 951 | floating_point->definition.sign = &floating_point->sign; |
| 952 | floating_point->sign.declaration = &floating_point_type->sign; |
| 953 | floating_point->definition.sign->p.declaration = |
| 954 | &floating_point_type->sign.p; |
| 955 | |
| 956 | floating_point->definition.mantissa = &floating_point->mantissa; |
| 957 | floating_point->mantissa.declaration = &floating_point_type->mantissa; |
| 958 | floating_point->definition.mantissa->p.declaration = |
| 959 | &floating_point_type->mantissa.p; |
| 960 | |
| 961 | floating_point->definition.exp = &floating_point->exp; |
| 962 | floating_point->exp.declaration = &floating_point_type->exp; |
| 963 | floating_point->definition.exp->p.declaration = |
| 964 | &floating_point_type->exp.p; |
| 965 | |
| 966 | end: |
| 967 | return floating_point ? &floating_point->parent : NULL; |
| 968 | } |
| 969 | |
| 970 | static |
| 971 | struct bt_ctf_field *bt_ctf_field_structure_create( |
| 972 | struct bt_ctf_field_type *type) |
| 973 | { |
| 974 | struct bt_ctf_field_type_structure *structure_type = container_of(type, |
| 975 | struct bt_ctf_field_type_structure, parent); |
| 976 | struct bt_ctf_field_structure *structure = g_new0( |
| 977 | struct bt_ctf_field_structure, 1); |
| 978 | struct bt_ctf_field *field = NULL; |
| 979 | |
| 980 | if (!structure || !structure_type->fields->len) { |
| 981 | goto end; |
| 982 | } |
| 983 | |
| 984 | structure->field_name_to_index = structure_type->field_name_to_index; |
| 985 | structure->fields = g_ptr_array_new_with_free_func( |
| 986 | (GDestroyNotify)bt_ctf_field_put); |
| 987 | g_ptr_array_set_size(structure->fields, |
| 988 | g_hash_table_size(structure->field_name_to_index)); |
| 989 | field = &structure->parent; |
| 990 | end: |
| 991 | return field; |
| 992 | } |
| 993 | |
| 994 | static |
| 995 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) |
| 996 | { |
| 997 | struct bt_ctf_field_variant *variant = g_new0( |
| 998 | struct bt_ctf_field_variant, 1); |
| 999 | return variant ? &variant->parent : NULL; |
| 1000 | } |
| 1001 | |
| 1002 | static |
| 1003 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) |
| 1004 | { |
| 1005 | struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1); |
| 1006 | struct bt_ctf_field_type_array *array_type; |
| 1007 | unsigned int array_length; |
| 1008 | |
| 1009 | if (!array || !type) { |
| 1010 | goto error; |
| 1011 | } |
| 1012 | |
| 1013 | array_type = container_of(type, struct bt_ctf_field_type_array, parent); |
| 1014 | array_length = array_type->length; |
| 1015 | array->elements = g_ptr_array_sized_new(array_length); |
| 1016 | if (!array->elements) { |
| 1017 | goto error; |
| 1018 | } |
| 1019 | |
| 1020 | g_ptr_array_set_free_func(array->elements, |
| 1021 | (GDestroyNotify)bt_ctf_field_put); |
| 1022 | g_ptr_array_set_size(array->elements, array_length); |
| 1023 | return &array->parent; |
| 1024 | error: |
| 1025 | g_free(array); |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | |
| 1029 | static |
| 1030 | struct bt_ctf_field *bt_ctf_field_sequence_create( |
| 1031 | struct bt_ctf_field_type *type) |
| 1032 | { |
| 1033 | struct bt_ctf_field_sequence *sequence = g_new0( |
| 1034 | struct bt_ctf_field_sequence, 1); |
| 1035 | return sequence ? &sequence->parent : NULL; |
| 1036 | } |
| 1037 | |
| 1038 | static |
| 1039 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) |
| 1040 | { |
| 1041 | struct bt_ctf_field_string *string = g_new0( |
| 1042 | struct bt_ctf_field_string, 1); |
| 1043 | return string ? &string->parent : NULL; |
| 1044 | } |
| 1045 | |
| 1046 | static |
| 1047 | void bt_ctf_field_destroy(struct bt_ctf_ref *ref) |
| 1048 | { |
| 1049 | struct bt_ctf_field *field; |
| 1050 | struct bt_ctf_field_type *type; |
| 1051 | enum ctf_type_id type_id; |
| 1052 | |
| 1053 | if (!ref) { |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | field = container_of(ref, struct bt_ctf_field, ref_count); |
| 1058 | type = field->type; |
| 1059 | type_id = bt_ctf_field_type_get_type_id(type); |
| 1060 | if (type_id <= CTF_TYPE_UNKNOWN || |
| 1061 | type_id >= NR_CTF_TYPES) { |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | field_destroy_funcs[type_id](field); |
| 1066 | if (type) { |
| 1067 | bt_ctf_field_type_put(type); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | static |
| 1072 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) |
| 1073 | { |
| 1074 | struct bt_ctf_field_integer *integer; |
| 1075 | |
| 1076 | if (!field) { |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | integer = container_of(field, struct bt_ctf_field_integer, parent); |
| 1081 | g_free(integer); |
| 1082 | } |
| 1083 | |
| 1084 | static |
| 1085 | void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field) |
| 1086 | { |
| 1087 | struct bt_ctf_field_enumeration *enumeration; |
| 1088 | |
| 1089 | if (!field) { |
| 1090 | return; |
| 1091 | } |
| 1092 | |
| 1093 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 1094 | parent); |
| 1095 | bt_ctf_field_put(enumeration->payload); |
| 1096 | g_free(enumeration); |
| 1097 | } |
| 1098 | |
| 1099 | static |
| 1100 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) |
| 1101 | { |
| 1102 | struct bt_ctf_field_floating_point *floating_point; |
| 1103 | |
| 1104 | if (!field) { |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | floating_point = container_of(field, struct bt_ctf_field_floating_point, |
| 1109 | parent); |
| 1110 | g_free(floating_point); |
| 1111 | } |
| 1112 | |
| 1113 | static |
| 1114 | void bt_ctf_field_structure_destroy(struct bt_ctf_field *field) |
| 1115 | { |
| 1116 | struct bt_ctf_field_structure *structure; |
| 1117 | |
| 1118 | if (!field) { |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 1123 | g_ptr_array_free(structure->fields, TRUE); |
| 1124 | g_free(structure); |
| 1125 | } |
| 1126 | |
| 1127 | static |
| 1128 | void bt_ctf_field_variant_destroy(struct bt_ctf_field *field) |
| 1129 | { |
| 1130 | struct bt_ctf_field_variant *variant; |
| 1131 | |
| 1132 | if (!field) { |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 1137 | bt_ctf_field_put(variant->tag); |
| 1138 | bt_ctf_field_put(variant->payload); |
| 1139 | g_free(variant); |
| 1140 | } |
| 1141 | |
| 1142 | static |
| 1143 | void bt_ctf_field_array_destroy(struct bt_ctf_field *field) |
| 1144 | { |
| 1145 | struct bt_ctf_field_array *array; |
| 1146 | |
| 1147 | if (!field) { |
| 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 1152 | g_ptr_array_free(array->elements, TRUE); |
| 1153 | g_free(array); |
| 1154 | } |
| 1155 | |
| 1156 | static |
| 1157 | void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field) |
| 1158 | { |
| 1159 | struct bt_ctf_field_sequence *sequence; |
| 1160 | |
| 1161 | if (!field) { |
| 1162 | return; |
| 1163 | } |
| 1164 | |
| 1165 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 1166 | g_ptr_array_free(sequence->elements, TRUE); |
| 1167 | bt_ctf_field_put(sequence->length); |
| 1168 | g_free(sequence); |
| 1169 | } |
| 1170 | |
| 1171 | static |
| 1172 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) |
| 1173 | { |
| 1174 | struct bt_ctf_field_string *string; |
| 1175 | if (!field) { |
| 1176 | return; |
| 1177 | } |
| 1178 | |
| 1179 | string = container_of(field, struct bt_ctf_field_string, parent); |
| 1180 | g_string_free(string->payload, TRUE); |
| 1181 | g_free(string); |
| 1182 | } |
| 1183 | |
| 1184 | static |
| 1185 | int bt_ctf_field_generic_validate(struct bt_ctf_field *field) |
| 1186 | { |
| 1187 | return (field && field->payload_set) ? 0 : -1; |
| 1188 | } |
| 1189 | |
| 1190 | static |
| 1191 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) |
| 1192 | { |
| 1193 | int ret; |
| 1194 | struct bt_ctf_field_enumeration *enumeration; |
| 1195 | |
| 1196 | if (!field) { |
| 1197 | ret = -1; |
| 1198 | goto end; |
| 1199 | } |
| 1200 | |
| 1201 | enumeration = container_of(field, struct bt_ctf_field_enumeration, |
| 1202 | parent); |
| 1203 | if (!enumeration->payload) { |
| 1204 | ret = -1; |
| 1205 | goto end; |
| 1206 | } |
| 1207 | |
| 1208 | ret = bt_ctf_field_validate(enumeration->payload); |
| 1209 | end: |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static |
| 1214 | int bt_ctf_field_structure_validate(struct bt_ctf_field *field) |
| 1215 | { |
| 1216 | size_t i; |
| 1217 | int ret = 0; |
| 1218 | struct bt_ctf_field_structure *structure; |
| 1219 | |
| 1220 | if (!field) { |
| 1221 | ret = -1; |
| 1222 | goto end; |
| 1223 | } |
| 1224 | |
| 1225 | structure = container_of(field, struct bt_ctf_field_structure, parent); |
| 1226 | for (i = 0; i < structure->fields->len; i++) { |
| 1227 | ret = bt_ctf_field_validate(structure->fields->pdata[i]); |
| 1228 | if (ret) { |
| 1229 | goto end; |
| 1230 | } |
| 1231 | } |
| 1232 | end: |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
| 1236 | static |
| 1237 | int bt_ctf_field_variant_validate(struct bt_ctf_field *field) |
| 1238 | { |
| 1239 | int ret = 0; |
| 1240 | struct bt_ctf_field_variant *variant; |
| 1241 | |
| 1242 | if (!field) { |
| 1243 | ret = -1; |
| 1244 | goto end; |
| 1245 | } |
| 1246 | |
| 1247 | variant = container_of(field, struct bt_ctf_field_variant, parent); |
| 1248 | ret = bt_ctf_field_validate(variant->payload); |
| 1249 | end: |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
| 1253 | static |
| 1254 | int bt_ctf_field_array_validate(struct bt_ctf_field *field) |
| 1255 | { |
| 1256 | size_t i; |
| 1257 | int ret = 0; |
| 1258 | struct bt_ctf_field_array *array; |
| 1259 | |
| 1260 | if (!field) { |
| 1261 | ret = -1; |
| 1262 | goto end; |
| 1263 | } |
| 1264 | |
| 1265 | array = container_of(field, struct bt_ctf_field_array, parent); |
| 1266 | for (i = 0; i < array->elements->len; i++) { |
| 1267 | ret = bt_ctf_field_validate(array->elements->pdata[i]); |
| 1268 | if (ret) { |
| 1269 | goto end; |
| 1270 | } |
| 1271 | } |
| 1272 | end: |
| 1273 | return ret; |
| 1274 | } |
| 1275 | |
| 1276 | static |
| 1277 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) |
| 1278 | { |
| 1279 | size_t i; |
| 1280 | int ret = 0; |
| 1281 | struct bt_ctf_field_sequence *sequence; |
| 1282 | |
| 1283 | if (!field) { |
| 1284 | ret = -1; |
| 1285 | goto end; |
| 1286 | } |
| 1287 | |
| 1288 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); |
| 1289 | for (i = 0; i < sequence->elements->len; i++) { |
| 1290 | ret = bt_ctf_field_validate(sequence->elements->pdata[i]); |
| 1291 | if (ret) { |
| 1292 | goto end; |
| 1293 | } |
| 1294 | } |
| 1295 | end: |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | static |
| 1300 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, |
| 1301 | struct ctf_stream_pos *pos) |
| 1302 | { |
| 1303 | int ret = 0; |
| 1304 | struct bt_ctf_field_integer *integer = container_of(field, |
| 1305 | struct bt_ctf_field_integer, parent); |
| 1306 | |
| 1307 | retry: |
| 1308 | ret = ctf_integer_write(&pos->parent, &integer->definition.p); |
| 1309 | if (ret == -EFAULT) { |
| 1310 | /* |
| 1311 | * The field is too large to fit in the current packet's |
| 1312 | * remaining space. Bump the packet size and retry. |
| 1313 | */ |
| 1314 | ret = increase_packet_size(pos); |
| 1315 | if (ret) { |
| 1316 | goto end; |
| 1317 | } |
| 1318 | goto retry; |
| 1319 | } |
| 1320 | end: |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
| 1324 | static |
| 1325 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, |
| 1326 | struct ctf_stream_pos *pos) |
| 1327 | { |
| 1328 | struct bt_ctf_field_enumeration *enumeration = container_of( |
| 1329 | field, struct bt_ctf_field_enumeration, parent); |
| 1330 | |
| 1331 | return bt_ctf_field_serialize(enumeration->payload, pos); |
| 1332 | } |
| 1333 | |
| 1334 | static |
| 1335 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, |
| 1336 | struct ctf_stream_pos *pos) |
| 1337 | { |
| 1338 | int ret = 0; |
| 1339 | struct bt_ctf_field_floating_point *floating_point = container_of(field, |
| 1340 | struct bt_ctf_field_floating_point, parent); |
| 1341 | |
| 1342 | retry: |
| 1343 | ret = ctf_float_write(&pos->parent, &floating_point->definition.p); |
| 1344 | if (ret == -EFAULT) { |
| 1345 | /* |
| 1346 | * The field is too large to fit in the current packet's |
| 1347 | * remaining space. Bump the packet size and retry. |
| 1348 | */ |
| 1349 | ret = increase_packet_size(pos); |
| 1350 | if (ret) { |
| 1351 | goto end; |
| 1352 | } |
| 1353 | goto retry; |
| 1354 | } |
| 1355 | end: |
| 1356 | return ret; |
| 1357 | } |
| 1358 | |
| 1359 | static |
| 1360 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, |
| 1361 | struct ctf_stream_pos *pos) |
| 1362 | { |
| 1363 | size_t i; |
| 1364 | int ret = 0; |
| 1365 | struct bt_ctf_field_structure *structure = container_of( |
| 1366 | field, struct bt_ctf_field_structure, parent); |
| 1367 | |
| 1368 | while (!ctf_pos_access_ok(pos, |
| 1369 | offset_align(pos->offset, |
| 1370 | field->type->declaration->alignment))) { |
| 1371 | ret = increase_packet_size(pos); |
| 1372 | if (ret) { |
| 1373 | goto end; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (!ctf_align_pos(pos, field->type->declaration->alignment)) { |
| 1378 | ret = -1; |
| 1379 | goto end; |
| 1380 | } |
| 1381 | |
| 1382 | for (i = 0; i < structure->fields->len; i++) { |
| 1383 | struct bt_ctf_field *field = g_ptr_array_index( |
| 1384 | structure->fields, i); |
| 1385 | |
| 1386 | ret = bt_ctf_field_serialize(field, pos); |
| 1387 | if (ret) { |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | end: |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | static |
| 1396 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, |
| 1397 | struct ctf_stream_pos *pos) |
| 1398 | { |
| 1399 | struct bt_ctf_field_variant *variant = container_of( |
| 1400 | field, struct bt_ctf_field_variant, parent); |
| 1401 | |
| 1402 | return bt_ctf_field_serialize(variant->payload, pos); |
| 1403 | } |
| 1404 | |
| 1405 | static |
| 1406 | int bt_ctf_field_array_serialize(struct bt_ctf_field *field, |
| 1407 | struct ctf_stream_pos *pos) |
| 1408 | { |
| 1409 | size_t i; |
| 1410 | int ret = 0; |
| 1411 | struct bt_ctf_field_array *array = container_of( |
| 1412 | field, struct bt_ctf_field_array, parent); |
| 1413 | |
| 1414 | for (i = 0; i < array->elements->len; i++) { |
| 1415 | ret = bt_ctf_field_serialize( |
| 1416 | g_ptr_array_index(array->elements, i), pos); |
| 1417 | if (ret) { |
| 1418 | goto end; |
| 1419 | } |
| 1420 | } |
| 1421 | end: |
| 1422 | return ret; |
| 1423 | } |
| 1424 | |
| 1425 | static |
| 1426 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, |
| 1427 | struct ctf_stream_pos *pos) |
| 1428 | { |
| 1429 | size_t i; |
| 1430 | int ret = 0; |
| 1431 | struct bt_ctf_field_sequence *sequence = container_of( |
| 1432 | field, struct bt_ctf_field_sequence, parent); |
| 1433 | |
| 1434 | for (i = 0; i < sequence->elements->len; i++) { |
| 1435 | ret = bt_ctf_field_serialize( |
| 1436 | g_ptr_array_index(sequence->elements, i), pos); |
| 1437 | if (ret) { |
| 1438 | goto end; |
| 1439 | } |
| 1440 | } |
| 1441 | end: |
| 1442 | return ret; |
| 1443 | } |
| 1444 | |
| 1445 | static |
| 1446 | int bt_ctf_field_string_serialize(struct bt_ctf_field *field, |
| 1447 | struct ctf_stream_pos *pos) |
| 1448 | { |
| 1449 | size_t i; |
| 1450 | int ret = 0; |
| 1451 | struct bt_ctf_field_string *string = container_of(field, |
| 1452 | struct bt_ctf_field_string, parent); |
| 1453 | struct bt_ctf_field_type *character_type = |
| 1454 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 1455 | struct bt_ctf_field *character = bt_ctf_field_create(character_type); |
| 1456 | |
| 1457 | for (i = 0; i < string->payload->len + 1; i++) { |
| 1458 | ret = bt_ctf_field_unsigned_integer_set_value(character, |
| 1459 | (uint64_t) string->payload->str[i]); |
| 1460 | if (ret) { |
| 1461 | goto end; |
| 1462 | } |
| 1463 | |
| 1464 | ret = bt_ctf_field_integer_serialize(character, pos); |
| 1465 | if (ret) { |
| 1466 | goto end; |
| 1467 | } |
| 1468 | } |
| 1469 | end: |
| 1470 | bt_ctf_field_put(character); |
| 1471 | bt_ctf_field_type_put(character_type); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | static |
| 1476 | int increase_packet_size(struct ctf_stream_pos *pos) |
| 1477 | { |
| 1478 | int ret; |
| 1479 | |
| 1480 | assert(pos); |
| 1481 | ret = munmap_align(pos->base_mma); |
| 1482 | if (ret) { |
| 1483 | goto end; |
| 1484 | } |
| 1485 | |
| 1486 | pos->packet_size += PACKET_LEN_INCREMENT; |
| 1487 | ret = posix_fallocate(pos->fd, pos->mmap_offset, |
| 1488 | pos->packet_size / CHAR_BIT); |
| 1489 | if (ret) { |
| 1490 | goto end; |
| 1491 | } |
| 1492 | |
| 1493 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
| 1494 | pos->flags, pos->fd, pos->mmap_offset); |
| 1495 | if (pos->base_mma == MAP_FAILED) { |
| 1496 | ret = -1; |
| 1497 | } |
| 1498 | end: |
| 1499 | return ret; |
| 1500 | } |