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