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