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