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