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