ir: add user byte order to integer/float types
[babeltrace.git] / formats / ctf / ir / trace.c
1 /*
2 * trace.c
3 *
4 * Babeltrace CTF IR - Trace
5 *
6 * Copyright 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-ir/trace-internal.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-ir/stream-internal.h>
32 #include <babeltrace/ctf-ir/stream-class-internal.h>
33 #include <babeltrace/ctf-writer/functor-internal.h>
34 #include <babeltrace/ctf-ir/event-types-internal.h>
35 #include <babeltrace/ctf-ir/attributes-internal.h>
36 #include <babeltrace/ctf-ir/visitor-internal.h>
37 #include <babeltrace/ctf-ir/utils.h>
38 #include <babeltrace/compiler.h>
39 #include <babeltrace/values.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/endian.h>
42
43 #define DEFAULT_IDENTIFIER_SIZE 128
44 #define DEFAULT_METADATA_STRING_SIZE 4096
45
46 static
47 void bt_ctf_trace_destroy(struct bt_object *obj);
48 static
49 int init_trace_packet_header(struct bt_ctf_trace *trace);
50 static
51 int bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
52
53 static
54 const unsigned int field_type_aliases_alignments[] = {
55 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
56 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
57 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
58 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
59 };
60
61 static
62 const unsigned int field_type_aliases_sizes[] = {
63 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
64 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
65 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
66 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
67 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
68 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
69 };
70
71 struct bt_ctf_trace *bt_ctf_trace_create(void)
72 {
73 struct bt_ctf_trace *trace = NULL;
74
75 trace = g_new0(struct bt_ctf_trace, 1);
76 if (!trace) {
77 goto error;
78 }
79
80 bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
81 bt_object_init(trace, bt_ctf_trace_destroy);
82 trace->clocks = g_ptr_array_new_with_free_func(
83 (GDestroyNotify) bt_put);
84 trace->streams = g_ptr_array_new_with_free_func(
85 (GDestroyNotify) bt_object_release);
86 trace->stream_classes = g_ptr_array_new_with_free_func(
87 (GDestroyNotify) bt_object_release);
88 if (!trace->clocks || !trace->stream_classes || !trace->streams) {
89 goto error;
90 }
91
92 /* Generate a trace UUID */
93 uuid_generate(trace->uuid);
94 if (init_trace_packet_header(trace)) {
95 goto error;
96 }
97
98 /* Create the environment array object */
99 trace->environment = bt_ctf_attributes_create();
100 if (!trace->environment) {
101 goto error;
102 }
103
104 return trace;
105
106 error:
107 BT_PUT(trace);
108 return trace;
109 }
110
111 void bt_ctf_trace_destroy(struct bt_object *obj)
112 {
113 struct bt_ctf_trace *trace;
114
115 trace = container_of(obj, struct bt_ctf_trace, base);
116 if (trace->environment) {
117 bt_ctf_attributes_destroy(trace->environment);
118 }
119
120 if (trace->clocks) {
121 g_ptr_array_free(trace->clocks, TRUE);
122 }
123
124 if (trace->streams) {
125 g_ptr_array_free(trace->streams, TRUE);
126 }
127
128 if (trace->stream_classes) {
129 g_ptr_array_free(trace->stream_classes, TRUE);
130 }
131
132 bt_put(trace->packet_header_type);
133 g_free(trace);
134 }
135
136 struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
137 struct bt_ctf_stream_class *stream_class)
138 {
139 int ret;
140 int stream_class_found = 0;
141 size_t i;
142 struct bt_ctf_stream *stream = NULL;
143
144 if (!trace || !stream_class) {
145 goto error;
146 }
147
148 for (i = 0; i < trace->stream_classes->len; i++) {
149 if (trace->stream_classes->pdata[i] == stream_class) {
150 stream_class_found = 1;
151 }
152 }
153
154 if (!stream_class_found) {
155 ret = bt_ctf_trace_add_stream_class(trace, stream_class);
156 if (ret) {
157 goto error;
158 }
159 }
160
161 stream = bt_ctf_stream_create(stream_class, trace);
162 if (!stream) {
163 goto error;
164 }
165
166 g_ptr_array_add(trace->streams, stream);
167 return stream;
168 error:
169 BT_PUT(stream);
170 return stream;
171 }
172
173 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
174 const char *name, struct bt_value *value)
175 {
176 int ret = 0;
177
178 if (!trace || !name || !value ||
179 bt_ctf_validate_identifier(name) ||
180 !(bt_value_is_integer(value) || bt_value_is_string(value))) {
181 ret = -1;
182 goto end;
183 }
184
185 if (strchr(name, ' ')) {
186 ret = -1;
187 goto end;
188 }
189
190 if (trace->frozen) {
191 /*
192 * New environment fields may be added to a frozen trace,
193 * but existing fields may not be changed.
194 *
195 * The object passed is frozen like all other attributes.
196 */
197 struct bt_value *attribute =
198 bt_ctf_attributes_get_field_value_by_name(
199 trace->environment, name);
200
201 if (attribute) {
202 BT_PUT(attribute);
203 ret = -1;
204 goto end;
205 }
206
207 bt_value_freeze(value);
208 }
209
210 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
211 value);
212
213 end:
214 return ret;
215 }
216
217 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
218 const char *name, const char *value)
219 {
220 int ret = 0;
221 struct bt_value *env_value_string_obj = NULL;
222
223 if (!trace || !name || !value) {
224 ret = -1;
225 goto end;
226 }
227
228 if (trace->frozen) {
229 /*
230 * New environment fields may be added to a frozen trace,
231 * but existing fields may not be changed.
232 */
233 struct bt_value *attribute =
234 bt_ctf_attributes_get_field_value_by_name(
235 trace->environment, name);
236
237 if (attribute) {
238 BT_PUT(attribute);
239 ret = -1;
240 goto end;
241 }
242 }
243
244 env_value_string_obj = bt_value_string_create_init(value);
245
246 if (!env_value_string_obj) {
247 ret = -1;
248 goto end;
249 }
250
251 if (trace->frozen) {
252 bt_value_freeze(env_value_string_obj);
253 }
254 ret = bt_ctf_trace_set_environment_field(trace, name,
255 env_value_string_obj);
256
257 end:
258 BT_PUT(env_value_string_obj);
259 return ret;
260 }
261
262 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
263 const char *name, int64_t value)
264 {
265 int ret = 0;
266 struct bt_value *env_value_integer_obj = NULL;
267
268 if (!trace || !name) {
269 ret = -1;
270 goto end;
271 }
272
273 if (trace->frozen) {
274 /*
275 * New environment fields may be added to a frozen trace,
276 * but existing fields may not be changed.
277 */
278 struct bt_value *attribute =
279 bt_ctf_attributes_get_field_value_by_name(
280 trace->environment, name);
281
282 if (attribute) {
283 BT_PUT(attribute);
284 ret = -1;
285 goto end;
286 }
287 }
288
289 env_value_integer_obj = bt_value_integer_create_init(value);
290 if (!env_value_integer_obj) {
291 ret = -1;
292 goto end;
293 }
294
295 ret = bt_ctf_trace_set_environment_field(trace, name,
296 env_value_integer_obj);
297 if (trace->frozen) {
298 bt_value_freeze(env_value_integer_obj);
299 }
300 end:
301 BT_PUT(env_value_integer_obj);
302 return ret;
303 }
304
305 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
306 {
307 int ret = 0;
308
309 if (!trace) {
310 ret = -1;
311 goto end;
312 }
313
314 ret = bt_ctf_attributes_get_count(trace->environment);
315
316 end:
317 return ret;
318 }
319
320 const char *
321 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
322 int index)
323 {
324 const char *ret = NULL;
325
326 if (!trace) {
327 goto end;
328 }
329
330 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
331
332 end:
333 return ret;
334 }
335
336 struct bt_value *bt_ctf_trace_get_environment_field_value(
337 struct bt_ctf_trace *trace, int index)
338 {
339 struct bt_value *ret = NULL;
340
341 if (!trace) {
342 goto end;
343 }
344
345 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
346
347 end:
348 return ret;
349 }
350
351 struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
352 struct bt_ctf_trace *trace, const char *name)
353 {
354 struct bt_value *ret = NULL;
355
356 if (!trace || !name) {
357 goto end;
358 }
359
360 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
361 name);
362
363 end:
364 return ret;
365 }
366
367 int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
368 struct bt_ctf_clock *clock)
369 {
370 int ret = 0;
371 struct search_query query = { .value = clock, .found = 0 };
372
373 if (!trace || !clock) {
374 ret = -1;
375 goto end;
376 }
377
378 /* Check for duplicate clocks */
379 g_ptr_array_foreach(trace->clocks, value_exists, &query);
380 if (query.found) {
381 ret = -1;
382 goto end;
383 }
384
385 bt_get(clock);
386 g_ptr_array_add(trace->clocks, clock);
387 end:
388 return ret;
389 }
390
391 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace)
392 {
393 int ret = -1;
394
395 if (!trace) {
396 goto end;
397 }
398
399 ret = trace->clocks->len;
400 end:
401 return ret;
402 }
403
404 struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace,
405 int index)
406 {
407 struct bt_ctf_clock *clock = NULL;
408
409 if (!trace || index < 0 || index >= trace->clocks->len) {
410 goto end;
411 }
412
413 clock = g_ptr_array_index(trace->clocks, index);
414 bt_get(clock);
415 end:
416 return clock;
417 }
418
419 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
420 struct bt_ctf_stream_class *stream_class)
421 {
422 int ret, i;
423 int64_t stream_id;
424
425 if (!trace || !stream_class) {
426 ret = -1;
427 goto end;
428 }
429
430 for (i = 0; i < trace->stream_classes->len; i++) {
431 if (trace->stream_classes->pdata[i] == stream_class) {
432 /* Stream already registered to the trace */
433 ret = -1;
434 goto end;
435 }
436 }
437
438 ret = bt_ctf_stream_class_resolve_types(stream_class, trace);
439 if (ret) {
440 goto end;
441 }
442
443 stream_id = bt_ctf_stream_class_get_id(stream_class);
444 if (stream_id < 0) {
445 stream_id = trace->next_stream_id++;
446
447 /* Try to assign a new stream id */
448 for (i = 0; i < trace->stream_classes->len; i++) {
449 if (stream_id == bt_ctf_stream_class_get_id(
450 trace->stream_classes->pdata[i])) {
451 /* Duplicate stream id found */
452 ret = -1;
453 goto end;
454 }
455 }
456
457 if (bt_ctf_stream_class_set_id_no_check(stream_class,
458 stream_id)) {
459 /* TODO Should retry with a different stream id */
460 ret = -1;
461 goto end;
462 }
463 }
464
465 bt_object_set_parent(stream_class, trace);
466 g_ptr_array_add(trace->stream_classes, stream_class);
467
468 /*
469 * Freeze the trace and its packet header.
470 *
471 * All field type byte orders set as "native" byte ordering can now be
472 * safely set to trace's own endianness, including the stream class'.
473 */
474 bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
475 trace->byte_order);
476 bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order);
477
478 bt_ctf_stream_class_freeze(stream_class);
479 if (!trace->frozen) {
480 ret = bt_ctf_trace_freeze(trace);
481 }
482 end:
483 if (ret) {
484 bt_object_set_parent(stream_class, NULL);
485 }
486 return ret;
487 }
488
489 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
490 {
491 int ret;
492
493 if (!trace) {
494 ret = -1;
495 goto end;
496 }
497
498 ret = trace->stream_classes->len;
499 end:
500 return ret;
501 }
502
503 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
504 struct bt_ctf_trace *trace, int index)
505 {
506 struct bt_ctf_stream_class *stream_class = NULL;
507
508 if (!trace || index < 0 || index >= trace->stream_classes->len) {
509 goto end;
510 }
511
512 stream_class = g_ptr_array_index(trace->stream_classes, index);
513 bt_get(stream_class);
514 end:
515 return stream_class;
516 }
517
518 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
519 struct bt_ctf_trace *trace, uint32_t id)
520 {
521 int i;
522 struct bt_ctf_stream_class *stream_class = NULL;
523
524 if (!trace) {
525 goto end;
526 }
527
528 for (i = 0; i < trace->stream_classes->len; ++i) {
529 struct bt_ctf_stream_class *stream_class_candidate;
530
531 stream_class_candidate =
532 g_ptr_array_index(trace->stream_classes, i);
533
534 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
535 (int64_t) id) {
536 stream_class = stream_class_candidate;
537 bt_get(stream_class);
538 goto end;
539 }
540 }
541
542 end:
543 return stream_class;
544 }
545
546 struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
547 struct bt_ctf_trace *trace, const char *name)
548 {
549 size_t i;
550 struct bt_ctf_clock *clock = NULL;
551
552 if (!trace || !name) {
553 goto end;
554 }
555
556 for (i = 0; i < trace->clocks->len; ++i) {
557 struct bt_ctf_clock *cur_clk =
558 g_ptr_array_index(trace->clocks, i);
559 const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk);
560
561 if (!cur_clk_name) {
562 goto end;
563 }
564
565 if (!strcmp(cur_clk_name, name)) {
566 clock = cur_clk;
567 bt_get(clock);
568 goto end;
569 }
570 }
571
572 end:
573 return clock;
574 }
575
576 BT_HIDDEN
577 const char *get_byte_order_string(int byte_order)
578 {
579 const char *string;
580
581 switch (byte_order) {
582 case LITTLE_ENDIAN:
583 string = "le";
584 break;
585 case BIG_ENDIAN:
586 string = "be";
587 break;
588 default:
589 string = "unknown";
590 break;
591 }
592
593 return string;
594 }
595
596 static
597 int append_trace_metadata(struct bt_ctf_trace *trace,
598 struct metadata_context *context)
599 {
600 unsigned char *uuid = trace->uuid;
601 int ret;
602
603 g_string_append(context->string, "trace {\n");
604
605 g_string_append(context->string, "\tmajor = 1;\n");
606 g_string_append(context->string, "\tminor = 8;\n");
607
608 g_string_append_printf(context->string,
609 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
610 uuid[0], uuid[1], uuid[2], uuid[3],
611 uuid[4], uuid[5], uuid[6], uuid[7],
612 uuid[8], uuid[9], uuid[10], uuid[11],
613 uuid[12], uuid[13], uuid[14], uuid[15]);
614 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
615 get_byte_order_string(trace->byte_order));
616
617 g_string_append(context->string, "\tpacket.header := ");
618 context->current_indentation_level++;
619 g_string_assign(context->field_name, "");
620 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
621 context);
622 if (ret) {
623 goto end;
624 }
625 context->current_indentation_level--;
626
627 g_string_append(context->string, ";\n};\n\n");
628 end:
629 return ret;
630 }
631
632 static
633 void append_env_metadata(struct bt_ctf_trace *trace,
634 struct metadata_context *context)
635 {
636 int i;
637 int env_size;
638
639 env_size = bt_ctf_attributes_get_count(trace->environment);
640
641 if (env_size <= 0) {
642 return;
643 }
644
645 g_string_append(context->string, "env {\n");
646
647 for (i = 0; i < env_size; ++i) {
648 struct bt_value *env_field_value_obj = NULL;
649 const char *entry_name;
650
651 entry_name = bt_ctf_attributes_get_field_name(
652 trace->environment, i);
653 env_field_value_obj = bt_ctf_attributes_get_field_value(
654 trace->environment, i);
655
656 if (!entry_name || !env_field_value_obj) {
657 goto loop_next;
658 }
659
660 switch (bt_value_get_type(env_field_value_obj)) {
661 case BT_VALUE_TYPE_INTEGER:
662 {
663 int ret;
664 int64_t int_value;
665
666 ret = bt_value_integer_get(env_field_value_obj,
667 &int_value);
668
669 if (ret) {
670 goto loop_next;
671 }
672
673 g_string_append_printf(context->string,
674 "\t%s = %" PRId64 ";\n", entry_name,
675 int_value);
676 break;
677 }
678 case BT_VALUE_TYPE_STRING:
679 {
680 int ret;
681 const char *str_value;
682 char *escaped_str = NULL;
683
684 ret = bt_value_string_get(env_field_value_obj,
685 &str_value);
686
687 if (ret) {
688 goto loop_next;
689 }
690
691 escaped_str = g_strescape(str_value, NULL);
692
693 if (!escaped_str) {
694 goto loop_next;
695 }
696
697 g_string_append_printf(context->string,
698 "\t%s = \"%s\";\n", entry_name, escaped_str);
699 free(escaped_str);
700 break;
701 }
702
703 default:
704 goto loop_next;
705 }
706
707 loop_next:
708 BT_PUT(env_field_value_obj);
709 }
710
711 g_string_append(context->string, "};\n\n");
712 }
713
714 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
715 {
716 char *metadata = NULL;
717 struct metadata_context *context = NULL;
718 int err = 0;
719 size_t i;
720
721 if (!trace) {
722 goto end;
723 }
724
725 context = g_new0(struct metadata_context, 1);
726 if (!context) {
727 goto end;
728 }
729
730 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
731 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
732 g_string_append(context->string, "/* CTF 1.8 */\n\n");
733 if (append_trace_metadata(trace, context)) {
734 goto error;
735 }
736 append_env_metadata(trace, context);
737 g_ptr_array_foreach(trace->clocks,
738 (GFunc)bt_ctf_clock_serialize, context);
739
740 for (i = 0; i < trace->stream_classes->len; i++) {
741 err = bt_ctf_stream_class_serialize(
742 trace->stream_classes->pdata[i], context);
743 if (err) {
744 goto error;
745 }
746 }
747
748 metadata = context->string->str;
749 error:
750 g_string_free(context->string, err ? TRUE : FALSE);
751 g_string_free(context->field_name, TRUE);
752 g_free(context);
753 end:
754 return metadata;
755 }
756
757 enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
758 {
759 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
760
761 if (!trace) {
762 goto end;
763 }
764
765 switch (trace->byte_order) {
766 case BIG_ENDIAN:
767 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
768 break;
769 case LITTLE_ENDIAN:
770 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
771 break;
772 default:
773 break;
774 }
775 end:
776 return ret;
777 }
778
779 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
780 enum bt_ctf_byte_order byte_order)
781 {
782 int ret = 0;
783 int internal_byte_order;
784
785 if (!trace || trace->frozen) {
786 ret = -1;
787 goto end;
788 }
789
790 switch (byte_order) {
791 case BT_CTF_BYTE_ORDER_NATIVE:
792 /*
793 * This doesn't make sense since the CTF specification defines
794 * the "native" byte order as "the byte order described in the
795 * trace description". However, this behavior had been
796 * implemented as part of v1.2 and is kept to maintain
797 * compatibility.
798 *
799 * This may be changed on a major version bump only.
800 */
801 internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ?
802 LITTLE_ENDIAN : BIG_ENDIAN;
803 break;
804 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
805 internal_byte_order = LITTLE_ENDIAN;
806 break;
807 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
808 case BT_CTF_BYTE_ORDER_NETWORK:
809 internal_byte_order = BIG_ENDIAN;
810 break;
811 default:
812 ret = -1;
813 goto end;
814 }
815
816 trace->byte_order = internal_byte_order;
817 end:
818 return ret;
819 }
820
821 struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
822 struct bt_ctf_trace *trace)
823 {
824 struct bt_ctf_field_type *field_type = NULL;
825
826 if (!trace) {
827 goto end;
828 }
829
830 bt_get(trace->packet_header_type);
831 field_type = trace->packet_header_type;
832 end:
833 return field_type;
834 }
835
836 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
837 struct bt_ctf_field_type *packet_header_type)
838 {
839 int ret = 0;
840
841 if (!trace || !packet_header_type || trace->frozen) {
842 ret = -1;
843 goto end;
844 }
845
846 /* packet_header_type must be a structure */
847 if (bt_ctf_field_type_get_type_id(packet_header_type) !=
848 CTF_TYPE_STRUCT) {
849 ret = -1;
850 goto end;
851 }
852
853 bt_get(packet_header_type);
854 bt_put(trace->packet_header_type);
855 trace->packet_header_type = packet_header_type;
856 end:
857 return ret;
858 }
859
860 void bt_ctf_trace_get(struct bt_ctf_trace *trace)
861 {
862 bt_get(trace);
863 }
864
865 void bt_ctf_trace_put(struct bt_ctf_trace *trace)
866 {
867 bt_put(trace);
868
869 }
870
871 BT_HIDDEN
872 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
873 {
874 unsigned int alignment, size;
875 struct bt_ctf_field_type *field_type = NULL;
876
877 if (alias >= NR_FIELD_TYPE_ALIAS) {
878 goto end;
879 }
880
881 alignment = field_type_aliases_alignments[alias];
882 size = field_type_aliases_sizes[alias];
883 field_type = bt_ctf_field_type_integer_create(size);
884 bt_ctf_field_type_set_alignment(field_type, alignment);
885 end:
886 return field_type;
887 }
888
889 static
890 int bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
891 {
892 int ret = 0;
893
894 ret = bt_ctf_trace_resolve_types(trace);
895 if (ret) {
896 goto end;
897 }
898
899 bt_ctf_attributes_freeze(trace->environment);
900 trace->frozen = 1;
901 end:
902 return ret;
903 }
904
905 static
906 int init_trace_packet_header(struct bt_ctf_trace *trace)
907 {
908 int ret = 0;
909 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
910 struct bt_ctf_field_type *_uint32_t =
911 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
912 struct bt_ctf_field_type *_uint8_t =
913 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
914 struct bt_ctf_field_type *trace_packet_header_type =
915 bt_ctf_field_type_structure_create();
916 struct bt_ctf_field_type *uuid_array_type =
917 bt_ctf_field_type_array_create(_uint8_t, 16);
918
919 if (!trace_packet_header_type || !uuid_array_type) {
920 ret = -1;
921 goto end;
922 }
923
924 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
925 _uint32_t, "magic");
926 if (ret) {
927 goto end;
928 }
929
930 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
931 uuid_array_type, "uuid");
932 if (ret) {
933 goto end;
934 }
935
936 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
937 _uint32_t, "stream_id");
938 if (ret) {
939 goto end;
940 }
941
942 ret = bt_ctf_trace_set_packet_header_type(trace,
943 trace_packet_header_type);
944 if (ret) {
945 goto end;
946 }
947 end:
948 bt_put(uuid_array_type);
949 bt_put(_uint32_t);
950 bt_put(_uint8_t);
951 bt_put(magic);
952 bt_put(uuid_array);
953 bt_put(trace_packet_header_type);
954 return ret;
955 }
This page took 0.05103 seconds and 5 git commands to generate.