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_NONE
;
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
, sizeof(uuid_t
));
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
);
807 bool packet_context_field_type_is_valid(struct bt_ctf_trace
*trace
,
808 struct bt_ctf_stream_class
*stream_class
,
809 struct bt_ctf_field_type
*packet_context_type
)
811 bool is_valid
= true;
812 struct bt_ctf_field_type
*field_type
= NULL
;
814 if (!packet_context_type
) {
815 /* No packet context field type: valid at this point */
819 /* Packet context field type, if it exists, must be a structure */
820 if (!bt_ctf_field_type_is_structure(packet_context_type
)) {
821 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
822 "ft-addr=%p, ft-id=%s",
824 bt_ctf_field_type_id_string(packet_context_type
->id
));
829 * If there's a `packet_size` field, it must be an unsigned
830 * integer field type.
832 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
833 packet_context_type
, "packet_size");
835 if (!bt_ctf_field_type_is_integer(field_type
)) {
836 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
837 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
839 bt_ctf_field_type_id_string(field_type
->id
));
843 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
844 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
845 "packet-size-ft-addr=%p", field_type
);
853 * If there's a `content_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
, "content_size");
859 if (!bt_ctf_field_type_is_integer(field_type
)) {
860 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
861 "content-size-ft-addr=%p, content-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: `content_size` field must be an unsigned integer field type: "
869 "content-size-ft-addr=%p", field_type
);
877 * If there's a `events_discarded` 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
, "events_discarded");
883 if (!bt_ctf_field_type_is_integer(field_type
)) {
884 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
885 "events-discarded-ft-addr=%p, events-discarded-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: `events_discarded` field must be an unsigned integer field type: "
893 "events-discarded-ft-addr=%p", field_type
);
901 * If there's a `timestamp_begin` field, it must be an unsigned
902 * integer field type. Also, if the trace is not a CTF writer's
903 * trace, then we cannot automatically set the mapped clock
904 * class of this field, so it must have a mapped clock class.
906 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
907 packet_context_type
, "timestamp_begin");
909 if (!bt_ctf_field_type_is_integer(field_type
)) {
910 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
911 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
913 bt_ctf_field_type_id_string(field_type
->id
));
917 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
918 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
919 "timestamp-begin-ft-addr=%p", field_type
);
923 if (!trace
->is_created_by_writer
) {
924 struct bt_ctf_clock_class
*clock_class
=
925 bt_ctf_field_type_integer_get_mapped_clock_class(
930 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
931 "timestamp-begin-ft-addr=%p", field_type
);
940 * If there's a `timestamp_end` field, it must be an unsigned
941 * integer field type. Also, if the trace is not a CTF writer's
942 * trace, then we cannot automatically set the mapped clock
943 * class of this field, so it must have a mapped clock class.
945 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
946 packet_context_type
, "timestamp_end");
948 if (!bt_ctf_field_type_is_integer(field_type
)) {
949 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
950 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
952 bt_ctf_field_type_id_string(field_type
->id
));
956 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
957 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
958 "timestamp-end-ft-addr=%p", field_type
);
962 if (!trace
->is_created_by_writer
) {
963 struct bt_ctf_clock_class
*clock_class
=
964 bt_ctf_field_type_integer_get_mapped_clock_class(
969 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
970 "timestamp-end-ft-addr=%p", field_type
);
989 bool event_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
990 struct bt_ctf_stream_class
*stream_class
,
991 struct bt_ctf_field_type
*event_header_type
)
993 bool is_valid
= true;
994 struct bt_ctf_field_type
*field_type
= NULL
;
997 * We do not validate that the `timestamp` field exists here
998 * because CTF does not require this exact name to be mapped to
1002 if (!event_header_type
) {
1004 * No event header field type: stream class must have
1005 * only one event class.
1007 if (bt_ctf_stream_class_get_event_class_count(stream_class
) > 1) {
1008 BT_LOGW_STR("Invalid event header field type: "
1009 "event header field type does not exist but there's more than one event class in the stream class.");
1013 /* No event header field type: valid at this point */
1017 /* Event header field type, if it exists, must be a structure */
1018 if (!bt_ctf_field_type_is_structure(event_header_type
)) {
1019 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1020 "ft-addr=%p, ft-id=%s",
1022 bt_ctf_field_type_id_string(event_header_type
->id
));
1027 * If there's an `id` field, it must be an unsigned integer
1028 * field type or an enumeration field type with an unsigned
1029 * integer container field type.
1031 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1032 event_header_type
, "id");
1034 struct bt_ctf_field_type
*int_ft
;
1036 if (bt_ctf_field_type_is_integer(field_type
)) {
1037 int_ft
= bt_get(field_type
);
1038 } else if (bt_ctf_field_type_is_enumeration(field_type
)) {
1039 int_ft
= bt_ctf_field_type_enumeration_get_container_type(
1042 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1043 "id-ft-addr=%p, id-ft-id=%s",
1045 bt_ctf_field_type_id_string(field_type
->id
));
1050 if (bt_ctf_field_type_integer_is_signed(int_ft
)) {
1051 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1052 "id-ft-addr=%p", int_ft
);
1070 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1071 struct bt_ctf_stream_class
*stream_class
)
1076 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
1077 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
1078 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
1079 BT_CTF_VALIDATION_FLAG_TRACE
|
1080 BT_CTF_VALIDATION_FLAG_STREAM
;
1081 const enum bt_ctf_validation_flag ec_validation_flags
=
1082 BT_CTF_VALIDATION_FLAG_EVENT
;
1083 struct bt_ctf_field_type
*packet_header_type
= NULL
;
1084 struct bt_ctf_field_type
*packet_context_type
= NULL
;
1085 struct bt_ctf_field_type
*event_header_type
= NULL
;
1086 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
1087 int64_t event_class_count
;
1088 struct bt_ctf_trace
*current_parent_trace
= NULL
;
1091 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1096 if (!stream_class
) {
1097 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1102 if (trace
->is_static
) {
1103 BT_LOGW_STR("Invalid parameter: trace is static.");
1108 BT_LOGD("Adding stream class to trace: "
1109 "trace-addr=%p, trace-name=\"%s\", "
1110 "stream-class-addr=%p, stream-class-name=\"%s\", "
1111 "stream-class-id=%" PRId64
,
1112 trace
, bt_ctf_trace_get_name(trace
),
1113 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1114 bt_ctf_stream_class_get_id(stream_class
));
1116 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
1117 if (current_parent_trace
) {
1118 /* Stream class is already associated to a trace, abort. */
1119 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1120 "stream-class-trace-addr=%p, "
1121 "stream-class-trace-name=\"%s\"",
1122 current_parent_trace
,
1123 bt_ctf_trace_get_name(current_parent_trace
));
1129 bt_ctf_stream_class_get_event_class_count(stream_class
);
1130 assert(event_class_count
>= 0);
1132 if (stream_class
->clock
) {
1133 struct bt_ctf_clock_class
*stream_clock_class
=
1134 stream_class
->clock
->clock_class
;
1136 if (trace
->is_created_by_writer
) {
1138 * Make sure this clock was also added to the
1139 * trace (potentially through its CTF writer
1144 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1145 if (trace
->clocks
->pdata
[i
] ==
1146 stream_clock_class
) {
1152 if (i
== trace
->clocks
->len
) {
1154 BT_LOGW("Stream class's clock's class is not part of the trace: "
1155 "clock-class-addr=%p, clock-class-name=\"%s\"",
1157 bt_ctf_clock_class_get_name(stream_clock_class
));
1163 * This trace was NOT created by a CTF writer,
1164 * thus do not allow the stream class to add to
1165 * have a clock at all. Those are two
1166 * independent APIs (non-writer and writer
1167 * APIs), and isolating them simplifies things.
1169 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1170 "clock-class-addr=%p, clock-class-name=\"%s\"",
1172 bt_ctf_clock_class_get_name(stream_clock_class
));
1179 * We're about to freeze both the trace and the stream class.
1180 * Also, each event class contained in this stream class are
1183 * This trace, this stream class, and all its event classes
1184 * should be valid at this point.
1186 * Validate trace and stream class first, then each event
1187 * class of this stream class can be validated individually.
1189 packet_header_type
=
1190 bt_ctf_trace_get_packet_header_type(trace
);
1191 packet_context_type
=
1192 bt_ctf_stream_class_get_packet_context_type(stream_class
);
1194 bt_ctf_stream_class_get_event_header_type(stream_class
);
1195 stream_event_ctx_type
=
1196 bt_ctf_stream_class_get_event_context_type(stream_class
);
1198 BT_LOGD("Validating trace and stream class field types.");
1199 ret
= bt_ctf_validate_class_types(trace
->environment
,
1200 packet_header_type
, packet_context_type
, event_header_type
,
1201 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1202 stream_class
->valid
, 1, &trace_sc_validation_output
,
1203 trace_sc_validation_flags
);
1204 BT_PUT(packet_header_type
);
1205 BT_PUT(packet_context_type
);
1206 BT_PUT(event_header_type
);
1207 BT_PUT(stream_event_ctx_type
);
1211 * This means something went wrong during the validation
1212 * process, not that the objects are invalid.
1214 BT_LOGE("Failed to validate trace and stream class field types: "
1219 if ((trace_sc_validation_output
.valid_flags
&
1220 trace_sc_validation_flags
) !=
1221 trace_sc_validation_flags
) {
1222 /* Invalid trace/stream class */
1223 BT_LOGW("Invalid trace or stream class field types: "
1225 trace_sc_validation_output
.valid_flags
);
1230 if (event_class_count
> 0) {
1231 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1233 if (!ec_validation_outputs
) {
1234 BT_LOGE_STR("Failed to allocate one validation output structure.");
1240 /* Validate each event class individually */
1241 for (i
= 0; i
< event_class_count
; i
++) {
1242 struct bt_ctf_event_class
*event_class
=
1243 bt_ctf_stream_class_get_event_class_by_index(
1245 struct bt_ctf_field_type
*event_context_type
= NULL
;
1246 struct bt_ctf_field_type
*event_payload_type
= NULL
;
1248 event_context_type
=
1249 bt_ctf_event_class_get_context_type(event_class
);
1250 event_payload_type
=
1251 bt_ctf_event_class_get_payload_type(event_class
);
1254 * It is important to use the field types returned by
1255 * the previous trace and stream class validation here
1256 * because copies could have been made.
1258 BT_LOGD("Validating event class's field types: "
1259 "addr=%p, name=\"%s\", id=%" PRId64
,
1260 event_class
, bt_ctf_event_class_get_name(event_class
),
1261 bt_ctf_event_class_get_id(event_class
));
1262 ret
= bt_ctf_validate_class_types(trace
->environment
,
1263 trace_sc_validation_output
.packet_header_type
,
1264 trace_sc_validation_output
.packet_context_type
,
1265 trace_sc_validation_output
.event_header_type
,
1266 trace_sc_validation_output
.stream_event_ctx_type
,
1267 event_context_type
, event_payload_type
,
1268 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1269 ec_validation_flags
);
1270 BT_PUT(event_context_type
);
1271 BT_PUT(event_payload_type
);
1272 BT_PUT(event_class
);
1275 BT_LOGE("Failed to validate event class field types: "
1280 if ((ec_validation_outputs
[i
].valid_flags
&
1281 ec_validation_flags
) != ec_validation_flags
) {
1282 /* Invalid event class */
1283 BT_LOGW("Invalid event class field types: "
1285 ec_validation_outputs
[i
].valid_flags
);
1291 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
1292 if (stream_id
< 0) {
1293 stream_id
= trace
->next_stream_id
++;
1294 if (stream_id
< 0) {
1295 BT_LOGE_STR("No more stream class IDs available.");
1300 /* Try to assign a new stream id */
1301 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1302 if (stream_id
== bt_ctf_stream_class_get_id(
1303 trace
->stream_classes
->pdata
[i
])) {
1304 /* Duplicate stream id found */
1305 BT_LOGW("Duplicate stream class ID: "
1306 "id=%" PRId64
, (int64_t) stream_id
);
1312 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
1314 /* TODO Should retry with a different stream id */
1315 BT_LOGE("Cannot set stream class's ID: "
1316 "id=%" PRId64
, (int64_t) stream_id
);
1323 * At this point all the field types in the validation output
1324 * are valid. Validate the semantics of some scopes according to
1325 * the CTF specification.
1327 if (!packet_header_field_type_is_valid(trace
,
1328 trace_sc_validation_output
.packet_header_type
)) {
1329 BT_LOGW_STR("Invalid trace's packet header field type.");
1334 if (!packet_context_field_type_is_valid(trace
,
1336 trace_sc_validation_output
.packet_context_type
)) {
1337 BT_LOGW_STR("Invalid stream class's packet context field type.");
1342 if (!event_header_field_type_is_valid(trace
,
1344 trace_sc_validation_output
.event_header_type
)) {
1345 BT_LOGW_STR("Invalid steam class's event header field type.");
1351 * Now is the time to automatically map specific field types of
1352 * the stream class's packet context and event header field
1353 * types to the stream class's clock's class if they are not
1354 * mapped to a clock class yet. We do it here because we know
1355 * that after this point, everything is frozen so it won't be
1356 * possible for the user to modify the stream class's clock, or
1357 * to map those field types to other clock classes.
1359 if (trace
->is_created_by_writer
) {
1360 if (bt_ctf_stream_class_map_clock_class(stream_class
,
1361 trace_sc_validation_output
.packet_context_type
,
1362 trace_sc_validation_output
.event_header_type
)) {
1363 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1369 bt_object_set_parent(stream_class
, trace
);
1370 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1373 * At this point we know that the function will be successful.
1374 * Therefore we can replace the trace and stream class field
1375 * types with what's in their validation output structure and
1376 * mark them as valid. We can also replace the field types of
1377 * all the event classes of the stream class and mark them as
1380 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1381 &trace_sc_validation_output
, trace_sc_validation_flags
);
1383 stream_class
->valid
= 1;
1386 * Put what was not moved in bt_ctf_validation_replace_types().
1388 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1390 for (i
= 0; i
< event_class_count
; i
++) {
1391 struct bt_ctf_event_class
*event_class
=
1392 bt_ctf_stream_class_get_event_class_by_index(
1395 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1396 &ec_validation_outputs
[i
], ec_validation_flags
);
1397 event_class
->valid
= 1;
1398 BT_PUT(event_class
);
1401 * Put what was not moved in
1402 * bt_ctf_validation_replace_types().
1404 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1408 * Freeze the trace and the stream class.
1410 bt_ctf_stream_class_freeze(stream_class
);
1411 bt_ctf_trace_freeze(trace
);
1413 /* Notifiy listeners of the trace's schema modification. */
1414 bt_ctf_stream_class_visit(stream_class
,
1415 bt_ctf_trace_object_modification
, trace
);
1416 BT_LOGD("Added stream class to trace: "
1417 "trace-addr=%p, trace-name=\"%s\", "
1418 "stream-class-addr=%p, stream-class-name=\"%s\", "
1419 "stream-class-id=%" PRId64
,
1420 trace
, bt_ctf_trace_get_name(trace
),
1421 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1422 bt_ctf_stream_class_get_id(stream_class
));
1426 bt_object_set_parent(stream_class
, NULL
);
1428 if (ec_validation_outputs
) {
1429 for (i
= 0; i
< event_class_count
; i
++) {
1430 bt_ctf_validation_output_put_types(
1431 &ec_validation_outputs
[i
]);
1436 g_free(ec_validation_outputs
);
1437 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1438 bt_put(current_parent_trace
);
1439 assert(!packet_header_type
);
1440 assert(!packet_context_type
);
1441 assert(!event_header_type
);
1442 assert(!stream_event_ctx_type
);
1446 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1451 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1456 ret
= (int64_t) trace
->streams
->len
;
1462 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1463 struct bt_ctf_trace
*trace
,
1466 struct bt_ctf_stream
*stream
= NULL
;
1469 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1473 if (index
>= trace
->streams
->len
) {
1474 BT_LOGW("Invalid parameter: index is out of bounds: "
1475 "addr=%p, name=\"%s\", "
1476 "index=%" PRIu64
", count=%u",
1477 trace
, bt_ctf_trace_get_name(trace
),
1478 index
, trace
->streams
->len
);
1482 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1488 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1493 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1498 ret
= (int64_t) trace
->stream_classes
->len
;
1503 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1504 struct bt_ctf_trace
*trace
, uint64_t index
)
1506 struct bt_ctf_stream_class
*stream_class
= NULL
;
1509 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1513 if (index
>= trace
->stream_classes
->len
) {
1514 BT_LOGW("Invalid parameter: index is out of bounds: "
1515 "addr=%p, name=\"%s\", "
1516 "index=%" PRIu64
", count=%u",
1517 trace
, bt_ctf_trace_get_name(trace
),
1518 index
, trace
->stream_classes
->len
);
1522 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1523 bt_get(stream_class
);
1525 return stream_class
;
1528 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1529 struct bt_ctf_trace
*trace
, uint64_t id_param
)
1532 struct bt_ctf_stream_class
*stream_class
= NULL
;
1533 int64_t id
= (int64_t) id_param
;
1536 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1541 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1542 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64
,
1543 trace
, bt_ctf_trace_get_name(trace
), id_param
);
1547 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1548 struct bt_ctf_stream_class
*stream_class_candidate
;
1550 stream_class_candidate
=
1551 g_ptr_array_index(trace
->stream_classes
, i
);
1553 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
1555 stream_class
= stream_class_candidate
;
1556 bt_get(stream_class
);
1562 return stream_class
;
1565 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1566 struct bt_ctf_trace
*trace
, const char *name
)
1569 struct bt_ctf_clock_class
*clock_class
= NULL
;
1572 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1577 BT_LOGW_STR("Invalid parameter: name is NULL.");
1581 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1582 struct bt_ctf_clock_class
*cur_clk
=
1583 g_ptr_array_index(trace
->clocks
, i
);
1584 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
1586 if (!cur_clk_name
) {
1590 if (!strcmp(cur_clk_name
, name
)) {
1591 clock_class
= cur_clk
;
1592 bt_get(clock_class
);
1602 bt_bool
bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
1603 struct bt_ctf_clock_class
*clock_class
)
1605 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1608 assert(clock_class
);
1610 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1615 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
1619 switch (byte_order
) {
1620 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1623 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1626 case BT_CTF_BYTE_ORDER_NATIVE
:
1637 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1638 struct metadata_context
*context
)
1640 unsigned char *uuid
= trace
->uuid
;
1643 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
||
1644 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NONE
) {
1645 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_NONE at this point; "
1646 "set it with bt_ctf_trace_set_native_byte_order(): "
1647 "addr=%p, name=\"%s\"",
1648 trace
, bt_ctf_trace_get_name(trace
));
1653 g_string_append(context
->string
, "trace {\n");
1654 g_string_append(context
->string
, "\tmajor = 1;\n");
1655 g_string_append(context
->string
, "\tminor = 8;\n");
1656 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1657 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1658 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1660 if (trace
->uuid_set
) {
1661 g_string_append_printf(context
->string
,
1662 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1663 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1664 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1665 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1666 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1669 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1670 get_byte_order_string(trace
->native_byte_order
));
1672 if (trace
->packet_header_type
) {
1673 g_string_append(context
->string
, "\tpacket.header := ");
1674 context
->current_indentation_level
++;
1675 g_string_assign(context
->field_name
, "");
1676 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1677 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
1682 context
->current_indentation_level
--;
1685 g_string_append(context
->string
, ";\n};\n\n");
1691 void append_env_metadata(struct bt_ctf_trace
*trace
,
1692 struct metadata_context
*context
)
1697 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
1698 if (env_size
<= 0) {
1702 g_string_append(context
->string
, "env {\n");
1704 for (i
= 0; i
< env_size
; i
++) {
1705 struct bt_value
*env_field_value_obj
= NULL
;
1706 const char *entry_name
;
1708 entry_name
= bt_ctf_attributes_get_field_name(
1709 trace
->environment
, i
);
1710 env_field_value_obj
= bt_ctf_attributes_get_field_value(
1711 trace
->environment
, i
);
1714 assert(env_field_value_obj
);
1716 switch (bt_value_get_type(env_field_value_obj
)) {
1717 case BT_VALUE_TYPE_INTEGER
:
1722 ret
= bt_value_integer_get(env_field_value_obj
,
1725 g_string_append_printf(context
->string
,
1726 "\t%s = %" PRId64
";\n", entry_name
,
1730 case BT_VALUE_TYPE_STRING
:
1733 const char *str_value
;
1734 char *escaped_str
= NULL
;
1736 ret
= bt_value_string_get(env_field_value_obj
,
1739 escaped_str
= g_strescape(str_value
, NULL
);
1741 BT_LOGE("Cannot escape string: string=\"%s\"",
1746 g_string_append_printf(context
->string
,
1747 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1756 BT_PUT(env_field_value_obj
);
1759 g_string_append(context
->string
, "};\n\n");
1762 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1764 char *metadata
= NULL
;
1765 struct metadata_context
*context
= NULL
;
1770 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1774 context
= g_new0(struct metadata_context
, 1);
1776 BT_LOGE_STR("Failed to allocate one metadata context.");
1780 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1781 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1782 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1783 if (append_trace_metadata(trace
, context
)) {
1784 /* append_trace_metadata() logs errors */
1787 append_env_metadata(trace
, context
);
1788 g_ptr_array_foreach(trace
->clocks
,
1789 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1791 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1792 /* bt_ctf_stream_class_serialize() logs details */
1793 err
= bt_ctf_stream_class_serialize(
1794 trace
->stream_classes
->pdata
[i
], context
);
1796 /* bt_ctf_stream_class_serialize() logs errors */
1801 metadata
= context
->string
->str
;
1804 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1805 g_string_free(context
->field_name
, TRUE
);
1812 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1813 struct bt_ctf_trace
*trace
)
1815 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1818 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1822 ret
= trace
->native_byte_order
;
1828 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1829 enum bt_ctf_byte_order byte_order
)
1834 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1839 if (trace
->frozen
) {
1840 BT_LOGW("Invalid parameter: trace is frozen: "
1841 "addr=%p, name=\"%s\"",
1842 trace
, bt_ctf_trace_get_name(trace
));
1847 if (trace
->is_created_by_writer
&&
1848 byte_order
== BT_CTF_BYTE_ORDER_NONE
) {
1849 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_NONE byte order is not allowed for a CTF writer trace: "
1850 "addr=%p, name=\"%s\"",
1851 trace
, bt_ctf_trace_get_name(trace
));
1856 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1857 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1858 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1859 BT_LOGW("Invalid parameter: invalid byte order: "
1860 "addr=%p, name=\"%s\", bo=%s",
1861 trace
, bt_ctf_trace_get_name(trace
),
1862 bt_ctf_byte_order_string(byte_order
));
1867 trace
->native_byte_order
= byte_order
;
1868 BT_LOGV("Set trace's native byte order: "
1869 "addr=%p, name=\"%s\", bo=%s",
1870 trace
, bt_ctf_trace_get_name(trace
),
1871 bt_ctf_byte_order_string(byte_order
));
1877 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1878 struct bt_ctf_trace
*trace
)
1880 struct bt_ctf_field_type
*field_type
= NULL
;
1883 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1887 bt_get(trace
->packet_header_type
);
1888 field_type
= trace
->packet_header_type
;
1893 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1894 struct bt_ctf_field_type
*packet_header_type
)
1899 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1904 if (trace
->frozen
) {
1905 BT_LOGW("Invalid parameter: trace is frozen: "
1906 "addr=%p, name=\"%s\"",
1907 trace
, bt_ctf_trace_get_name(trace
));
1912 /* packet_header_type must be a structure. */
1913 if (packet_header_type
&&
1914 !bt_ctf_field_type_is_structure(packet_header_type
)) {
1915 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1916 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1917 trace
, bt_ctf_trace_get_name(trace
),
1919 bt_ctf_field_type_id_string(packet_header_type
->id
));
1924 bt_put(trace
->packet_header_type
);
1925 trace
->packet_header_type
= bt_get(packet_header_type
);
1926 BT_LOGV("Set trace's packet header field type: "
1927 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1928 trace
, bt_ctf_trace_get_name(trace
), packet_header_type
);
1934 int64_t get_stream_class_count(void *element
)
1936 return bt_ctf_trace_get_stream_class_count(
1937 (struct bt_ctf_trace
*) element
);
1941 void *get_stream_class(void *element
, int i
)
1943 return bt_ctf_trace_get_stream_class_by_index(
1944 (struct bt_ctf_trace
*) element
, i
);
1948 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1950 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1953 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1954 bt_ctf_visitor visitor
, void *data
)
1957 struct bt_ctf_object obj
=
1958 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1961 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1967 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1972 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1973 trace
, bt_ctf_trace_get_name(trace
));
1974 ret
= visitor_helper(&obj
, get_stream_class_count
,
1975 get_stream_class
, visit_stream_class
, visitor
, data
);
1981 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1983 struct listener_wrapper
*listener_wrapper
= data
;
1985 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1989 // TODO: add logging to this function once we use it internally.
1990 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1991 bt_ctf_listener_cb listener
, void *listener_data
)
1994 struct listener_wrapper
*listener_wrapper
=
1995 g_new0(struct listener_wrapper
, 1);
1997 if (!trace
|| !listener
|| !listener_wrapper
) {
2002 listener_wrapper
->listener
= listener
;
2003 listener_wrapper
->data
= listener_data
;
2005 /* Visit the current schema. */
2006 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2012 * Add listener to the array of callbacks which will be invoked on
2015 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2018 g_free(listener_wrapper
);
2023 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
2027 struct bt_ctf_trace
*trace
= trace_ptr
;
2032 if (trace
->listeners
->len
== 0) {
2036 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2037 struct listener_wrapper
*listener
=
2038 g_ptr_array_index(trace
->listeners
, i
);
2040 listener
->listener(object
, listener
->data
);
2047 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
2050 unsigned int alignment
, size
;
2051 struct bt_ctf_field_type
*field_type
= NULL
;
2053 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2057 alignment
= field_type_aliases_alignments
[alias
];
2058 size
= field_type_aliases_sizes
[alias
];
2059 field_type
= bt_ctf_field_type_integer_create(size
);
2060 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
2069 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
2073 if (trace
->frozen
) {
2077 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2078 trace
, bt_ctf_trace_get_name(trace
));
2079 BT_LOGD_STR("Freezing packet header field type.");
2080 bt_ctf_field_type_freeze(trace
->packet_header_type
);
2081 BT_LOGD_STR("Freezing environment attributes.");
2082 bt_ctf_attributes_freeze(trace
->environment
);
2084 if (trace
->clocks
->len
> 0) {
2085 BT_LOGD_STR("Freezing clock classes.");
2088 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2089 struct bt_ctf_clock_class
*clock_class
=
2090 g_ptr_array_index(trace
->clocks
, i
);
2092 bt_ctf_clock_class_freeze(clock_class
);
2098 bt_bool
bt_ctf_trace_is_static(struct bt_ctf_trace
*trace
)
2100 bt_bool is_static
= BT_FALSE
;
2103 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2107 is_static
= trace
->is_static
;
2113 int bt_ctf_trace_set_is_static(struct bt_ctf_trace
*trace
)
2119 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2124 trace
->is_static
= BT_TRUE
;
2125 bt_ctf_trace_freeze(trace
);
2126 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2127 trace
, bt_ctf_trace_get_name(trace
));
2129 /* Call all the "trace is static" listeners */
2130 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2131 struct bt_ctf_trace_is_static_listener_elem elem
=
2132 g_array_index(trace
->is_static_listeners
,
2133 struct bt_ctf_trace_is_static_listener_elem
, i
);
2136 elem
.func(trace
, elem
.data
);
2144 int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace
*trace
,
2145 bt_ctf_trace_is_static_listener listener
, void *data
)
2148 struct bt_ctf_trace_is_static_listener_elem new_elem
= {
2154 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2160 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2165 if (trace
->is_static
) {
2166 BT_LOGW("Invalid parameter: trace is already static: "
2167 "addr=%p, name=\"%s\"",
2168 trace
, bt_ctf_trace_get_name(trace
));
2173 /* Find the next available spot */
2174 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2175 struct bt_ctf_trace_is_static_listener_elem elem
=
2176 g_array_index(trace
->is_static_listeners
,
2177 struct bt_ctf_trace_is_static_listener_elem
, i
);
2184 if (i
== trace
->is_static_listeners
->len
) {
2185 g_array_append_val(trace
->is_static_listeners
, new_elem
);
2187 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
2190 BT_LOGV("Added \"trace is static\" listener: "
2191 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2192 "data-addr=%p, listener-id=%d",
2193 trace
, bt_ctf_trace_get_name(trace
), listener
, data
, i
);
2199 int bt_ctf_trace_remove_is_static_listener(
2200 struct bt_ctf_trace
*trace
, int listener_id
)
2203 struct bt_ctf_trace_is_static_listener_elem
*elem
;
2206 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2211 if (listener_id
< 0) {
2212 BT_LOGW("Invalid listener ID: must be zero or positive: "
2213 "listener-id=%d", listener_id
);
2218 if (listener_id
>= trace
->is_static_listeners
->len
) {
2219 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2220 "addr=%p, name=\"%s\", listener-id=%d",
2221 trace
, bt_ctf_trace_get_name(trace
),
2227 elem
= &g_array_index(trace
->is_static_listeners
,
2228 struct bt_ctf_trace_is_static_listener_elem
,
2231 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2232 "addr=%p, name=\"%s\", listener-id=%d",
2233 trace
, bt_ctf_trace_get_name(trace
),
2241 BT_LOGV("Removed \"trace is static\" listener: "
2242 "trace-addr=%p, trace-name=\"%s\", "
2243 "listener-id=%d", trace
, bt_ctf_trace_get_name(trace
),