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