ir: validate sequence length fields and variant tags of event 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>
654c1444 36#include <babeltrace/ctf-ir/utils.h>
bc37ae52 37#include <babeltrace/compiler.h>
7f800dc7 38#include <babeltrace/objects.h>
bc37ae52
JG
39
40#define DEFAULT_IDENTIFIER_SIZE 128
41#define DEFAULT_METADATA_STRING_SIZE 4096
42
bc37ae52
JG
43static
44void bt_ctf_trace_destroy(struct bt_ctf_ref *ref);
45static
46int init_trace_packet_header(struct bt_ctf_trace *trace);
f67f3a6e
JG
47static
48void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
bc37ae52 49
bc37ae52
JG
50static
51const 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
58static
59const 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
d3814b54
JG
68static
69void 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
bc37ae52
JG
75struct 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);
bc37ae52 86 trace->clocks = g_ptr_array_new_with_free_func(
d3814b54 87 (GDestroyNotify) bt_ctf_clock_put);
bc37ae52 88 trace->streams = g_ptr_array_new_with_free_func(
d3814b54 89 (GDestroyNotify) bt_ctf_stream_put);
bc37ae52 90 trace->stream_classes = g_ptr_array_new_with_free_func(
d3814b54 91 (GDestroyNotify) put_stream_class);
7f800dc7 92 if (!trace->clocks || !trace->stream_classes || !trace->streams) {
bc37ae52
JG
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
7f800dc7
PP
102 /* Create the environment array object */
103 trace->environment = bt_ctf_attributes_create();
104 if (!trace->environment) {
105 goto error_destroy;
106 }
107
bc37ae52
JG
108 return trace;
109
110error_destroy:
111 bt_ctf_trace_destroy(&trace->ref_count);
112 trace = NULL;
113error:
114 return trace;
115}
116
117void 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) {
7f800dc7 127 bt_ctf_attributes_destroy(trace->environment);
bc37ae52
JG
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
d246b111 142 bt_ctf_field_type_put(trace->packet_header_type);
bc37ae52
JG
143 g_free(trace);
144}
145
146struct 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
d246b111 158 stream = bt_ctf_stream_create(stream_class, trace);
bc37ae52
JG
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) {
ef0c4a15
JG
170 ret = bt_ctf_trace_add_stream_class(trace, stream_class);
171 if (ret) {
172 goto error;
bc37ae52 173 }
bc37ae52
JG
174 }
175
176 bt_ctf_stream_get(stream);
177 g_ptr_array_add(trace->streams, stream);
c35a1669 178
bc37ae52 179 return stream;
bc37ae52
JG
180error:
181 bt_ctf_stream_put(stream);
182 return NULL;
183}
184
7f800dc7
PP
185int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
186 const char *name, struct bt_object *value)
bc37ae52 187{
bc37ae52
JG
188 int ret = 0;
189
a0d12916 190 if (!trace || !name || !value ||
7f800dc7
PP
191 bt_ctf_validate_identifier(name) ||
192 !(bt_object_is_integer(value) || bt_object_is_string(value))) {
bc37ae52 193 ret = -1;
7f800dc7 194 goto end;
bc37ae52
JG
195 }
196
197 if (strchr(name, ' ')) {
198 ret = -1;
7f800dc7 199 goto end;
bc37ae52
JG
200 }
201
a0d12916
JG
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
7f800dc7
PP
222 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
223 value);
bc37ae52 224
7f800dc7
PP
225end:
226 return ret;
227}
bc37ae52 228
7f800dc7
PP
229int 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) {
bc37ae52 236 ret = -1;
7f800dc7 237 goto end;
bc37ae52
JG
238 }
239
a0d12916
JG
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
7f800dc7 256 env_value_string_obj = bt_object_string_create_init(value);
bc37ae52 257
7f800dc7
PP
258 if (!env_value_string_obj) {
259 ret = -1;
260 goto end;
bc37ae52
JG
261 }
262
a0d12916
JG
263 if (trace->frozen) {
264 bt_object_freeze(env_value_string_obj);
265 }
7f800dc7
PP
266 ret = bt_ctf_trace_set_environment_field(trace, name,
267 env_value_string_obj);
268
269end:
270 BT_OBJECT_PUT(env_value_string_obj);
3487c9f3 271
3487c9f3
JG
272 return ret;
273}
274
7f800dc7
PP
275int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
276 const char *name, int64_t value)
3487c9f3 277{
3487c9f3 278 int ret = 0;
7f800dc7 279 struct bt_object *env_value_integer_obj = NULL;
3487c9f3
JG
280
281 if (!trace || !name) {
282 ret = -1;
7f800dc7 283 goto end;
3487c9f3
JG
284 }
285
a0d12916
JG
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
7f800dc7
PP
302 env_value_integer_obj = bt_object_integer_create_init(value);
303 if (!env_value_integer_obj) {
3487c9f3 304 ret = -1;
7f800dc7 305 goto end;
3487c9f3
JG
306 }
307
7f800dc7
PP
308 ret = bt_ctf_trace_set_environment_field(trace, name,
309 env_value_integer_obj);
a0d12916
JG
310 if (trace->frozen) {
311 bt_object_freeze(env_value_integer_obj);
312 }
7f800dc7
PP
313end:
314 BT_OBJECT_PUT(env_value_integer_obj);
bc37ae52 315
bc37ae52
JG
316 return ret;
317}
318
e6fa2160
JG
319int 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
7f800dc7 328 ret = bt_ctf_attributes_get_count(trace->environment);
e6fa2160 329
e6fa2160 330end:
7f800dc7 331 return ret;
e6fa2160
JG
332}
333
334const char *
335bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
336 int index)
337{
e6fa2160
JG
338 const char *ret = NULL;
339
7f800dc7 340 if (!trace) {
e6fa2160
JG
341 goto end;
342 }
343
7f800dc7
PP
344 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
345
e6fa2160
JG
346end:
347 return ret;
348}
349
7f800dc7
PP
350struct bt_object *bt_ctf_trace_get_environment_field_value(
351 struct bt_ctf_trace *trace, int index)
e6fa2160 352{
7f800dc7 353 struct bt_object *ret = NULL;
e6fa2160 354
7f800dc7 355 if (!trace) {
e6fa2160
JG
356 goto end;
357 }
358
7f800dc7
PP
359 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
360
e6fa2160
JG
361end:
362 return ret;
363}
364
7f800dc7
PP
365struct bt_object *bt_ctf_trace_get_environment_field_value_by_name(
366 struct bt_ctf_trace *trace, const char *name)
e6fa2160 367{
7f800dc7 368 struct bt_object *ret = NULL;
e6fa2160 369
7f800dc7 370 if (!trace || !name) {
e6fa2160
JG
371 goto end;
372 }
373
7f800dc7
PP
374 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
375 name);
376
e6fa2160
JG
377end:
378 return ret;
379}
380
bc37ae52
JG
381int 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);
401end:
402 return ret;
403}
404
884cd6c3
JG
405int 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;
414end:
415 return ret;
416}
417
418struct 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);
429end:
430 return clock;
431}
432
ef0c4a15
JG
433int 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) {
c968baf7 446 /* Stream already registered to the trace */
ef0c4a15
JG
447 ret = -1;
448 goto end;
449 }
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
0ddffae5
JG
466 if (bt_ctf_stream_class_set_id_no_check(stream_class,
467 stream_id)) {
ef0c4a15
JG
468 /* TODO Should retry with a different stream id */
469 ret = -1;
470 goto end;
471 }
472 }
473
d3814b54
JG
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
ef0c4a15
JG
481 bt_ctf_stream_class_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 }
ef0c4a15 498
f67f3a6e 499 bt_ctf_stream_class_freeze(stream_class);
7c4c65c4
JG
500 if (!trace->frozen) {
501 bt_ctf_trace_freeze(trace);
502 }
ef0c4a15 503end:
d3814b54
JG
504 if (ret) {
505 (void) bt_ctf_stream_class_set_trace(stream_class, NULL);
506 }
ef0c4a15
JG
507 return ret;
508}
509
510int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
511{
512 int ret;
513
514 if (!trace) {
515 ret = -1;
516 goto end;
517 }
518
519 ret = trace->stream_classes->len;
520end:
521 return ret;
522}
523
524struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
525 struct bt_ctf_trace *trace, int index)
526{
527 struct bt_ctf_stream_class *stream_class = NULL;
528
529 if (!trace || index < 0 || index >= trace->stream_classes->len) {
530 goto end;
531 }
532
533 stream_class = g_ptr_array_index(trace->stream_classes, index);
534 bt_ctf_stream_class_get(stream_class);
535end:
536 return stream_class;
537}
538
7e4347a3
PP
539struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
540 struct bt_ctf_trace *trace, const char *name)
541{
542 size_t i;
543 struct bt_ctf_clock *clock = NULL;
544
545 if (!trace || !name) {
546 goto end;
547 }
548
549 for (i = 0; i < trace->clocks->len; ++i) {
550 struct bt_ctf_clock *cur_clk =
551 g_ptr_array_index(trace->clocks, i);
552 const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk);
553
554 if (!cur_clk_name) {
555 goto end;
556 }
557
558 if (!strcmp(cur_clk_name, name)) {
559 clock = cur_clk;
560 bt_ctf_clock_get(clock);
561 goto end;
562 }
563 }
564
565end:
566 return clock;
567}
568
bc37ae52
JG
569BT_HIDDEN
570const char *get_byte_order_string(int byte_order)
571{
572 const char *string;
573
574 switch (byte_order) {
575 case LITTLE_ENDIAN:
576 string = "le";
577 break;
578 case BIG_ENDIAN:
579 string = "be";
580 break;
581 default:
582 string = "unknown";
583 break;
584 }
585
586 return string;
587}
588
589static
590int append_trace_metadata(struct bt_ctf_trace *trace,
591 struct metadata_context *context)
592{
593 unsigned char *uuid = trace->uuid;
594 int ret;
595
596 g_string_append(context->string, "trace {\n");
597
598 g_string_append(context->string, "\tmajor = 1;\n");
599 g_string_append(context->string, "\tminor = 8;\n");
600
601 g_string_append_printf(context->string,
602 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
603 uuid[0], uuid[1], uuid[2], uuid[3],
604 uuid[4], uuid[5], uuid[6], uuid[7],
605 uuid[8], uuid[9], uuid[10], uuid[11],
606 uuid[12], uuid[13], uuid[14], uuid[15]);
607 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
608 get_byte_order_string(trace->byte_order));
609
610 g_string_append(context->string, "\tpacket.header := ");
611 context->current_indentation_level++;
612 g_string_assign(context->field_name, "");
d246b111 613 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
bc37ae52
JG
614 context);
615 if (ret) {
616 goto end;
617 }
618 context->current_indentation_level--;
619
620 g_string_append(context->string, ";\n};\n\n");
621end:
622 return ret;
623}
624
bc37ae52
JG
625static
626void append_env_metadata(struct bt_ctf_trace *trace,
627 struct metadata_context *context)
628{
7f800dc7
PP
629 int i;
630 int env_size;
631
632 env_size = bt_ctf_attributes_get_count(trace->environment);
633
634 if (env_size <= 0) {
bc37ae52
JG
635 return;
636 }
637
638 g_string_append(context->string, "env {\n");
7f800dc7
PP
639
640 for (i = 0; i < env_size; ++i) {
641 struct bt_object *env_field_value_obj = NULL;
642 const char *entry_name;
7f800dc7
PP
643
644 entry_name = bt_ctf_attributes_get_field_name(
645 trace->environment, i);
646 env_field_value_obj = bt_ctf_attributes_get_field_value(
647 trace->environment, i);
648
649 if (!entry_name || !env_field_value_obj) {
650 goto loop_next;
651 }
652
653 switch (bt_object_get_type(env_field_value_obj)) {
654 case BT_OBJECT_TYPE_INTEGER:
b8248cc0
PP
655 {
656 int ret;
657 int64_t int_value;
658
7f800dc7
PP
659 ret = bt_object_integer_get(env_field_value_obj,
660 &int_value);
661
662 if (ret) {
663 goto loop_next;
664 }
665
666 g_string_append_printf(context->string,
667 "\t%s = %" PRId64 ";\n", entry_name,
668 int_value);
669 break;
b8248cc0 670 }
7f800dc7
PP
671 case BT_OBJECT_TYPE_STRING:
672 {
673 int ret;
674 const char *str_value;
675 char *escaped_str = NULL;
676
677 ret = bt_object_string_get(env_field_value_obj,
678 &str_value);
679
680 if (ret) {
681 goto loop_next;
682 }
683
684 escaped_str = g_strescape(str_value, NULL);
685
686 if (!escaped_str) {
687 goto loop_next;
688 }
689
690 g_string_append_printf(context->string,
691 "\t%s = \"%s\";\n", entry_name, escaped_str);
692 free(escaped_str);
693 break;
694 }
695
696 default:
697 goto loop_next;
698 }
699
700loop_next:
701 BT_OBJECT_PUT(env_field_value_obj);
702 }
703
bc37ae52
JG
704 g_string_append(context->string, "};\n\n");
705}
706
707char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
708{
709 char *metadata = NULL;
710 struct metadata_context *context = NULL;
711 int err = 0;
712 size_t i;
713
714 if (!trace) {
715 goto end;
716 }
717
718 context = g_new0(struct metadata_context, 1);
719 if (!context) {
720 goto end;
721 }
722
723 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
724 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
725 g_string_append(context->string, "/* CTF 1.8 */\n\n");
726 if (append_trace_metadata(trace, context)) {
727 goto error;
728 }
729 append_env_metadata(trace, context);
730 g_ptr_array_foreach(trace->clocks,
731 (GFunc)bt_ctf_clock_serialize, context);
732
733 for (i = 0; i < trace->stream_classes->len; i++) {
734 err = bt_ctf_stream_class_serialize(
735 trace->stream_classes->pdata[i], context);
736 if (err) {
737 goto error;
738 }
739 }
740
741 metadata = context->string->str;
742error:
743 g_string_free(context->string, err ? TRUE : FALSE);
744 g_string_free(context->field_name, TRUE);
745 g_free(context);
746end:
747 return metadata;
748}
749
4ed90fb3
JG
750enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
751{
752 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
753
754 if (!trace) {
755 goto end;
756 }
757
758 switch (trace->byte_order) {
759 case BIG_ENDIAN:
760 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
761 break;
762 case LITTLE_ENDIAN:
763 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
764 break;
765 default:
766 break;
767 }
768end:
769 return ret;
770}
771
bc37ae52
JG
772int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
773 enum bt_ctf_byte_order byte_order)
774{
775 int ret = 0;
776 int internal_byte_order;
777
778 if (!trace || trace->frozen) {
779 ret = -1;
780 goto end;
781 }
782
783 switch (byte_order) {
784 case BT_CTF_BYTE_ORDER_NATIVE:
c35a1669
JG
785 /*
786 * This doesn't make sense since the CTF specification defines
787 * the "native" byte order as "the byte order described in the
788 * trace description". However, this behavior had been
789 * implemented as part of v1.2 and is kept to maintain
790 * compatibility.
791 *
792 * This may be changed on a major version bump only.
793 */
794 internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ?
bc37ae52
JG
795 LITTLE_ENDIAN : BIG_ENDIAN;
796 break;
797 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
798 internal_byte_order = LITTLE_ENDIAN;
799 break;
800 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
801 case BT_CTF_BYTE_ORDER_NETWORK:
802 internal_byte_order = BIG_ENDIAN;
803 break;
804 default:
805 ret = -1;
806 goto end;
807 }
808
809 trace->byte_order = internal_byte_order;
bc37ae52
JG
810end:
811 return ret;
812}
813
d246b111
JG
814struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
815 struct bt_ctf_trace *trace)
816{
817 struct bt_ctf_field_type *field_type = NULL;
818
819 if (!trace) {
820 goto end;
821 }
822
823 bt_ctf_field_type_get(trace->packet_header_type);
824 field_type = trace->packet_header_type;
825end:
826 return field_type;
827}
828
829int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
830 struct bt_ctf_field_type *packet_header_type)
831{
832 int ret = 0;
833
834 if (!trace || !packet_header_type || trace->frozen) {
835 ret = -1;
836 goto end;
837 }
838
839 /* packet_header_type must be a structure */
840 if (bt_ctf_field_type_get_type_id(packet_header_type) !=
841 CTF_TYPE_STRUCT) {
842 ret = -1;
843 goto end;
844 }
845
846 bt_ctf_field_type_get(packet_header_type);
847 bt_ctf_field_type_put(trace->packet_header_type);
848 trace->packet_header_type = packet_header_type;
849end:
850 return ret;
851}
852
bc37ae52
JG
853void bt_ctf_trace_get(struct bt_ctf_trace *trace)
854{
855 if (!trace) {
856 return;
857 }
858
859 bt_ctf_ref_get(&trace->ref_count);
860}
861
862void bt_ctf_trace_put(struct bt_ctf_trace *trace)
863{
864 if (!trace) {
865 return;
866 }
867
868 bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy);
869}
870
bc37ae52
JG
871BT_HIDDEN
872struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
873{
874 unsigned int alignment, size;
875 struct bt_ctf_field_type *field_type;
876
877 if (alias >= NR_FIELD_TYPE_ALIAS) {
878 return NULL;
879 }
880
881 alignment = field_type_aliases_alignments[alias];
882 size = field_type_aliases_sizes[alias];
883 field_type = bt_ctf_field_type_integer_create(size);
884 bt_ctf_field_type_set_alignment(field_type, alignment);
885 return field_type;
886}
887
f67f3a6e
JG
888static
889void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
890{
891 bt_ctf_attributes_freeze(trace->environment);
892 trace->frozen = 1;
893}
894
bc37ae52
JG
895static
896int init_trace_packet_header(struct bt_ctf_trace *trace)
897{
bc37ae52 898 int ret = 0;
d246b111 899 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
bc37ae52
JG
900 struct bt_ctf_field_type *_uint32_t =
901 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
902 struct bt_ctf_field_type *_uint8_t =
903 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
904 struct bt_ctf_field_type *trace_packet_header_type =
905 bt_ctf_field_type_structure_create();
906 struct bt_ctf_field_type *uuid_array_type =
907 bt_ctf_field_type_array_create(_uint8_t, 16);
908
909 if (!trace_packet_header_type || !uuid_array_type) {
910 ret = -1;
911 goto end;
912 }
913
bc37ae52
JG
914 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
915 _uint32_t, "magic");
916 if (ret) {
917 goto end;
918 }
919
920 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
921 uuid_array_type, "uuid");
922 if (ret) {
923 goto end;
924 }
925
926 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
927 _uint32_t, "stream_id");
928 if (ret) {
929 goto end;
930 }
931
662e778c
JG
932 ret = bt_ctf_trace_set_packet_header_type(trace,
933 trace_packet_header_type);
934 if (ret) {
935 goto end;
936 }
bc37ae52
JG
937end:
938 bt_ctf_field_type_put(uuid_array_type);
939 bt_ctf_field_type_put(_uint32_t);
940 bt_ctf_field_type_put(_uint8_t);
941 bt_ctf_field_put(magic);
942 bt_ctf_field_put(uuid_array);
662e778c 943 bt_ctf_field_type_put(trace_packet_header_type);
bc37ae52
JG
944
945 return ret;
946}
This page took 0.063488 seconds and 4 git commands to generate.