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