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