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