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