4 * Babeltrace CTF IR - Trace
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_TAG "TRACE"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-ir/trace-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/stream-internal.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/event-class.h>
38 #include <babeltrace/ctf-ir/event-class-internal.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/ctf-writer/clock-internal.h>
41 #include <babeltrace/ctf-ir/field-types-internal.h>
42 #include <babeltrace/ctf-ir/attributes-internal.h>
43 #include <babeltrace/ctf-ir/validation-internal.h>
44 #include <babeltrace/ctf-ir/visitor-internal.h>
45 #include <babeltrace/ctf-ir/utils.h>
46 #include <babeltrace/compiler-internal.h>
47 #include <babeltrace/values.h>
48 #include <babeltrace/values-internal.h>
49 #include <babeltrace/ref.h>
50 #include <babeltrace/types.h>
51 #include <babeltrace/endian-internal.h>
57 #define DEFAULT_IDENTIFIER_SIZE 128
58 #define DEFAULT_METADATA_STRING_SIZE 4096
60 struct listener_wrapper
{
61 bt_ctf_listener_cb listener
;
65 struct bt_ctf_trace_is_static_listener_elem
{
66 bt_ctf_trace_is_static_listener func
;
71 void bt_ctf_trace_destroy(struct bt_object
*obj
);
73 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
76 const unsigned int field_type_aliases_alignments
[] = {
77 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
78 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
79 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
80 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
84 const unsigned int field_type_aliases_sizes
[] = {
85 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
86 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
87 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
88 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
89 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
90 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
93 struct bt_ctf_trace
*bt_ctf_trace_create(void)
95 struct bt_ctf_trace
*trace
= NULL
;
97 trace
= g_new0(struct bt_ctf_trace
, 1);
99 BT_LOGE_STR("Failed to allocate one trace.");
103 BT_LOGD_STR("Creating trace object.");
104 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_UNSPECIFIED
;
105 bt_object_init(trace
, bt_ctf_trace_destroy
);
106 trace
->clocks
= g_ptr_array_new_with_free_func(
107 (GDestroyNotify
) bt_put
);
108 if (!trace
->clocks
) {
109 BT_LOGE_STR("Failed to allocate one GPtrArray.");
113 trace
->streams
= g_ptr_array_new_with_free_func(
114 (GDestroyNotify
) bt_object_release
);
115 if (!trace
->streams
) {
116 BT_LOGE_STR("Failed to allocate one GPtrArray.");
120 trace
->stream_classes
= g_ptr_array_new_with_free_func(
121 (GDestroyNotify
) bt_object_release
);
122 if (!trace
->stream_classes
) {
123 BT_LOGE_STR("Failed to allocate one GPtrArray.");
127 /* Create the environment array object */
128 trace
->environment
= bt_ctf_attributes_create();
129 if (!trace
->environment
) {
130 BT_LOGE_STR("Cannot create empty attributes object.");
134 trace
->listeners
= g_ptr_array_new_with_free_func(
135 (GDestroyNotify
) g_free
);
136 if (!trace
->listeners
) {
137 BT_LOGE_STR("Failed to allocate one GPtrArray.");
141 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
142 sizeof(struct bt_ctf_trace_is_static_listener_elem
));
143 if (!trace
->is_static_listeners
) {
144 BT_LOGE_STR("Failed to allocate one GArray.");
148 BT_LOGD("Created trace object: addr=%p", trace
);
156 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
158 const char *name
= NULL
;
161 BT_LOGW_STR("Invalid parameter: trace is NULL.");
169 name
= trace
->name
->str
;
174 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
179 BT_LOGW_STR("Invalid parameter: trace is NULL.");
185 BT_LOGW_STR("Invalid parameter: name is NULL.");
191 BT_LOGW("Invalid parameter: trace is frozen: "
192 "addr=%p, name=\"%s\"",
193 trace
, bt_ctf_trace_get_name(trace
));
198 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
201 BT_LOGE_STR("Failed to allocate one GString.");
206 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
212 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
214 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
217 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
, const unsigned char *uuid
)
222 BT_LOGW_STR("Invalid parameter: trace is NULL.");
228 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
234 BT_LOGW("Invalid parameter: trace is frozen: "
235 "addr=%p, name=\"%s\"",
236 trace
, bt_ctf_trace_get_name(trace
));
241 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
242 trace
->uuid_set
= BT_TRUE
;
243 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
244 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
245 trace
, bt_ctf_trace_get_name(trace
),
246 (unsigned int) uuid
[0],
247 (unsigned int) uuid
[1],
248 (unsigned int) uuid
[2],
249 (unsigned int) uuid
[3],
250 (unsigned int) uuid
[4],
251 (unsigned int) uuid
[5],
252 (unsigned int) uuid
[6],
253 (unsigned int) uuid
[7],
254 (unsigned int) uuid
[8],
255 (unsigned int) uuid
[9],
256 (unsigned int) uuid
[10],
257 (unsigned int) uuid
[11],
258 (unsigned int) uuid
[12],
259 (unsigned int) uuid
[13],
260 (unsigned int) uuid
[14],
261 (unsigned int) uuid
[15]);
267 void bt_ctf_trace_destroy(struct bt_object
*obj
)
269 struct bt_ctf_trace
*trace
;
271 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
273 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
274 trace
, bt_ctf_trace_get_name(trace
));
276 if (trace
->environment
) {
277 BT_LOGD_STR("Destroying environment attributes.");
278 bt_ctf_attributes_destroy(trace
->environment
);
282 g_string_free(trace
->name
, TRUE
);
286 BT_LOGD_STR("Putting clock classes.");
287 g_ptr_array_free(trace
->clocks
, TRUE
);
290 if (trace
->streams
) {
291 BT_LOGD_STR("Destroying streams.");
292 g_ptr_array_free(trace
->streams
, TRUE
);
295 if (trace
->stream_classes
) {
296 BT_LOGD_STR("Destroying stream classes.");
297 g_ptr_array_free(trace
->stream_classes
, TRUE
);
300 if (trace
->listeners
) {
301 g_ptr_array_free(trace
->listeners
, TRUE
);
304 if (trace
->is_static_listeners
) {
305 g_array_free(trace
->is_static_listeners
, TRUE
);
308 BT_LOGD_STR("Putting packet header field type.");
309 bt_put(trace
->packet_header_type
);
313 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
314 const char *name
, struct bt_value
*value
)
319 BT_LOGW_STR("Invalid parameter: trace is NULL.");
325 BT_LOGW_STR("Invalid parameter: name is NULL.");
331 BT_LOGW_STR("Invalid parameter: value is NULL.");
336 if (bt_ctf_validate_identifier(name
)) {
337 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
338 "trace-addr=%p, trace-name=\"%s\", "
340 trace
, bt_ctf_trace_get_name(trace
), name
);
345 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
346 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
347 "trace-addr=%p, trace-name=\"%s\", "
348 "env-name=\"%s\", env-value-type=%s",
349 trace
, bt_ctf_trace_get_name(trace
), name
,
350 bt_value_type_string(bt_value_get_type(value
)));
355 if (trace
->is_static
) {
356 BT_LOGW("Invalid parameter: trace is static: "
357 "addr=%p, name=\"%s\"",
358 trace
, bt_ctf_trace_get_name(trace
));
365 * New environment fields may be added to a frozen trace,
366 * but existing fields may not be changed.
368 * The object passed is frozen like all other attributes.
370 struct bt_value
*attribute
=
371 bt_ctf_attributes_get_field_value_by_name(
372 trace
->environment
, name
);
375 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
376 "trace-addr=%p, trace-name=\"%s\", "
378 trace
, bt_ctf_trace_get_name(trace
), name
);
384 bt_value_freeze(value
);
387 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
390 BT_LOGE("Cannot set environment field's value: "
391 "trace-addr=%p, trace-name=\"%s\", "
393 trace
, bt_ctf_trace_get_name(trace
), name
);
395 BT_LOGV("Set environment field's value: "
396 "trace-addr=%p, trace-name=\"%s\", "
397 "env-name=\"%s\", value-addr=%p",
398 trace
, bt_ctf_trace_get_name(trace
), name
, value
);
405 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
406 const char *name
, const char *value
)
409 struct bt_value
*env_value_string_obj
= NULL
;
412 BT_LOGW_STR("Invalid parameter: value is NULL.");
417 env_value_string_obj
= bt_value_string_create_init(value
);
418 if (!env_value_string_obj
) {
419 BT_LOGE_STR("Cannot create string value object.");
424 /* bt_ctf_trace_set_environment_field() logs errors */
425 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
426 env_value_string_obj
);
429 bt_put(env_value_string_obj
);
433 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
434 const char *name
, int64_t value
)
437 struct bt_value
*env_value_integer_obj
= NULL
;
439 env_value_integer_obj
= bt_value_integer_create_init(value
);
440 if (!env_value_integer_obj
) {
441 BT_LOGE_STR("Cannot create integer value object.");
446 /* bt_ctf_trace_set_environment_field() logs errors */
447 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
448 env_value_integer_obj
);
451 bt_put(env_value_integer_obj
);
455 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
460 BT_LOGW_STR("Invalid parameter: trace is NULL.");
465 ret
= bt_ctf_attributes_get_count(trace
->environment
);
473 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
476 const char *ret
= NULL
;
479 BT_LOGW_STR("Invalid parameter: trace is NULL.");
483 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
489 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
490 struct bt_ctf_trace
*trace
, uint64_t index
)
492 struct bt_value
*ret
= NULL
;
495 BT_LOGW_STR("Invalid parameter: trace is NULL.");
499 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
505 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
506 struct bt_ctf_trace
*trace
, const char *name
)
508 struct bt_value
*ret
= NULL
;
511 BT_LOGW_STR("Invalid parameter: trace is NULL.");
516 BT_LOGW_STR("Invalid parameter: name is NULL.");
520 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
527 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
528 struct bt_ctf_clock_class
*clock_class
)
533 BT_LOGW_STR("Invalid parameter: trace is NULL.");
538 if (trace
->is_static
) {
539 BT_LOGW("Invalid parameter: trace is static: "
540 "addr=%p, name=\"%s\"",
541 trace
, bt_ctf_trace_get_name(trace
));
546 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
547 BT_LOGW("Invalid parameter: clock class is invalid: "
548 "trace-addr=%p, trace-name=\"%s\", "
549 "clock-class-addr=%p, clock-class-name=\"%s\"",
550 trace
, bt_ctf_trace_get_name(trace
),
551 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
556 /* Check for duplicate clock classes */
557 if (bt_ctf_trace_has_clock_class(trace
, clock_class
)) {
558 BT_LOGW("Invalid parameter: clock class already exists in trace: "
559 "trace-addr=%p, trace-name=\"%s\", "
560 "clock-class-addr=%p, clock-class-name=\"%s\"",
561 trace
, bt_ctf_trace_get_name(trace
),
562 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
568 g_ptr_array_add(trace
->clocks
, clock_class
);
571 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
572 bt_ctf_clock_class_freeze(clock_class
);
575 BT_LOGV("Added clock class to trace: "
576 "trace-addr=%p, trace-name=\"%s\", "
577 "clock-class-addr=%p, clock-class-name=\"%s\"",
578 trace
, bt_ctf_trace_get_name(trace
),
579 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
585 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
587 int64_t ret
= (int64_t) -1;
590 BT_LOGW_STR("Invalid parameter: trace is NULL.");
594 ret
= trace
->clocks
->len
;
599 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
600 struct bt_ctf_trace
*trace
, uint64_t index
)
602 struct bt_ctf_clock_class
*clock_class
= NULL
;
605 BT_LOGW_STR("Invalid parameter: trace is NULL.");
609 if (index
>= trace
->clocks
->len
) {
610 BT_LOGW("Invalid parameter: index is out of bounds: "
611 "addr=%p, name=\"%s\", "
612 "index=%" PRIu64
", count=%u",
613 trace
, bt_ctf_trace_get_name(trace
),
614 index
, trace
->clocks
->len
);
618 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
625 bool packet_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
626 struct bt_ctf_field_type
*packet_header_type
)
629 bool is_valid
= true;
630 struct bt_ctf_field_type
*field_type
= NULL
;
632 if (!packet_header_type
) {
634 * No packet header field type: trace must have only
635 * one stream. At this point the stream class being
636 * added is not part of the trace yet, so we validate
637 * that the trace contains no stream classes yet.
639 if (trace
->stream_classes
->len
>= 1) {
640 BT_LOGW_STR("Invalid packet header field type: "
641 "packet header field type does not exist but there's more than one stream class in the trace.");
645 /* No packet header field type: valid at this point */
649 /* Packet header field type, if it exists, must be a structure */
650 if (!bt_ctf_field_type_is_structure(packet_header_type
)) {
651 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
652 "ft-addr=%p, ft-id=%s",
654 bt_ctf_field_type_id_string(packet_header_type
->id
));
659 * If there's a `magic` field, it must be a 32-bit unsigned
660 * integer field type. Also it must be the first field of the
661 * packet header field type.
663 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
664 packet_header_type
, "magic");
666 const char *field_name
;
668 if (!bt_ctf_field_type_is_integer(field_type
)) {
669 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
670 "magic-ft-addr=%p, magic-ft-id=%s",
672 bt_ctf_field_type_id_string(field_type
->id
));
676 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
677 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
678 "magic-ft-addr=%p", field_type
);
682 if (bt_ctf_field_type_integer_get_size(field_type
) != 32) {
683 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
684 "magic-ft-addr=%p, magic-ft-size=%u",
686 bt_ctf_field_type_integer_get_size(field_type
));
690 ret
= bt_ctf_field_type_structure_get_field_by_index(
691 packet_header_type
, &field_name
, NULL
, 0);
694 if (strcmp(field_name
, "magic") != 0) {
695 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
696 "magic-ft-addr=%p, first-field-name=\"%s\"",
697 field_type
, field_name
);
705 * If there's a `uuid` field, it must be an array field type of
706 * length 16 with an 8-bit unsigned integer element field type.
708 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
709 packet_header_type
, "uuid");
711 struct bt_ctf_field_type
*elem_ft
;
713 if (!bt_ctf_field_type_is_array(field_type
)) {
714 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
715 "uuid-ft-addr=%p, uuid-ft-id=%s",
717 bt_ctf_field_type_id_string(field_type
->id
));
721 if (bt_ctf_field_type_array_get_length(field_type
) != 16) {
722 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
723 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
725 bt_ctf_field_type_array_get_length(field_type
));
729 elem_ft
= bt_ctf_field_type_array_get_element_type(field_type
);
732 if (!bt_ctf_field_type_is_integer(elem_ft
)) {
733 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
734 "elem-ft-addr=%p, elem-ft-id=%s",
736 bt_ctf_field_type_id_string(elem_ft
->id
));
741 if (bt_ctf_field_type_integer_is_signed(elem_ft
)) {
742 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
743 "elem-ft-addr=%p", elem_ft
);
748 if (bt_ctf_field_type_integer_get_size(elem_ft
) != 8) {
749 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
750 "elem-ft-addr=%p, elem-ft-size=%u",
752 bt_ctf_field_type_integer_get_size(elem_ft
));
762 * The `stream_id` field must exist if there's more than one
763 * stream classes in the trace.
765 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
766 packet_header_type
, "stream_id");
768 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
769 BT_LOGW_STR("Invalid packet header field type: "
770 "`stream_id` field does not exist but there's more than one stream class in the trace.");
775 * If there's a `stream_id` field, it must be an unsigned
776 * integer field type.
779 if (!bt_ctf_field_type_is_integer(field_type
)) {
780 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
781 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
783 bt_ctf_field_type_id_string(field_type
->id
));
787 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
788 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
789 "stream-id-ft-addr=%p", field_type
);
797 * If there's a `packet_seq_num` field, it must be an unsigned
798 * integer field type.
800 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
801 packet_header_type
, "packet_seq_num");
803 if (!bt_ctf_field_type_is_integer(field_type
)) {
804 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
805 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
807 bt_ctf_field_type_id_string(field_type
->id
));
811 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
812 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
813 "packet-seq-num-ft-addr=%p", field_type
);
831 bool packet_context_field_type_is_valid(struct bt_ctf_trace
*trace
,
832 struct bt_ctf_stream_class
*stream_class
,
833 struct bt_ctf_field_type
*packet_context_type
)
835 bool is_valid
= true;
836 struct bt_ctf_field_type
*field_type
= NULL
;
838 if (!packet_context_type
) {
839 /* No packet context field type: valid at this point */
843 /* Packet context field type, if it exists, must be a structure */
844 if (!bt_ctf_field_type_is_structure(packet_context_type
)) {
845 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
846 "ft-addr=%p, ft-id=%s",
848 bt_ctf_field_type_id_string(packet_context_type
->id
));
853 * If there's a `packet_size` field, it must be an unsigned
854 * integer field type.
856 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
857 packet_context_type
, "packet_size");
859 if (!bt_ctf_field_type_is_integer(field_type
)) {
860 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
861 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
863 bt_ctf_field_type_id_string(field_type
->id
));
867 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
868 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
869 "packet-size-ft-addr=%p", field_type
);
877 * If there's a `content_size` field, it must be an unsigned
878 * integer field type.
880 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
881 packet_context_type
, "content_size");
883 if (!bt_ctf_field_type_is_integer(field_type
)) {
884 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
885 "content-size-ft-addr=%p, content-size-ft-id=%s",
887 bt_ctf_field_type_id_string(field_type
->id
));
891 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
892 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
893 "content-size-ft-addr=%p", field_type
);
901 * If there's a `events_discarded` field, it must be an unsigned
902 * integer field type.
904 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
905 packet_context_type
, "events_discarded");
907 if (!bt_ctf_field_type_is_integer(field_type
)) {
908 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
909 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
911 bt_ctf_field_type_id_string(field_type
->id
));
915 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
916 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
917 "events-discarded-ft-addr=%p", field_type
);
925 * If there's a `timestamp_begin` field, it must be an unsigned
926 * integer field type. Also, if the trace is not a CTF writer's
927 * trace, then we cannot automatically set the mapped clock
928 * class of this field, so it must have a mapped clock class.
930 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
931 packet_context_type
, "timestamp_begin");
933 if (!bt_ctf_field_type_is_integer(field_type
)) {
934 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
935 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
937 bt_ctf_field_type_id_string(field_type
->id
));
941 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
942 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
943 "timestamp-begin-ft-addr=%p", field_type
);
947 if (!trace
->is_created_by_writer
) {
948 struct bt_ctf_clock_class
*clock_class
=
949 bt_ctf_field_type_integer_get_mapped_clock_class(
954 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
955 "timestamp-begin-ft-addr=%p", field_type
);
964 * If there's a `timestamp_end` field, it must be an unsigned
965 * integer field type. Also, if the trace is not a CTF writer's
966 * trace, then we cannot automatically set the mapped clock
967 * class of this field, so it must have a mapped clock class.
969 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
970 packet_context_type
, "timestamp_end");
972 if (!bt_ctf_field_type_is_integer(field_type
)) {
973 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
974 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
976 bt_ctf_field_type_id_string(field_type
->id
));
980 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
981 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
982 "timestamp-end-ft-addr=%p", field_type
);
986 if (!trace
->is_created_by_writer
) {
987 struct bt_ctf_clock_class
*clock_class
=
988 bt_ctf_field_type_integer_get_mapped_clock_class(
993 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
994 "timestamp-end-ft-addr=%p", field_type
);
1013 bool event_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
1014 struct bt_ctf_stream_class
*stream_class
,
1015 struct bt_ctf_field_type
*event_header_type
)
1017 bool is_valid
= true;
1018 struct bt_ctf_field_type
*field_type
= NULL
;
1021 * We do not validate that the `timestamp` field exists here
1022 * because CTF does not require this exact name to be mapped to
1026 if (!event_header_type
) {
1028 * No event header field type: stream class must have
1029 * only one event class.
1031 if (bt_ctf_stream_class_get_event_class_count(stream_class
) > 1) {
1032 BT_LOGW_STR("Invalid event header field type: "
1033 "event header field type does not exist but there's more than one event class in the stream class.");
1037 /* No event header field type: valid at this point */
1041 /* Event header field type, if it exists, must be a structure */
1042 if (!bt_ctf_field_type_is_structure(event_header_type
)) {
1043 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1044 "ft-addr=%p, ft-id=%s",
1046 bt_ctf_field_type_id_string(event_header_type
->id
));
1051 * If there's an `id` field, it must be an unsigned integer
1052 * field type or an enumeration field type with an unsigned
1053 * integer container field type.
1055 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1056 event_header_type
, "id");
1058 struct bt_ctf_field_type
*int_ft
;
1060 if (bt_ctf_field_type_is_integer(field_type
)) {
1061 int_ft
= bt_get(field_type
);
1062 } else if (bt_ctf_field_type_is_enumeration(field_type
)) {
1063 int_ft
= bt_ctf_field_type_enumeration_get_container_type(
1066 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1067 "id-ft-addr=%p, id-ft-id=%s",
1069 bt_ctf_field_type_id_string(field_type
->id
));
1074 if (bt_ctf_field_type_integer_is_signed(int_ft
)) {
1075 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1076 "id-ft-addr=%p", int_ft
);
1094 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1095 struct bt_ctf_stream_class
*stream_class
)
1100 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
1101 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
1102 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
1103 BT_CTF_VALIDATION_FLAG_TRACE
|
1104 BT_CTF_VALIDATION_FLAG_STREAM
;
1105 const enum bt_ctf_validation_flag ec_validation_flags
=
1106 BT_CTF_VALIDATION_FLAG_EVENT
;
1107 struct bt_ctf_field_type
*packet_header_type
= NULL
;
1108 struct bt_ctf_field_type
*packet_context_type
= NULL
;
1109 struct bt_ctf_field_type
*event_header_type
= NULL
;
1110 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
1111 int64_t event_class_count
;
1112 struct bt_ctf_trace
*current_parent_trace
= NULL
;
1115 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1120 if (!stream_class
) {
1121 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1126 if (trace
->is_static
) {
1127 BT_LOGW_STR("Invalid parameter: trace is static.");
1132 BT_LOGD("Adding stream class to trace: "
1133 "trace-addr=%p, trace-name=\"%s\", "
1134 "stream-class-addr=%p, stream-class-name=\"%s\", "
1135 "stream-class-id=%" PRId64
,
1136 trace
, bt_ctf_trace_get_name(trace
),
1137 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1138 bt_ctf_stream_class_get_id(stream_class
));
1140 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
1141 if (current_parent_trace
) {
1142 /* Stream class is already associated to a trace, abort. */
1143 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1144 "stream-class-trace-addr=%p, "
1145 "stream-class-trace-name=\"%s\"",
1146 current_parent_trace
,
1147 bt_ctf_trace_get_name(current_parent_trace
));
1153 bt_ctf_stream_class_get_event_class_count(stream_class
);
1154 assert(event_class_count
>= 0);
1156 if (stream_class
->clock
) {
1157 struct bt_ctf_clock_class
*stream_clock_class
=
1158 stream_class
->clock
->clock_class
;
1160 if (trace
->is_created_by_writer
) {
1162 * Make sure this clock was also added to the
1163 * trace (potentially through its CTF writer
1168 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1169 if (trace
->clocks
->pdata
[i
] ==
1170 stream_clock_class
) {
1176 if (i
== trace
->clocks
->len
) {
1178 BT_LOGW("Stream class's clock's class is not part of the trace: "
1179 "clock-class-addr=%p, clock-class-name=\"%s\"",
1181 bt_ctf_clock_class_get_name(stream_clock_class
));
1187 * This trace was NOT created by a CTF writer,
1188 * thus do not allow the stream class to add to
1189 * have a clock at all. Those are two
1190 * independent APIs (non-writer and writer
1191 * APIs), and isolating them simplifies things.
1193 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1194 "clock-class-addr=%p, clock-class-name=\"%s\"",
1196 bt_ctf_clock_class_get_name(stream_clock_class
));
1203 * We're about to freeze both the trace and the stream class.
1204 * Also, each event class contained in this stream class are
1207 * This trace, this stream class, and all its event classes
1208 * should be valid at this point.
1210 * Validate trace and stream class first, then each event
1211 * class of this stream class can be validated individually.
1213 packet_header_type
=
1214 bt_ctf_trace_get_packet_header_type(trace
);
1215 packet_context_type
=
1216 bt_ctf_stream_class_get_packet_context_type(stream_class
);
1218 bt_ctf_stream_class_get_event_header_type(stream_class
);
1219 stream_event_ctx_type
=
1220 bt_ctf_stream_class_get_event_context_type(stream_class
);
1222 BT_LOGD("Validating trace and stream class field types.");
1223 ret
= bt_ctf_validate_class_types(trace
->environment
,
1224 packet_header_type
, packet_context_type
, event_header_type
,
1225 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1226 stream_class
->valid
, 1, &trace_sc_validation_output
,
1227 trace_sc_validation_flags
);
1228 BT_PUT(packet_header_type
);
1229 BT_PUT(packet_context_type
);
1230 BT_PUT(event_header_type
);
1231 BT_PUT(stream_event_ctx_type
);
1235 * This means something went wrong during the validation
1236 * process, not that the objects are invalid.
1238 BT_LOGE("Failed to validate trace and stream class field types: "
1243 if ((trace_sc_validation_output
.valid_flags
&
1244 trace_sc_validation_flags
) !=
1245 trace_sc_validation_flags
) {
1246 /* Invalid trace/stream class */
1247 BT_LOGW("Invalid trace or stream class field types: "
1249 trace_sc_validation_output
.valid_flags
);
1254 if (event_class_count
> 0) {
1255 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1257 if (!ec_validation_outputs
) {
1258 BT_LOGE_STR("Failed to allocate one validation output structure.");
1264 /* Validate each event class individually */
1265 for (i
= 0; i
< event_class_count
; i
++) {
1266 struct bt_ctf_event_class
*event_class
=
1267 bt_ctf_stream_class_get_event_class_by_index(
1269 struct bt_ctf_field_type
*event_context_type
= NULL
;
1270 struct bt_ctf_field_type
*event_payload_type
= NULL
;
1272 event_context_type
=
1273 bt_ctf_event_class_get_context_type(event_class
);
1274 event_payload_type
=
1275 bt_ctf_event_class_get_payload_type(event_class
);
1278 * It is important to use the field types returned by
1279 * the previous trace and stream class validation here
1280 * because copies could have been made.
1282 BT_LOGD("Validating event class's field types: "
1283 "addr=%p, name=\"%s\", id=%" PRId64
,
1284 event_class
, bt_ctf_event_class_get_name(event_class
),
1285 bt_ctf_event_class_get_id(event_class
));
1286 ret
= bt_ctf_validate_class_types(trace
->environment
,
1287 trace_sc_validation_output
.packet_header_type
,
1288 trace_sc_validation_output
.packet_context_type
,
1289 trace_sc_validation_output
.event_header_type
,
1290 trace_sc_validation_output
.stream_event_ctx_type
,
1291 event_context_type
, event_payload_type
,
1292 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1293 ec_validation_flags
);
1294 BT_PUT(event_context_type
);
1295 BT_PUT(event_payload_type
);
1296 BT_PUT(event_class
);
1299 BT_LOGE("Failed to validate event class field types: "
1304 if ((ec_validation_outputs
[i
].valid_flags
&
1305 ec_validation_flags
) != ec_validation_flags
) {
1306 /* Invalid event class */
1307 BT_LOGW("Invalid event class field types: "
1309 ec_validation_outputs
[i
].valid_flags
);
1315 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
1316 if (stream_id
< 0) {
1317 stream_id
= trace
->next_stream_id
++;
1318 if (stream_id
< 0) {
1319 BT_LOGE_STR("No more stream class IDs available.");
1324 /* Try to assign a new stream id */
1325 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1326 if (stream_id
== bt_ctf_stream_class_get_id(
1327 trace
->stream_classes
->pdata
[i
])) {
1328 /* Duplicate stream id found */
1329 BT_LOGW("Duplicate stream class ID: "
1330 "id=%" PRId64
, (int64_t) stream_id
);
1336 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
1338 /* TODO Should retry with a different stream id */
1339 BT_LOGE("Cannot set stream class's ID: "
1340 "id=%" PRId64
, (int64_t) stream_id
);
1347 * At this point all the field types in the validation output
1348 * are valid. Validate the semantics of some scopes according to
1349 * the CTF specification.
1351 if (!packet_header_field_type_is_valid(trace
,
1352 trace_sc_validation_output
.packet_header_type
)) {
1353 BT_LOGW_STR("Invalid trace's packet header field type.");
1358 if (!packet_context_field_type_is_valid(trace
,
1360 trace_sc_validation_output
.packet_context_type
)) {
1361 BT_LOGW_STR("Invalid stream class's packet context field type.");
1366 if (!event_header_field_type_is_valid(trace
,
1368 trace_sc_validation_output
.event_header_type
)) {
1369 BT_LOGW_STR("Invalid steam class's event header field type.");
1375 * Now is the time to automatically map specific field types of
1376 * the stream class's packet context and event header field
1377 * types to the stream class's clock's class if they are not
1378 * mapped to a clock class yet. We do it here because we know
1379 * that after this point, everything is frozen so it won't be
1380 * possible for the user to modify the stream class's clock, or
1381 * to map those field types to other clock classes.
1383 if (trace
->is_created_by_writer
) {
1384 if (bt_ctf_stream_class_map_clock_class(stream_class
,
1385 trace_sc_validation_output
.packet_context_type
,
1386 trace_sc_validation_output
.event_header_type
)) {
1387 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1393 bt_object_set_parent(stream_class
, trace
);
1394 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1397 * At this point we know that the function will be successful.
1398 * Therefore we can replace the trace and stream class field
1399 * types with what's in their validation output structure and
1400 * mark them as valid. We can also replace the field types of
1401 * all the event classes of the stream class and mark them as
1404 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1405 &trace_sc_validation_output
, trace_sc_validation_flags
);
1407 stream_class
->valid
= 1;
1410 * Put what was not moved in bt_ctf_validation_replace_types().
1412 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1414 for (i
= 0; i
< event_class_count
; i
++) {
1415 struct bt_ctf_event_class
*event_class
=
1416 bt_ctf_stream_class_get_event_class_by_index(
1419 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1420 &ec_validation_outputs
[i
], ec_validation_flags
);
1421 event_class
->valid
= 1;
1422 BT_PUT(event_class
);
1425 * Put what was not moved in
1426 * bt_ctf_validation_replace_types().
1428 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1432 * Freeze the trace and the stream class.
1434 bt_ctf_stream_class_freeze(stream_class
);
1435 bt_ctf_trace_freeze(trace
);
1437 /* Notifiy listeners of the trace's schema modification. */
1438 bt_ctf_stream_class_visit(stream_class
,
1439 bt_ctf_trace_object_modification
, trace
);
1440 BT_LOGD("Added stream class to trace: "
1441 "trace-addr=%p, trace-name=\"%s\", "
1442 "stream-class-addr=%p, stream-class-name=\"%s\", "
1443 "stream-class-id=%" PRId64
,
1444 trace
, bt_ctf_trace_get_name(trace
),
1445 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1446 bt_ctf_stream_class_get_id(stream_class
));
1450 bt_object_set_parent(stream_class
, NULL
);
1452 if (ec_validation_outputs
) {
1453 for (i
= 0; i
< event_class_count
; i
++) {
1454 bt_ctf_validation_output_put_types(
1455 &ec_validation_outputs
[i
]);
1460 g_free(ec_validation_outputs
);
1461 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1462 bt_put(current_parent_trace
);
1463 assert(!packet_header_type
);
1464 assert(!packet_context_type
);
1465 assert(!event_header_type
);
1466 assert(!stream_event_ctx_type
);
1470 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1475 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1480 ret
= (int64_t) trace
->streams
->len
;
1486 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1487 struct bt_ctf_trace
*trace
,
1490 struct bt_ctf_stream
*stream
= NULL
;
1493 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1497 if (index
>= trace
->streams
->len
) {
1498 BT_LOGW("Invalid parameter: index is out of bounds: "
1499 "addr=%p, name=\"%s\", "
1500 "index=%" PRIu64
", count=%u",
1501 trace
, bt_ctf_trace_get_name(trace
),
1502 index
, trace
->streams
->len
);
1506 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1512 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1517 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1522 ret
= (int64_t) trace
->stream_classes
->len
;
1527 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1528 struct bt_ctf_trace
*trace
, uint64_t index
)
1530 struct bt_ctf_stream_class
*stream_class
= NULL
;
1533 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1537 if (index
>= trace
->stream_classes
->len
) {
1538 BT_LOGW("Invalid parameter: index is out of bounds: "
1539 "addr=%p, name=\"%s\", "
1540 "index=%" PRIu64
", count=%u",
1541 trace
, bt_ctf_trace_get_name(trace
),
1542 index
, trace
->stream_classes
->len
);
1546 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1547 bt_get(stream_class
);
1549 return stream_class
;
1552 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1553 struct bt_ctf_trace
*trace
, uint64_t id_param
)
1556 struct bt_ctf_stream_class
*stream_class
= NULL
;
1557 int64_t id
= (int64_t) id_param
;
1560 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1565 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1566 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64
,
1567 trace
, bt_ctf_trace_get_name(trace
), id_param
);
1571 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1572 struct bt_ctf_stream_class
*stream_class_candidate
;
1574 stream_class_candidate
=
1575 g_ptr_array_index(trace
->stream_classes
, i
);
1577 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
1579 stream_class
= stream_class_candidate
;
1580 bt_get(stream_class
);
1586 return stream_class
;
1589 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1590 struct bt_ctf_trace
*trace
, const char *name
)
1593 struct bt_ctf_clock_class
*clock_class
= NULL
;
1596 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1601 BT_LOGW_STR("Invalid parameter: name is NULL.");
1605 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1606 struct bt_ctf_clock_class
*cur_clk
=
1607 g_ptr_array_index(trace
->clocks
, i
);
1608 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
1610 if (!cur_clk_name
) {
1614 if (!strcmp(cur_clk_name
, name
)) {
1615 clock_class
= cur_clk
;
1616 bt_get(clock_class
);
1626 bt_bool
bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
1627 struct bt_ctf_clock_class
*clock_class
)
1629 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1632 assert(clock_class
);
1634 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1639 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
1643 switch (byte_order
) {
1644 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1647 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1650 case BT_CTF_BYTE_ORDER_NATIVE
:
1661 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1662 struct metadata_context
*context
)
1664 unsigned char *uuid
= trace
->uuid
;
1667 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1668 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1669 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1670 "set it with bt_ctf_trace_set_native_byte_order(): "
1671 "addr=%p, name=\"%s\"",
1672 trace
, bt_ctf_trace_get_name(trace
));
1677 g_string_append(context
->string
, "trace {\n");
1678 g_string_append(context
->string
, "\tmajor = 1;\n");
1679 g_string_append(context
->string
, "\tminor = 8;\n");
1680 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1681 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1682 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1684 if (trace
->uuid_set
) {
1685 g_string_append_printf(context
->string
,
1686 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1687 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1688 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1689 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1690 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1693 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1694 get_byte_order_string(trace
->native_byte_order
));
1696 if (trace
->packet_header_type
) {
1697 g_string_append(context
->string
, "\tpacket.header := ");
1698 context
->current_indentation_level
++;
1699 g_string_assign(context
->field_name
, "");
1700 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1701 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
1706 context
->current_indentation_level
--;
1709 g_string_append(context
->string
, ";\n};\n\n");
1715 void append_env_metadata(struct bt_ctf_trace
*trace
,
1716 struct metadata_context
*context
)
1721 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
1722 if (env_size
<= 0) {
1726 g_string_append(context
->string
, "env {\n");
1728 for (i
= 0; i
< env_size
; i
++) {
1729 struct bt_value
*env_field_value_obj
= NULL
;
1730 const char *entry_name
;
1732 entry_name
= bt_ctf_attributes_get_field_name(
1733 trace
->environment
, i
);
1734 env_field_value_obj
= bt_ctf_attributes_get_field_value(
1735 trace
->environment
, i
);
1738 assert(env_field_value_obj
);
1740 switch (bt_value_get_type(env_field_value_obj
)) {
1741 case BT_VALUE_TYPE_INTEGER
:
1746 ret
= bt_value_integer_get(env_field_value_obj
,
1749 g_string_append_printf(context
->string
,
1750 "\t%s = %" PRId64
";\n", entry_name
,
1754 case BT_VALUE_TYPE_STRING
:
1757 const char *str_value
;
1758 char *escaped_str
= NULL
;
1760 ret
= bt_value_string_get(env_field_value_obj
,
1763 escaped_str
= g_strescape(str_value
, NULL
);
1765 BT_LOGE("Cannot escape string: string=\"%s\"",
1770 g_string_append_printf(context
->string
,
1771 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1780 BT_PUT(env_field_value_obj
);
1783 g_string_append(context
->string
, "};\n\n");
1786 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1788 char *metadata
= NULL
;
1789 struct metadata_context
*context
= NULL
;
1794 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1798 context
= g_new0(struct metadata_context
, 1);
1800 BT_LOGE_STR("Failed to allocate one metadata context.");
1804 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1805 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1806 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1807 if (append_trace_metadata(trace
, context
)) {
1808 /* append_trace_metadata() logs errors */
1811 append_env_metadata(trace
, context
);
1812 g_ptr_array_foreach(trace
->clocks
,
1813 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1815 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1816 /* bt_ctf_stream_class_serialize() logs details */
1817 err
= bt_ctf_stream_class_serialize(
1818 trace
->stream_classes
->pdata
[i
], context
);
1820 /* bt_ctf_stream_class_serialize() logs errors */
1825 metadata
= context
->string
->str
;
1828 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1829 g_string_free(context
->field_name
, TRUE
);
1836 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1837 struct bt_ctf_trace
*trace
)
1839 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1842 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1846 ret
= trace
->native_byte_order
;
1852 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1853 enum bt_ctf_byte_order byte_order
)
1858 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1863 if (trace
->frozen
) {
1864 BT_LOGW("Invalid parameter: trace is frozen: "
1865 "addr=%p, name=\"%s\"",
1866 trace
, bt_ctf_trace_get_name(trace
));
1871 if (trace
->is_created_by_writer
&&
1872 byte_order
== BT_CTF_BYTE_ORDER_UNSPECIFIED
) {
1873 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
1874 "addr=%p, name=\"%s\"",
1875 trace
, bt_ctf_trace_get_name(trace
));
1880 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1881 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1882 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1883 BT_LOGW("Invalid parameter: invalid byte order: "
1884 "addr=%p, name=\"%s\", bo=%s",
1885 trace
, bt_ctf_trace_get_name(trace
),
1886 bt_ctf_byte_order_string(byte_order
));
1891 trace
->native_byte_order
= byte_order
;
1892 BT_LOGV("Set trace's native byte order: "
1893 "addr=%p, name=\"%s\", bo=%s",
1894 trace
, bt_ctf_trace_get_name(trace
),
1895 bt_ctf_byte_order_string(byte_order
));
1901 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1902 struct bt_ctf_trace
*trace
)
1904 struct bt_ctf_field_type
*field_type
= NULL
;
1907 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1911 bt_get(trace
->packet_header_type
);
1912 field_type
= trace
->packet_header_type
;
1917 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1918 struct bt_ctf_field_type
*packet_header_type
)
1923 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1928 if (trace
->frozen
) {
1929 BT_LOGW("Invalid parameter: trace is frozen: "
1930 "addr=%p, name=\"%s\"",
1931 trace
, bt_ctf_trace_get_name(trace
));
1936 /* packet_header_type must be a structure. */
1937 if (packet_header_type
&&
1938 !bt_ctf_field_type_is_structure(packet_header_type
)) {
1939 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1940 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1941 trace
, bt_ctf_trace_get_name(trace
),
1943 bt_ctf_field_type_id_string(packet_header_type
->id
));
1948 bt_put(trace
->packet_header_type
);
1949 trace
->packet_header_type
= bt_get(packet_header_type
);
1950 BT_LOGV("Set trace's packet header field type: "
1951 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1952 trace
, bt_ctf_trace_get_name(trace
), packet_header_type
);
1958 int64_t get_stream_class_count(void *element
)
1960 return bt_ctf_trace_get_stream_class_count(
1961 (struct bt_ctf_trace
*) element
);
1965 void *get_stream_class(void *element
, int i
)
1967 return bt_ctf_trace_get_stream_class_by_index(
1968 (struct bt_ctf_trace
*) element
, i
);
1972 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1974 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1977 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1978 bt_ctf_visitor visitor
, void *data
)
1981 struct bt_ctf_object obj
=
1982 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1985 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1991 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1996 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1997 trace
, bt_ctf_trace_get_name(trace
));
1998 ret
= visitor_helper(&obj
, get_stream_class_count
,
1999 get_stream_class
, visit_stream_class
, visitor
, data
);
2005 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
2007 struct listener_wrapper
*listener_wrapper
= data
;
2009 listener_wrapper
->listener(object
, listener_wrapper
->data
);
2013 // TODO: add logging to this function once we use it internally.
2014 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
2015 bt_ctf_listener_cb listener
, void *listener_data
)
2018 struct listener_wrapper
*listener_wrapper
=
2019 g_new0(struct listener_wrapper
, 1);
2021 if (!trace
|| !listener
|| !listener_wrapper
) {
2026 listener_wrapper
->listener
= listener
;
2027 listener_wrapper
->data
= listener_data
;
2029 /* Visit the current schema. */
2030 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2036 * Add listener to the array of callbacks which will be invoked on
2039 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2042 g_free(listener_wrapper
);
2047 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
2051 struct bt_ctf_trace
*trace
= trace_ptr
;
2056 if (trace
->listeners
->len
== 0) {
2060 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2061 struct listener_wrapper
*listener
=
2062 g_ptr_array_index(trace
->listeners
, i
);
2064 listener
->listener(object
, listener
->data
);
2071 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
2074 unsigned int alignment
, size
;
2075 struct bt_ctf_field_type
*field_type
= NULL
;
2077 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2081 alignment
= field_type_aliases_alignments
[alias
];
2082 size
= field_type_aliases_sizes
[alias
];
2083 field_type
= bt_ctf_field_type_integer_create(size
);
2084 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
2093 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
2097 if (trace
->frozen
) {
2101 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2102 trace
, bt_ctf_trace_get_name(trace
));
2103 BT_LOGD_STR("Freezing packet header field type.");
2104 bt_ctf_field_type_freeze(trace
->packet_header_type
);
2105 BT_LOGD_STR("Freezing environment attributes.");
2106 bt_ctf_attributes_freeze(trace
->environment
);
2108 if (trace
->clocks
->len
> 0) {
2109 BT_LOGD_STR("Freezing clock classes.");
2112 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2113 struct bt_ctf_clock_class
*clock_class
=
2114 g_ptr_array_index(trace
->clocks
, i
);
2116 bt_ctf_clock_class_freeze(clock_class
);
2122 bt_bool
bt_ctf_trace_is_static(struct bt_ctf_trace
*trace
)
2124 bt_bool is_static
= BT_FALSE
;
2127 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2131 is_static
= trace
->is_static
;
2137 int bt_ctf_trace_set_is_static(struct bt_ctf_trace
*trace
)
2143 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2148 trace
->is_static
= BT_TRUE
;
2149 bt_ctf_trace_freeze(trace
);
2150 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2151 trace
, bt_ctf_trace_get_name(trace
));
2153 /* Call all the "trace is static" listeners */
2154 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2155 struct bt_ctf_trace_is_static_listener_elem elem
=
2156 g_array_index(trace
->is_static_listeners
,
2157 struct bt_ctf_trace_is_static_listener_elem
, i
);
2160 elem
.func(trace
, elem
.data
);
2168 int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace
*trace
,
2169 bt_ctf_trace_is_static_listener listener
, void *data
)
2172 struct bt_ctf_trace_is_static_listener_elem new_elem
= {
2178 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2184 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2189 if (trace
->is_static
) {
2190 BT_LOGW("Invalid parameter: trace is already static: "
2191 "addr=%p, name=\"%s\"",
2192 trace
, bt_ctf_trace_get_name(trace
));
2197 /* Find the next available spot */
2198 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2199 struct bt_ctf_trace_is_static_listener_elem elem
=
2200 g_array_index(trace
->is_static_listeners
,
2201 struct bt_ctf_trace_is_static_listener_elem
, i
);
2208 if (i
== trace
->is_static_listeners
->len
) {
2209 g_array_append_val(trace
->is_static_listeners
, new_elem
);
2211 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
2214 BT_LOGV("Added \"trace is static\" listener: "
2215 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2216 "data-addr=%p, listener-id=%d",
2217 trace
, bt_ctf_trace_get_name(trace
), listener
, data
, i
);
2223 int bt_ctf_trace_remove_is_static_listener(
2224 struct bt_ctf_trace
*trace
, int listener_id
)
2227 struct bt_ctf_trace_is_static_listener_elem
*elem
;
2230 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2235 if (listener_id
< 0) {
2236 BT_LOGW("Invalid listener ID: must be zero or positive: "
2237 "listener-id=%d", listener_id
);
2242 if (listener_id
>= trace
->is_static_listeners
->len
) {
2243 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2244 "addr=%p, name=\"%s\", listener-id=%d",
2245 trace
, bt_ctf_trace_get_name(trace
),
2251 elem
= &g_array_index(trace
->is_static_listeners
,
2252 struct bt_ctf_trace_is_static_listener_elem
,
2255 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2256 "addr=%p, name=\"%s\", listener-id=%d",
2257 trace
, bt_ctf_trace_get_name(trace
),
2265 BT_LOGV("Removed \"trace is static\" listener: "
2266 "trace-addr=%p, trace-name=\"%s\", "
2267 "listener-id=%d", trace
, bt_ctf_trace_get_name(trace
),