ir: split event files into event and event-class files
[babeltrace.git] / formats / ctf / ir / trace.c
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-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/event-class.h>
35 #include <babeltrace/ctf-ir/event-class-internal.h>
36 #include <babeltrace/ctf-writer/functor-internal.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/attributes-internal.h>
39 #include <babeltrace/ctf-ir/validation-internal.h>
40 #include <babeltrace/ctf-ir/utils.h>
41 #include <babeltrace/compiler.h>
42 #include <babeltrace/values.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/endian.h>
45
46 #define DEFAULT_IDENTIFIER_SIZE 128
47 #define DEFAULT_METADATA_STRING_SIZE 4096
48
49 static
50 void bt_ctf_trace_destroy(struct bt_object *obj);
51 static
52 int init_trace_packet_header(struct bt_ctf_trace *trace);
53 static
54 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
55
56 static
57 const unsigned int field_type_aliases_alignments[] = {
58 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
59 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
60 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
61 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
62 };
63
64 static
65 const unsigned int field_type_aliases_sizes[] = {
66 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
67 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
68 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
69 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
70 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
71 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
72 };
73
74 struct bt_ctf_trace *bt_ctf_trace_create(void)
75 {
76 struct bt_ctf_trace *trace = NULL;
77
78 trace = g_new0(struct bt_ctf_trace, 1);
79 if (!trace) {
80 goto error;
81 }
82
83 bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
84 bt_object_init(trace, bt_ctf_trace_destroy);
85 trace->clocks = g_ptr_array_new_with_free_func(
86 (GDestroyNotify) bt_put);
87 trace->streams = g_ptr_array_new_with_free_func(
88 (GDestroyNotify) bt_object_release);
89 trace->stream_classes = g_ptr_array_new_with_free_func(
90 (GDestroyNotify) bt_object_release);
91 if (!trace->clocks || !trace->stream_classes || !trace->streams) {
92 goto error;
93 }
94
95 /* Generate a trace UUID */
96 uuid_generate(trace->uuid);
97 if (init_trace_packet_header(trace)) {
98 goto error;
99 }
100
101 /* Create the environment array object */
102 trace->environment = bt_ctf_attributes_create();
103 if (!trace->environment) {
104 goto error;
105 }
106
107 return trace;
108
109 error:
110 BT_PUT(trace);
111 return trace;
112 }
113
114 void bt_ctf_trace_destroy(struct bt_object *obj)
115 {
116 struct bt_ctf_trace *trace;
117
118 trace = container_of(obj, struct bt_ctf_trace, base);
119 if (trace->environment) {
120 bt_ctf_attributes_destroy(trace->environment);
121 }
122
123 if (trace->clocks) {
124 g_ptr_array_free(trace->clocks, TRUE);
125 }
126
127 if (trace->streams) {
128 g_ptr_array_free(trace->streams, TRUE);
129 }
130
131 if (trace->stream_classes) {
132 g_ptr_array_free(trace->stream_classes, TRUE);
133 }
134
135 bt_put(trace->packet_header_type);
136 g_free(trace);
137 }
138
139 struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
140 struct bt_ctf_stream_class *stream_class)
141 {
142 int ret;
143 int stream_class_found = 0;
144 size_t i;
145 struct bt_ctf_stream *stream = NULL;
146
147 if (!trace || !stream_class) {
148 goto error;
149 }
150
151 for (i = 0; i < trace->stream_classes->len; i++) {
152 if (trace->stream_classes->pdata[i] == stream_class) {
153 stream_class_found = 1;
154 }
155 }
156
157 if (!stream_class_found) {
158 ret = bt_ctf_trace_add_stream_class(trace, stream_class);
159 if (ret) {
160 goto error;
161 }
162 }
163
164 stream = bt_ctf_stream_create(stream_class, trace);
165 if (!stream) {
166 goto error;
167 }
168
169 g_ptr_array_add(trace->streams, stream);
170 return stream;
171 error:
172 BT_PUT(stream);
173 return stream;
174 }
175
176 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
177 const char *name, struct bt_value *value)
178 {
179 int ret = 0;
180
181 if (!trace || !name || !value ||
182 bt_ctf_validate_identifier(name) ||
183 !(bt_value_is_integer(value) || bt_value_is_string(value))) {
184 ret = -1;
185 goto end;
186 }
187
188 if (strchr(name, ' ')) {
189 ret = -1;
190 goto end;
191 }
192
193 if (trace->frozen) {
194 /*
195 * New environment fields may be added to a frozen trace,
196 * but existing fields may not be changed.
197 *
198 * The object passed is frozen like all other attributes.
199 */
200 struct bt_value *attribute =
201 bt_ctf_attributes_get_field_value_by_name(
202 trace->environment, name);
203
204 if (attribute) {
205 BT_PUT(attribute);
206 ret = -1;
207 goto end;
208 }
209
210 bt_value_freeze(value);
211 }
212
213 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
214 value);
215
216 end:
217 return ret;
218 }
219
220 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
221 const char *name, const char *value)
222 {
223 int ret = 0;
224 struct bt_value *env_value_string_obj = NULL;
225
226 if (!trace || !name || !value) {
227 ret = -1;
228 goto end;
229 }
230
231 if (trace->frozen) {
232 /*
233 * New environment fields may be added to a frozen trace,
234 * but existing fields may not be changed.
235 */
236 struct bt_value *attribute =
237 bt_ctf_attributes_get_field_value_by_name(
238 trace->environment, name);
239
240 if (attribute) {
241 BT_PUT(attribute);
242 ret = -1;
243 goto end;
244 }
245 }
246
247 env_value_string_obj = bt_value_string_create_init(value);
248
249 if (!env_value_string_obj) {
250 ret = -1;
251 goto end;
252 }
253
254 if (trace->frozen) {
255 bt_value_freeze(env_value_string_obj);
256 }
257 ret = bt_ctf_trace_set_environment_field(trace, name,
258 env_value_string_obj);
259
260 end:
261 BT_PUT(env_value_string_obj);
262 return ret;
263 }
264
265 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
266 const char *name, int64_t value)
267 {
268 int ret = 0;
269 struct bt_value *env_value_integer_obj = NULL;
270
271 if (!trace || !name) {
272 ret = -1;
273 goto end;
274 }
275
276 if (trace->frozen) {
277 /*
278 * New environment fields may be added to a frozen trace,
279 * but existing fields may not be changed.
280 */
281 struct bt_value *attribute =
282 bt_ctf_attributes_get_field_value_by_name(
283 trace->environment, name);
284
285 if (attribute) {
286 BT_PUT(attribute);
287 ret = -1;
288 goto end;
289 }
290 }
291
292 env_value_integer_obj = bt_value_integer_create_init(value);
293 if (!env_value_integer_obj) {
294 ret = -1;
295 goto end;
296 }
297
298 ret = bt_ctf_trace_set_environment_field(trace, name,
299 env_value_integer_obj);
300 if (trace->frozen) {
301 bt_value_freeze(env_value_integer_obj);
302 }
303 end:
304 BT_PUT(env_value_integer_obj);
305 return ret;
306 }
307
308 int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
309 {
310 int ret = 0;
311
312 if (!trace) {
313 ret = -1;
314 goto end;
315 }
316
317 ret = bt_ctf_attributes_get_count(trace->environment);
318
319 end:
320 return ret;
321 }
322
323 const char *
324 bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
325 int index)
326 {
327 const char *ret = NULL;
328
329 if (!trace) {
330 goto end;
331 }
332
333 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
334
335 end:
336 return ret;
337 }
338
339 struct bt_value *bt_ctf_trace_get_environment_field_value(
340 struct bt_ctf_trace *trace, int index)
341 {
342 struct bt_value *ret = NULL;
343
344 if (!trace) {
345 goto end;
346 }
347
348 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
349
350 end:
351 return ret;
352 }
353
354 struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
355 struct bt_ctf_trace *trace, const char *name)
356 {
357 struct bt_value *ret = NULL;
358
359 if (!trace || !name) {
360 goto end;
361 }
362
363 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
364 name);
365
366 end:
367 return ret;
368 }
369
370 int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
371 struct bt_ctf_clock *clock)
372 {
373 int ret = 0;
374 struct search_query query = { .value = clock, .found = 0 };
375
376 if (!trace || !clock) {
377 ret = -1;
378 goto end;
379 }
380
381 /* Check for duplicate clocks */
382 g_ptr_array_foreach(trace->clocks, value_exists, &query);
383 if (query.found) {
384 ret = -1;
385 goto end;
386 }
387
388 bt_get(clock);
389 g_ptr_array_add(trace->clocks, clock);
390 end:
391 return ret;
392 }
393
394 int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace)
395 {
396 int ret = -1;
397
398 if (!trace) {
399 goto end;
400 }
401
402 ret = trace->clocks->len;
403 end:
404 return ret;
405 }
406
407 struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace,
408 int index)
409 {
410 struct bt_ctf_clock *clock = NULL;
411
412 if (!trace || index < 0 || index >= trace->clocks->len) {
413 goto end;
414 }
415
416 clock = g_ptr_array_index(trace->clocks, index);
417 bt_get(clock);
418 end:
419 return clock;
420 }
421
422 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
423 struct bt_ctf_stream_class *stream_class)
424 {
425 int ret, i;
426 int64_t stream_id;
427 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
428 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
429 const enum bt_ctf_validation_flag trace_sc_validation_flags =
430 BT_CTF_VALIDATION_FLAG_TRACE |
431 BT_CTF_VALIDATION_FLAG_STREAM;
432 const enum bt_ctf_validation_flag ec_validation_flags =
433 BT_CTF_VALIDATION_FLAG_EVENT;
434 struct bt_ctf_field_type *packet_header_type = NULL;
435 struct bt_ctf_field_type *packet_context_type = NULL;
436 struct bt_ctf_field_type *event_header_type = NULL;
437 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
438 int event_class_count;
439
440 if (!trace || !stream_class) {
441 ret = -1;
442 goto end;
443 }
444
445 event_class_count =
446 bt_ctf_stream_class_get_event_class_count(stream_class);
447 assert(event_class_count >= 0);
448
449 /* Check for duplicate stream classes */
450 for (i = 0; i < trace->stream_classes->len; i++) {
451 if (trace->stream_classes->pdata[i] == stream_class) {
452 /* Stream class already registered to the trace */
453 ret = -1;
454 goto end;
455 }
456 }
457
458 /*
459 * We're about to freeze both the trace and the stream class.
460 * Also, each event class contained in this stream class are
461 * already frozen.
462 *
463 * This trace, this stream class, and all its event classes
464 * should be valid at this point.
465 *
466 * Validate trace and stream class first, then each event
467 * class of this stream class can be validated individually.
468 */
469 packet_header_type =
470 bt_ctf_trace_get_packet_header_type(trace);
471 packet_context_type =
472 bt_ctf_stream_class_get_packet_context_type(stream_class);
473 event_header_type =
474 bt_ctf_stream_class_get_event_header_type(stream_class);
475 stream_event_ctx_type =
476 bt_ctf_stream_class_get_event_context_type(stream_class);
477 ret = bt_ctf_validate_class_types(trace->environment,
478 packet_header_type, packet_context_type, event_header_type,
479 stream_event_ctx_type, NULL, NULL, trace->valid,
480 stream_class->valid, 1, &trace_sc_validation_output,
481 trace_sc_validation_flags);
482 BT_PUT(packet_header_type);
483 BT_PUT(packet_context_type);
484 BT_PUT(event_header_type);
485 BT_PUT(stream_event_ctx_type);
486
487 if (ret) {
488 /*
489 * This means something went wrong during the validation
490 * process, not that the objects are invalid.
491 */
492 goto end;
493 }
494
495 if ((trace_sc_validation_output.valid_flags &
496 trace_sc_validation_flags) !=
497 trace_sc_validation_flags) {
498 /* Invalid trace/stream class */
499 ret = -1;
500 goto end;
501 }
502
503 if (event_class_count > 0) {
504 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
505 event_class_count);
506 if (!ec_validation_outputs) {
507 ret = -1;
508 goto end;
509 }
510 }
511
512 /* Validate each event class individually */
513 for (i = 0; i < event_class_count; ++i) {
514 struct bt_ctf_event_class *event_class =
515 bt_ctf_stream_class_get_event_class(stream_class, i);
516 struct bt_ctf_field_type *event_context_type = NULL;
517 struct bt_ctf_field_type *event_payload_type = NULL;
518
519 event_context_type =
520 bt_ctf_event_class_get_context_type(event_class);
521 event_payload_type =
522 bt_ctf_event_class_get_payload_type(event_class);
523
524 /*
525 * It is important to use the field types returned by
526 * the previous trace and stream class validation here
527 * because copies could have been made.
528 */
529 ret = bt_ctf_validate_class_types(trace->environment,
530 trace_sc_validation_output.packet_header_type,
531 trace_sc_validation_output.packet_context_type,
532 trace_sc_validation_output.event_header_type,
533 trace_sc_validation_output.stream_event_ctx_type,
534 event_context_type, event_payload_type,
535 1, 1, event_class->valid, &ec_validation_outputs[i],
536 ec_validation_flags);
537 BT_PUT(event_context_type);
538 BT_PUT(event_payload_type);
539 BT_PUT(event_class);
540
541 if (ret) {
542 goto end;
543 }
544
545 if ((ec_validation_outputs[i].valid_flags &
546 ec_validation_flags) != ec_validation_flags) {
547 /* Invalid event class */
548 ret = -1;
549 goto end;
550 }
551 }
552
553 stream_id = bt_ctf_stream_class_get_id(stream_class);
554 if (stream_id < 0) {
555 stream_id = trace->next_stream_id++;
556
557 /* Try to assign a new stream id */
558 for (i = 0; i < trace->stream_classes->len; i++) {
559 if (stream_id == bt_ctf_stream_class_get_id(
560 trace->stream_classes->pdata[i])) {
561 /* Duplicate stream id found */
562 ret = -1;
563 goto end;
564 }
565 }
566
567 if (bt_ctf_stream_class_set_id_no_check(stream_class,
568 stream_id)) {
569 /* TODO Should retry with a different stream id */
570 ret = -1;
571 goto end;
572 }
573 }
574
575 bt_object_set_parent(stream_class, trace);
576 g_ptr_array_add(trace->stream_classes, stream_class);
577
578 /*
579 * At this point we know that the function will be successful.
580 * Therefore we can replace the trace and stream class field
581 * types with what's in their validation output structure and
582 * mark them as valid. We can also replace the field types of
583 * all the event classes of the stream class and mark them as
584 * valid.
585 */
586 bt_ctf_validation_replace_types(trace, stream_class, NULL,
587 &trace_sc_validation_output, trace_sc_validation_flags);
588 trace->valid = 1;
589 stream_class->valid = 1;
590
591 /*
592 * Put what was not moved in bt_ctf_validation_replace_types().
593 */
594 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
595
596 for (i = 0; i < event_class_count; ++i) {
597 struct bt_ctf_event_class *event_class =
598 bt_ctf_stream_class_get_event_class(stream_class, i);
599
600 bt_ctf_validation_replace_types(NULL, NULL, event_class,
601 &ec_validation_outputs[i], ec_validation_flags);
602 event_class->valid = 1;
603 BT_PUT(event_class);
604
605 /*
606 * Put what was not moved in
607 * bt_ctf_validation_replace_types().
608 */
609 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
610 }
611
612 /*
613 * All field type byte orders set as "native" byte ordering can now be
614 * safely set to trace's own endianness, including the stream class'.
615 */
616 bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
617 trace->byte_order);
618 bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order);
619
620 /*
621 * Freeze the trace and the stream class.
622 */
623 bt_ctf_stream_class_freeze(stream_class);
624 bt_ctf_trace_freeze(trace);
625
626 end:
627 if (ret) {
628 bt_object_set_parent(stream_class, NULL);
629
630 if (ec_validation_outputs) {
631 for (i = 0; i < event_class_count; ++i) {
632 bt_ctf_validation_output_put_types(
633 &ec_validation_outputs[i]);
634 }
635 }
636 }
637
638 g_free(ec_validation_outputs);
639 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
640 assert(!packet_header_type);
641 assert(!packet_context_type);
642 assert(!event_header_type);
643 assert(!stream_event_ctx_type);
644
645 return ret;
646 }
647
648 int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
649 {
650 int ret;
651
652 if (!trace) {
653 ret = -1;
654 goto end;
655 }
656
657 ret = trace->stream_classes->len;
658 end:
659 return ret;
660 }
661
662 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
663 struct bt_ctf_trace *trace, int index)
664 {
665 struct bt_ctf_stream_class *stream_class = NULL;
666
667 if (!trace || index < 0 || index >= trace->stream_classes->len) {
668 goto end;
669 }
670
671 stream_class = g_ptr_array_index(trace->stream_classes, index);
672 bt_get(stream_class);
673 end:
674 return stream_class;
675 }
676
677 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
678 struct bt_ctf_trace *trace, uint32_t id)
679 {
680 int i;
681 struct bt_ctf_stream_class *stream_class = NULL;
682
683 if (!trace) {
684 goto end;
685 }
686
687 for (i = 0; i < trace->stream_classes->len; ++i) {
688 struct bt_ctf_stream_class *stream_class_candidate;
689
690 stream_class_candidate =
691 g_ptr_array_index(trace->stream_classes, i);
692
693 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
694 (int64_t) id) {
695 stream_class = stream_class_candidate;
696 bt_get(stream_class);
697 goto end;
698 }
699 }
700
701 end:
702 return stream_class;
703 }
704
705 struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
706 struct bt_ctf_trace *trace, const char *name)
707 {
708 size_t i;
709 struct bt_ctf_clock *clock = NULL;
710
711 if (!trace || !name) {
712 goto end;
713 }
714
715 for (i = 0; i < trace->clocks->len; ++i) {
716 struct bt_ctf_clock *cur_clk =
717 g_ptr_array_index(trace->clocks, i);
718 const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk);
719
720 if (!cur_clk_name) {
721 goto end;
722 }
723
724 if (!strcmp(cur_clk_name, name)) {
725 clock = cur_clk;
726 bt_get(clock);
727 goto end;
728 }
729 }
730
731 end:
732 return clock;
733 }
734
735 BT_HIDDEN
736 const char *get_byte_order_string(int byte_order)
737 {
738 const char *string;
739
740 switch (byte_order) {
741 case LITTLE_ENDIAN:
742 string = "le";
743 break;
744 case BIG_ENDIAN:
745 string = "be";
746 break;
747 default:
748 string = "unknown";
749 break;
750 }
751
752 return string;
753 }
754
755 static
756 int append_trace_metadata(struct bt_ctf_trace *trace,
757 struct metadata_context *context)
758 {
759 unsigned char *uuid = trace->uuid;
760 int ret;
761
762 g_string_append(context->string, "trace {\n");
763
764 g_string_append(context->string, "\tmajor = 1;\n");
765 g_string_append(context->string, "\tminor = 8;\n");
766
767 g_string_append_printf(context->string,
768 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
769 uuid[0], uuid[1], uuid[2], uuid[3],
770 uuid[4], uuid[5], uuid[6], uuid[7],
771 uuid[8], uuid[9], uuid[10], uuid[11],
772 uuid[12], uuid[13], uuid[14], uuid[15]);
773 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
774 get_byte_order_string(trace->byte_order));
775
776 g_string_append(context->string, "\tpacket.header := ");
777 context->current_indentation_level++;
778 g_string_assign(context->field_name, "");
779 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
780 context);
781 if (ret) {
782 goto end;
783 }
784 context->current_indentation_level--;
785
786 g_string_append(context->string, ";\n};\n\n");
787 end:
788 return ret;
789 }
790
791 static
792 void append_env_metadata(struct bt_ctf_trace *trace,
793 struct metadata_context *context)
794 {
795 int i;
796 int env_size;
797
798 env_size = bt_ctf_attributes_get_count(trace->environment);
799
800 if (env_size <= 0) {
801 return;
802 }
803
804 g_string_append(context->string, "env {\n");
805
806 for (i = 0; i < env_size; ++i) {
807 struct bt_value *env_field_value_obj = NULL;
808 const char *entry_name;
809
810 entry_name = bt_ctf_attributes_get_field_name(
811 trace->environment, i);
812 env_field_value_obj = bt_ctf_attributes_get_field_value(
813 trace->environment, i);
814
815 if (!entry_name || !env_field_value_obj) {
816 goto loop_next;
817 }
818
819 switch (bt_value_get_type(env_field_value_obj)) {
820 case BT_VALUE_TYPE_INTEGER:
821 {
822 int ret;
823 int64_t int_value;
824
825 ret = bt_value_integer_get(env_field_value_obj,
826 &int_value);
827
828 if (ret) {
829 goto loop_next;
830 }
831
832 g_string_append_printf(context->string,
833 "\t%s = %" PRId64 ";\n", entry_name,
834 int_value);
835 break;
836 }
837 case BT_VALUE_TYPE_STRING:
838 {
839 int ret;
840 const char *str_value;
841 char *escaped_str = NULL;
842
843 ret = bt_value_string_get(env_field_value_obj,
844 &str_value);
845
846 if (ret) {
847 goto loop_next;
848 }
849
850 escaped_str = g_strescape(str_value, NULL);
851
852 if (!escaped_str) {
853 goto loop_next;
854 }
855
856 g_string_append_printf(context->string,
857 "\t%s = \"%s\";\n", entry_name, escaped_str);
858 free(escaped_str);
859 break;
860 }
861
862 default:
863 goto loop_next;
864 }
865
866 loop_next:
867 BT_PUT(env_field_value_obj);
868 }
869
870 g_string_append(context->string, "};\n\n");
871 }
872
873 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
874 {
875 char *metadata = NULL;
876 struct metadata_context *context = NULL;
877 int err = 0;
878 size_t i;
879
880 if (!trace) {
881 goto end;
882 }
883
884 context = g_new0(struct metadata_context, 1);
885 if (!context) {
886 goto end;
887 }
888
889 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
890 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
891 g_string_append(context->string, "/* CTF 1.8 */\n\n");
892 if (append_trace_metadata(trace, context)) {
893 goto error;
894 }
895 append_env_metadata(trace, context);
896 g_ptr_array_foreach(trace->clocks,
897 (GFunc)bt_ctf_clock_serialize, context);
898
899 for (i = 0; i < trace->stream_classes->len; i++) {
900 err = bt_ctf_stream_class_serialize(
901 trace->stream_classes->pdata[i], context);
902 if (err) {
903 goto error;
904 }
905 }
906
907 metadata = context->string->str;
908 error:
909 g_string_free(context->string, err ? TRUE : FALSE);
910 g_string_free(context->field_name, TRUE);
911 g_free(context);
912 end:
913 return metadata;
914 }
915
916 enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
917 {
918 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
919
920 if (!trace) {
921 goto end;
922 }
923
924 switch (trace->byte_order) {
925 case BIG_ENDIAN:
926 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
927 break;
928 case LITTLE_ENDIAN:
929 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
930 break;
931 default:
932 break;
933 }
934 end:
935 return ret;
936 }
937
938 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
939 enum bt_ctf_byte_order byte_order)
940 {
941 int ret = 0;
942 int internal_byte_order;
943
944 if (!trace || trace->frozen) {
945 ret = -1;
946 goto end;
947 }
948
949 switch (byte_order) {
950 case BT_CTF_BYTE_ORDER_NATIVE:
951 /*
952 * This doesn't make sense since the CTF specification defines
953 * the "native" byte order as "the byte order described in the
954 * trace description". However, this behavior had been
955 * implemented as part of v1.2 and is kept to maintain
956 * compatibility.
957 *
958 * This may be changed on a major version bump only.
959 */
960 internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ?
961 LITTLE_ENDIAN : BIG_ENDIAN;
962 break;
963 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
964 internal_byte_order = LITTLE_ENDIAN;
965 break;
966 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
967 case BT_CTF_BYTE_ORDER_NETWORK:
968 internal_byte_order = BIG_ENDIAN;
969 break;
970 default:
971 ret = -1;
972 goto end;
973 }
974
975 trace->byte_order = internal_byte_order;
976 end:
977 return ret;
978 }
979
980 struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
981 struct bt_ctf_trace *trace)
982 {
983 struct bt_ctf_field_type *field_type = NULL;
984
985 if (!trace) {
986 goto end;
987 }
988
989 bt_get(trace->packet_header_type);
990 field_type = trace->packet_header_type;
991 end:
992 return field_type;
993 }
994
995 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
996 struct bt_ctf_field_type *packet_header_type)
997 {
998 int ret = 0;
999
1000 if (!trace || !packet_header_type || trace->frozen) {
1001 ret = -1;
1002 goto end;
1003 }
1004
1005 /* packet_header_type must be a structure */
1006 if (bt_ctf_field_type_get_type_id(packet_header_type) !=
1007 BT_CTF_TYPE_ID_STRUCT) {
1008 ret = -1;
1009 goto end;
1010 }
1011
1012 bt_get(packet_header_type);
1013 bt_put(trace->packet_header_type);
1014 trace->packet_header_type = packet_header_type;
1015 end:
1016 return ret;
1017 }
1018
1019 void bt_ctf_trace_get(struct bt_ctf_trace *trace)
1020 {
1021 bt_get(trace);
1022 }
1023
1024 void bt_ctf_trace_put(struct bt_ctf_trace *trace)
1025 {
1026 bt_put(trace);
1027
1028 }
1029
1030 BT_HIDDEN
1031 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
1032 {
1033 unsigned int alignment, size;
1034 struct bt_ctf_field_type *field_type = NULL;
1035
1036 if (alias >= NR_FIELD_TYPE_ALIAS) {
1037 goto end;
1038 }
1039
1040 alignment = field_type_aliases_alignments[alias];
1041 size = field_type_aliases_sizes[alias];
1042 field_type = bt_ctf_field_type_integer_create(size);
1043 bt_ctf_field_type_set_alignment(field_type, alignment);
1044 end:
1045 return field_type;
1046 }
1047
1048 static
1049 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
1050 {
1051 bt_ctf_field_type_freeze(trace->packet_header_type);
1052 bt_ctf_attributes_freeze(trace->environment);
1053 trace->frozen = 1;
1054 }
1055
1056 static
1057 int init_trace_packet_header(struct bt_ctf_trace *trace)
1058 {
1059 int ret = 0;
1060 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
1061 struct bt_ctf_field_type *_uint32_t =
1062 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
1063 struct bt_ctf_field_type *_uint8_t =
1064 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1065 struct bt_ctf_field_type *trace_packet_header_type =
1066 bt_ctf_field_type_structure_create();
1067 struct bt_ctf_field_type *uuid_array_type =
1068 bt_ctf_field_type_array_create(_uint8_t, 16);
1069
1070 if (!trace_packet_header_type || !uuid_array_type) {
1071 ret = -1;
1072 goto end;
1073 }
1074
1075 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1076 _uint32_t, "magic");
1077 if (ret) {
1078 goto end;
1079 }
1080
1081 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1082 uuid_array_type, "uuid");
1083 if (ret) {
1084 goto end;
1085 }
1086
1087 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1088 _uint32_t, "stream_id");
1089 if (ret) {
1090 goto end;
1091 }
1092
1093 ret = bt_ctf_trace_set_packet_header_type(trace,
1094 trace_packet_header_type);
1095 if (ret) {
1096 goto end;
1097 }
1098 end:
1099 bt_put(uuid_array_type);
1100 bt_put(_uint32_t);
1101 bt_put(_uint8_t);
1102 bt_put(magic);
1103 bt_put(uuid_array);
1104 bt_put(trace_packet_header_type);
1105 return ret;
1106 }
This page took 0.062095 seconds and 5 git commands to generate.