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>
56 #define DEFAULT_IDENTIFIER_SIZE 128
57 #define DEFAULT_METADATA_STRING_SIZE 4096
59 struct listener_wrapper
{
60 bt_ctf_listener_cb listener
;
65 void bt_ctf_trace_destroy(struct bt_object
*obj
);
67 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
);
70 const unsigned int field_type_aliases_alignments
[] = {
71 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
72 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
73 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
74 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
78 const unsigned int field_type_aliases_sizes
[] = {
79 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
80 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
81 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
82 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
83 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
84 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
87 struct bt_ctf_trace
*bt_ctf_trace_create(void)
89 struct bt_ctf_trace
*trace
= NULL
;
90 struct bt_ctf_field_type
*packet_header_type
= NULL
;
92 trace
= g_new0(struct bt_ctf_trace
, 1);
94 BT_LOGE_STR("Failed to allocate one trace.");
98 BT_LOGD_STR("Creating trace object.");
99 trace
->native_byte_order
= BT_CTF_BYTE_ORDER_NATIVE
;
100 bt_object_init(trace
, bt_ctf_trace_destroy
);
101 trace
->clocks
= g_ptr_array_new_with_free_func(
102 (GDestroyNotify
) bt_put
);
103 if (!trace
->clocks
) {
104 BT_LOGE_STR("Failed to allocate one GPtrArray.");
108 trace
->streams
= g_ptr_array_new_with_free_func(
109 (GDestroyNotify
) bt_object_release
);
110 if (!trace
->streams
) {
111 BT_LOGE_STR("Failed to allocate one GPtrArray.");
115 trace
->stream_classes
= g_ptr_array_new_with_free_func(
116 (GDestroyNotify
) bt_object_release
);
117 if (!trace
->stream_classes
) {
118 BT_LOGE_STR("Failed to allocate one GPtrArray.");
122 packet_header_type
= bt_ctf_field_type_structure_create();
123 if (!packet_header_type
) {
127 BT_MOVE(trace
->packet_header_type
, packet_header_type
);
129 /* Create the environment array object */
130 trace
->environment
= bt_ctf_attributes_create();
131 if (!trace
->environment
) {
132 BT_LOGE_STR("Cannot create empty attributes object.");
136 trace
->listeners
= g_ptr_array_new_with_free_func(
137 (GDestroyNotify
) g_free
);
138 if (!trace
->listeners
) {
139 BT_LOGE_STR("Failed to allocate one GPtrArray.");
143 BT_LOGD("Created trace object: addr=%p", trace
);
148 bt_put(packet_header_type
);
152 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
154 const char *name
= NULL
;
157 BT_LOGW_STR("Invalid parameter: trace is NULL.");
165 name
= trace
->name
->str
;
170 int bt_ctf_trace_set_name(struct bt_ctf_trace
*trace
, const char *name
)
175 BT_LOGW_STR("Invalid parameter: trace is NULL.");
181 BT_LOGW_STR("Invalid parameter: name is NULL.");
187 BT_LOGW("Invalid parameter: trace is frozen: "
188 "addr=%p, name=\"%s\"",
189 trace
, bt_ctf_trace_get_name(trace
));
194 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
197 BT_LOGE_STR("Failed to allocate one GString.");
202 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
208 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
210 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
213 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
, const unsigned char *uuid
)
218 BT_LOGW_STR("Invalid parameter: trace is NULL.");
224 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
230 BT_LOGW("Invalid parameter: trace is frozen: "
231 "addr=%p, name=\"%s\"",
232 trace
, bt_ctf_trace_get_name(trace
));
237 memcpy(trace
->uuid
, uuid
, sizeof(uuid_t
));
238 trace
->uuid_set
= BT_TRUE
;
239 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
240 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
241 trace
, bt_ctf_trace_get_name(trace
),
242 (unsigned int) uuid
[0],
243 (unsigned int) uuid
[1],
244 (unsigned int) uuid
[2],
245 (unsigned int) uuid
[3],
246 (unsigned int) uuid
[4],
247 (unsigned int) uuid
[5],
248 (unsigned int) uuid
[6],
249 (unsigned int) uuid
[7],
250 (unsigned int) uuid
[8],
251 (unsigned int) uuid
[9],
252 (unsigned int) uuid
[10],
253 (unsigned int) uuid
[11],
254 (unsigned int) uuid
[12],
255 (unsigned int) uuid
[13],
256 (unsigned int) uuid
[14],
257 (unsigned int) uuid
[15]);
263 void bt_ctf_trace_destroy(struct bt_object
*obj
)
265 struct bt_ctf_trace
*trace
;
267 trace
= container_of(obj
, struct bt_ctf_trace
, base
);
269 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
270 trace
, bt_ctf_trace_get_name(trace
));
272 if (trace
->environment
) {
273 BT_LOGD_STR("Destroying environment attributes.");
274 bt_ctf_attributes_destroy(trace
->environment
);
278 g_string_free(trace
->name
, TRUE
);
282 BT_LOGD_STR("Putting clock classes.");
283 g_ptr_array_free(trace
->clocks
, TRUE
);
286 if (trace
->streams
) {
287 BT_LOGD_STR("Destroying streams.");
288 g_ptr_array_free(trace
->streams
, TRUE
);
291 if (trace
->stream_classes
) {
292 BT_LOGD_STR("Destroying stream classes.");
293 g_ptr_array_free(trace
->stream_classes
, TRUE
);
296 if (trace
->listeners
) {
297 g_ptr_array_free(trace
->listeners
, TRUE
);
300 BT_LOGD_STR("Putting packet header field type.");
301 bt_put(trace
->packet_header_type
);
305 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
306 const char *name
, struct bt_value
*value
)
311 BT_LOGW_STR("Invalid parameter: trace is NULL.");
317 BT_LOGW_STR("Invalid parameter: name is NULL.");
323 BT_LOGW_STR("Invalid parameter: value is NULL.");
328 if (bt_ctf_validate_identifier(name
)) {
329 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
330 "trace-addr=%p, trace-name=\"%s\", "
332 trace
, bt_ctf_trace_get_name(trace
), name
);
337 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
338 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
339 "trace-addr=%p, trace-name=\"%s\", "
340 "env-name=\"%s\", env-value-type=%s",
341 trace
, bt_ctf_trace_get_name(trace
), name
,
342 bt_value_type_string(bt_value_get_type(value
)));
347 if (trace
->is_static
) {
348 BT_LOGW("Invalid parameter: trace is static: "
349 "addr=%p, name=\"%s\"",
350 trace
, bt_ctf_trace_get_name(trace
));
357 * New environment fields may be added to a frozen trace,
358 * but existing fields may not be changed.
360 * The object passed is frozen like all other attributes.
362 struct bt_value
*attribute
=
363 bt_ctf_attributes_get_field_value_by_name(
364 trace
->environment
, name
);
367 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
368 "trace-addr=%p, trace-name=\"%s\", "
370 trace
, bt_ctf_trace_get_name(trace
), name
);
376 bt_value_freeze(value
);
379 ret
= bt_ctf_attributes_set_field_value(trace
->environment
, name
,
382 BT_LOGE("Cannot set environment field's value: "
383 "trace-addr=%p, trace-name=\"%s\", "
385 trace
, bt_ctf_trace_get_name(trace
), name
);
387 BT_LOGV("Set environment field's value: "
388 "trace-addr=%p, trace-name=\"%s\", "
389 "env-name=\"%s\", value-addr=%p",
390 trace
, bt_ctf_trace_get_name(trace
), name
, value
);
397 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
398 const char *name
, const char *value
)
401 struct bt_value
*env_value_string_obj
= NULL
;
404 BT_LOGW_STR("Invalid parameter: value is NULL.");
409 env_value_string_obj
= bt_value_string_create_init(value
);
410 if (!env_value_string_obj
) {
411 BT_LOGE_STR("Cannot create string value object.");
416 /* bt_ctf_trace_set_environment_field() logs errors */
417 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
418 env_value_string_obj
);
421 bt_put(env_value_string_obj
);
425 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace
*trace
,
426 const char *name
, int64_t value
)
429 struct bt_value
*env_value_integer_obj
= NULL
;
431 env_value_integer_obj
= bt_value_integer_create_init(value
);
432 if (!env_value_integer_obj
) {
433 BT_LOGE_STR("Cannot create integer value object.");
438 /* bt_ctf_trace_set_environment_field() logs errors */
439 ret
= bt_ctf_trace_set_environment_field(trace
, name
,
440 env_value_integer_obj
);
443 bt_put(env_value_integer_obj
);
447 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
452 BT_LOGW_STR("Invalid parameter: trace is NULL.");
457 ret
= bt_ctf_attributes_get_count(trace
->environment
);
465 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
468 const char *ret
= NULL
;
471 BT_LOGW_STR("Invalid parameter: trace is NULL.");
475 ret
= bt_ctf_attributes_get_field_name(trace
->environment
, index
);
481 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
482 struct bt_ctf_trace
*trace
, uint64_t index
)
484 struct bt_value
*ret
= NULL
;
487 BT_LOGW_STR("Invalid parameter: trace is NULL.");
491 ret
= bt_ctf_attributes_get_field_value(trace
->environment
, index
);
497 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
498 struct bt_ctf_trace
*trace
, const char *name
)
500 struct bt_value
*ret
= NULL
;
503 BT_LOGW_STR("Invalid parameter: trace is NULL.");
508 BT_LOGW_STR("Invalid parameter: name is NULL.");
512 ret
= bt_ctf_attributes_get_field_value_by_name(trace
->environment
,
519 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
520 struct bt_ctf_clock_class
*clock_class
)
525 BT_LOGW_STR("Invalid parameter: trace is NULL.");
530 if (trace
->is_static
) {
531 BT_LOGW("Invalid parameter: trace is static: "
532 "addr=%p, name=\"%s\"",
533 trace
, bt_ctf_trace_get_name(trace
));
538 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
539 BT_LOGW("Invalid parameter: clock class is invalid: "
540 "trace-addr=%p, trace-name=\"%s\", "
541 "clock-class-addr=%p, clock-class-name=\"%s\"",
542 trace
, bt_ctf_trace_get_name(trace
),
543 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
548 /* Check for duplicate clock classes */
549 if (bt_ctf_trace_has_clock_class(trace
, clock_class
)) {
550 BT_LOGW("Invalid parameter: clock class already exists in trace: "
551 "trace-addr=%p, trace-name=\"%s\", "
552 "clock-class-addr=%p, clock-class-name=\"%s\"",
553 trace
, bt_ctf_trace_get_name(trace
),
554 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
560 g_ptr_array_add(trace
->clocks
, clock_class
);
563 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
564 bt_ctf_clock_class_freeze(clock_class
);
567 BT_LOGV("Added clock class to trace: "
568 "trace-addr=%p, trace-name=\"%s\", "
569 "clock-class-addr=%p, clock-class-name=\"%s\"",
570 trace
, bt_ctf_trace_get_name(trace
),
571 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
577 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
579 int64_t ret
= (int64_t) -1;
582 BT_LOGW_STR("Invalid parameter: trace is NULL.");
586 ret
= trace
->clocks
->len
;
591 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
592 struct bt_ctf_trace
*trace
, uint64_t index
)
594 struct bt_ctf_clock_class
*clock_class
= NULL
;
597 BT_LOGW_STR("Invalid parameter: trace is NULL.");
601 if (index
>= trace
->clocks
->len
) {
602 BT_LOGW("Invalid parameter: index is out of bounds: "
603 "addr=%p, name=\"%s\", "
604 "index=%" PRIu64
", count=%u",
605 trace
, bt_ctf_trace_get_name(trace
),
606 index
, trace
->clocks
->len
);
610 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
617 bool packet_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
618 struct bt_ctf_field_type
*packet_header_type
)
621 bool is_valid
= true;
622 struct bt_ctf_field_type
*field_type
= NULL
;
624 if (!packet_header_type
) {
626 * No packet header field type: trace must have only
627 * one stream. At this point the stream class being
628 * added is not part of the trace yet, so we validate
629 * that the trace contains no stream classes yet.
631 if (trace
->stream_classes
->len
>= 1) {
632 BT_LOGW_STR("Invalid packet header field type: "
633 "packet header field type does not exist but there's more than one stream class in the trace.");
637 /* No packet header field type: valid at this point */
641 /* Packet header field type, if it exists, must be a structure */
642 if (!bt_ctf_field_type_is_structure(packet_header_type
)) {
643 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
644 "ft-addr=%p, ft-id=%s",
646 bt_ctf_field_type_id_string(packet_header_type
->id
));
651 * If there's a `magic` field, it must be a 32-bit unsigned
652 * integer field type. Also it must be the first field of the
653 * packet header field type.
655 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
656 packet_header_type
, "magic");
658 const char *field_name
;
660 if (!bt_ctf_field_type_is_integer(field_type
)) {
661 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
662 "magic-ft-addr=%p, magic-ft-id=%s",
664 bt_ctf_field_type_id_string(field_type
->id
));
668 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
669 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
670 "magic-ft-addr=%p", field_type
);
674 if (bt_ctf_field_type_integer_get_size(field_type
) != 32) {
675 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
676 "magic-ft-addr=%p, magic-ft-size=%u",
678 bt_ctf_field_type_integer_get_size(field_type
));
682 ret
= bt_ctf_field_type_structure_get_field_by_index(
683 packet_header_type
, &field_name
, NULL
, 0);
686 if (strcmp(field_name
, "magic") != 0) {
687 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
688 "magic-ft-addr=%p, first-field-name=\"%s\"",
689 field_type
, field_name
);
697 * If there's a `uuid` field, it must be an array field type of
698 * length 16 with an 8-bit unsigned integer element field type.
700 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
701 packet_header_type
, "uuid");
703 struct bt_ctf_field_type
*elem_ft
;
705 if (!bt_ctf_field_type_is_array(field_type
)) {
706 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
707 "uuid-ft-addr=%p, uuid-ft-id=%s",
709 bt_ctf_field_type_id_string(field_type
->id
));
713 if (bt_ctf_field_type_array_get_length(field_type
) != 16) {
714 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
715 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
717 bt_ctf_field_type_array_get_length(field_type
));
721 elem_ft
= bt_ctf_field_type_array_get_element_type(field_type
);
724 if (!bt_ctf_field_type_is_integer(elem_ft
)) {
725 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
726 "elem-ft-addr=%p, elem-ft-id=%s",
728 bt_ctf_field_type_id_string(elem_ft
->id
));
733 if (bt_ctf_field_type_integer_is_signed(elem_ft
)) {
734 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
735 "elem-ft-addr=%p", elem_ft
);
740 if (bt_ctf_field_type_integer_get_size(elem_ft
) != 8) {
741 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
742 "elem-ft-addr=%p, elem-ft-size=%u",
744 bt_ctf_field_type_integer_get_size(elem_ft
));
754 * The `stream_id` field must exist if there's more than one
755 * stream classes in the trace.
757 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
758 packet_header_type
, "stream_id");
760 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
761 BT_LOGW_STR("Invalid packet header field type: "
762 "`stream_id` field does not exist but there's more than one stream class in the trace.");
767 * If there's a `stream_id` field, it must be an unsigned
768 * integer field type.
771 if (!bt_ctf_field_type_is_integer(field_type
)) {
772 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
773 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
775 bt_ctf_field_type_id_string(field_type
->id
));
779 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
780 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
781 "stream-id-ft-addr=%p", field_type
);
799 bool packet_context_field_type_is_valid(struct bt_ctf_trace
*trace
,
800 struct bt_ctf_stream_class
*stream_class
,
801 struct bt_ctf_field_type
*packet_context_type
)
803 bool is_valid
= true;
804 struct bt_ctf_field_type
*field_type
= NULL
;
806 if (!packet_context_type
) {
807 /* No packet context field type: valid at this point */
811 /* Packet context field type, if it exists, must be a structure */
812 if (!bt_ctf_field_type_is_structure(packet_context_type
)) {
813 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
814 "ft-addr=%p, ft-id=%s",
816 bt_ctf_field_type_id_string(packet_context_type
->id
));
821 * If there's a `packet_size` field, it must be an unsigned
822 * integer field type.
824 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
825 packet_context_type
, "packet_size");
827 if (!bt_ctf_field_type_is_integer(field_type
)) {
828 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
829 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
831 bt_ctf_field_type_id_string(field_type
->id
));
835 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
836 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
837 "packet-size-ft-addr=%p", field_type
);
845 * If there's a `content_size` field, it must be an unsigned
846 * integer field type.
848 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
849 packet_context_type
, "content_size");
851 if (!bt_ctf_field_type_is_integer(field_type
)) {
852 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
853 "content-size-ft-addr=%p, content-size-ft-id=%s",
855 bt_ctf_field_type_id_string(field_type
->id
));
859 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
860 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
861 "content-size-ft-addr=%p", field_type
);
869 * If there's a `events_discarded` field, it must be an unsigned
870 * integer field type.
872 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
873 packet_context_type
, "events_discarded");
875 if (!bt_ctf_field_type_is_integer(field_type
)) {
876 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
877 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
879 bt_ctf_field_type_id_string(field_type
->id
));
883 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
884 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
885 "events-discarded-ft-addr=%p", field_type
);
893 * If there's a `timestamp_begin` field, it must be an unsigned
894 * integer field type. Also, if the trace is not a CTF writer's
895 * trace, then we cannot automatically set the mapped clock
896 * class of this field, so it must have a mapped clock class.
898 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
899 packet_context_type
, "timestamp_begin");
901 if (!bt_ctf_field_type_is_integer(field_type
)) {
902 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
903 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
905 bt_ctf_field_type_id_string(field_type
->id
));
909 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
910 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
911 "timestamp-begin-ft-addr=%p", field_type
);
915 if (!trace
->is_created_by_writer
) {
916 struct bt_ctf_clock_class
*clock_class
=
917 bt_ctf_field_type_integer_get_mapped_clock_class(
922 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
923 "timestamp-begin-ft-addr=%p", field_type
);
932 * If there's a `timestamp_end` field, it must be an unsigned
933 * integer field type. Also, if the trace is not a CTF writer's
934 * trace, then we cannot automatically set the mapped clock
935 * class of this field, so it must have a mapped clock class.
937 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
938 packet_context_type
, "timestamp_end");
940 if (!bt_ctf_field_type_is_integer(field_type
)) {
941 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
942 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
944 bt_ctf_field_type_id_string(field_type
->id
));
948 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
949 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
950 "timestamp-end-ft-addr=%p", field_type
);
954 if (!trace
->is_created_by_writer
) {
955 struct bt_ctf_clock_class
*clock_class
=
956 bt_ctf_field_type_integer_get_mapped_clock_class(
961 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
962 "timestamp-end-ft-addr=%p", field_type
);
981 bool event_header_field_type_is_valid(struct bt_ctf_trace
*trace
,
982 struct bt_ctf_stream_class
*stream_class
,
983 struct bt_ctf_field_type
*event_header_type
)
985 bool is_valid
= true;
986 struct bt_ctf_field_type
*field_type
= NULL
;
989 * We do not validate that the `timestamp` field exists here
990 * because CTF does not require this exact name to be mapped to
994 if (!event_header_type
) {
996 * No event header field type: stream class must have
997 * only one event class.
999 if (bt_ctf_stream_class_get_event_class_count(stream_class
) > 1) {
1000 BT_LOGW_STR("Invalid event header field type: "
1001 "event header field type does not exist but there's more than one event class in the stream class.");
1005 /* No event header field type: valid at this point */
1009 /* Event header field type, if it exists, must be a structure */
1010 if (!bt_ctf_field_type_is_structure(event_header_type
)) {
1011 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1012 "ft-addr=%p, ft-id=%s",
1014 bt_ctf_field_type_id_string(event_header_type
->id
));
1019 * If there's an `id` field, it must be an unsigned integer
1020 * field type or an enumeration field type with an unsigned
1021 * integer container field type.
1023 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1024 event_header_type
, "id");
1026 struct bt_ctf_field_type
*int_ft
;
1028 if (bt_ctf_field_type_is_integer(field_type
)) {
1029 int_ft
= bt_get(field_type
);
1030 } else if (bt_ctf_field_type_is_enumeration(field_type
)) {
1031 int_ft
= bt_ctf_field_type_enumeration_get_container_type(
1034 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1035 "id-ft-addr=%p, id-ft-id=%s",
1037 bt_ctf_field_type_id_string(field_type
->id
));
1042 if (bt_ctf_field_type_integer_is_signed(int_ft
)) {
1043 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1044 "id-ft-addr=%p", int_ft
);
1062 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
1063 struct bt_ctf_stream_class
*stream_class
)
1068 struct bt_ctf_validation_output trace_sc_validation_output
= { 0 };
1069 struct bt_ctf_validation_output
*ec_validation_outputs
= NULL
;
1070 const enum bt_ctf_validation_flag trace_sc_validation_flags
=
1071 BT_CTF_VALIDATION_FLAG_TRACE
|
1072 BT_CTF_VALIDATION_FLAG_STREAM
;
1073 const enum bt_ctf_validation_flag ec_validation_flags
=
1074 BT_CTF_VALIDATION_FLAG_EVENT
;
1075 struct bt_ctf_field_type
*packet_header_type
= NULL
;
1076 struct bt_ctf_field_type
*packet_context_type
= NULL
;
1077 struct bt_ctf_field_type
*event_header_type
= NULL
;
1078 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
1079 int64_t event_class_count
;
1080 struct bt_ctf_trace
*current_parent_trace
= NULL
;
1083 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1088 if (!stream_class
) {
1089 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1094 if (trace
->is_static
) {
1095 BT_LOGW_STR("Invalid parameter: trace is static.");
1100 BT_LOGD("Adding stream class to trace: "
1101 "trace-addr=%p, trace-name=\"%s\", "
1102 "stream-class-addr=%p, stream-class-name=\"%s\", "
1103 "stream-class-id=%" PRId64
,
1104 trace
, bt_ctf_trace_get_name(trace
),
1105 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1106 bt_ctf_stream_class_get_id(stream_class
));
1109 * At the end of this function we freeze the trace, so its
1110 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
1112 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
1113 BT_LOGW_STR("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE at this point; "
1114 "set it with bt_ctf_trace_set_native_byte_order().");
1119 current_parent_trace
= bt_ctf_stream_class_get_trace(stream_class
);
1120 if (current_parent_trace
) {
1121 /* Stream class is already associated to a trace, abort. */
1122 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1123 "stream-class-trace-addr=%p, "
1124 "stream-class-trace-name=\"%s\"",
1125 current_parent_trace
,
1126 bt_ctf_trace_get_name(current_parent_trace
));
1132 bt_ctf_stream_class_get_event_class_count(stream_class
);
1133 assert(event_class_count
>= 0);
1135 if (stream_class
->clock
) {
1136 struct bt_ctf_clock_class
*stream_clock_class
=
1137 stream_class
->clock
->clock_class
;
1139 if (trace
->is_created_by_writer
) {
1141 * Make sure this clock was also added to the
1142 * trace (potentially through its CTF writer
1147 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1148 if (trace
->clocks
->pdata
[i
] ==
1149 stream_clock_class
) {
1155 if (i
== trace
->clocks
->len
) {
1157 BT_LOGW("Stream class's clock's class is not part of the trace: "
1158 "clock-class-addr=%p, clock-class-name=\"%s\"",
1160 bt_ctf_clock_class_get_name(stream_clock_class
));
1166 * This trace was NOT created by a CTF writer,
1167 * thus do not allow the stream class to add to
1168 * have a clock at all. Those are two
1169 * independent APIs (non-writer and writer
1170 * APIs), and isolating them simplifies things.
1172 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1173 "clock-class-addr=%p, clock-class-name=\"%s\"",
1175 bt_ctf_clock_class_get_name(stream_clock_class
));
1182 * We're about to freeze both the trace and the stream class.
1183 * Also, each event class contained in this stream class are
1186 * This trace, this stream class, and all its event classes
1187 * should be valid at this point.
1189 * Validate trace and stream class first, then each event
1190 * class of this stream class can be validated individually.
1192 packet_header_type
=
1193 bt_ctf_trace_get_packet_header_type(trace
);
1194 packet_context_type
=
1195 bt_ctf_stream_class_get_packet_context_type(stream_class
);
1197 bt_ctf_stream_class_get_event_header_type(stream_class
);
1198 stream_event_ctx_type
=
1199 bt_ctf_stream_class_get_event_context_type(stream_class
);
1201 BT_LOGD("Validating trace and stream class field types.");
1202 ret
= bt_ctf_validate_class_types(trace
->environment
,
1203 packet_header_type
, packet_context_type
, event_header_type
,
1204 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1205 stream_class
->valid
, 1, &trace_sc_validation_output
,
1206 trace_sc_validation_flags
);
1207 BT_PUT(packet_header_type
);
1208 BT_PUT(packet_context_type
);
1209 BT_PUT(event_header_type
);
1210 BT_PUT(stream_event_ctx_type
);
1214 * This means something went wrong during the validation
1215 * process, not that the objects are invalid.
1217 BT_LOGE("Failed to validate trace and stream class field types: "
1222 if ((trace_sc_validation_output
.valid_flags
&
1223 trace_sc_validation_flags
) !=
1224 trace_sc_validation_flags
) {
1225 /* Invalid trace/stream class */
1226 BT_LOGW("Invalid trace or stream class field types: "
1228 trace_sc_validation_output
.valid_flags
);
1233 if (event_class_count
> 0) {
1234 ec_validation_outputs
= g_new0(struct bt_ctf_validation_output
,
1236 if (!ec_validation_outputs
) {
1237 BT_LOGE_STR("Failed to allocate one validation output structure.");
1243 /* Validate each event class individually */
1244 for (i
= 0; i
< event_class_count
; i
++) {
1245 struct bt_ctf_event_class
*event_class
=
1246 bt_ctf_stream_class_get_event_class_by_index(
1248 struct bt_ctf_field_type
*event_context_type
= NULL
;
1249 struct bt_ctf_field_type
*event_payload_type
= NULL
;
1251 event_context_type
=
1252 bt_ctf_event_class_get_context_type(event_class
);
1253 event_payload_type
=
1254 bt_ctf_event_class_get_payload_type(event_class
);
1257 * It is important to use the field types returned by
1258 * the previous trace and stream class validation here
1259 * because copies could have been made.
1261 BT_LOGD("Validating event class's field types: "
1262 "addr=%p, name=\"%s\", id=%" PRId64
,
1263 event_class
, bt_ctf_event_class_get_name(event_class
),
1264 bt_ctf_event_class_get_id(event_class
));
1265 ret
= bt_ctf_validate_class_types(trace
->environment
,
1266 trace_sc_validation_output
.packet_header_type
,
1267 trace_sc_validation_output
.packet_context_type
,
1268 trace_sc_validation_output
.event_header_type
,
1269 trace_sc_validation_output
.stream_event_ctx_type
,
1270 event_context_type
, event_payload_type
,
1271 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1272 ec_validation_flags
);
1273 BT_PUT(event_context_type
);
1274 BT_PUT(event_payload_type
);
1275 BT_PUT(event_class
);
1278 BT_LOGE("Failed to validate event class field types: "
1283 if ((ec_validation_outputs
[i
].valid_flags
&
1284 ec_validation_flags
) != ec_validation_flags
) {
1285 /* Invalid event class */
1286 BT_LOGW("Invalid event class field types: "
1288 ec_validation_outputs
[i
].valid_flags
);
1294 stream_id
= bt_ctf_stream_class_get_id(stream_class
);
1295 if (stream_id
< 0) {
1296 stream_id
= trace
->next_stream_id
++;
1297 if (stream_id
< 0) {
1298 BT_LOGE_STR("No more stream class IDs available.");
1303 /* Try to assign a new stream id */
1304 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1305 if (stream_id
== bt_ctf_stream_class_get_id(
1306 trace
->stream_classes
->pdata
[i
])) {
1307 /* Duplicate stream id found */
1308 BT_LOGW("Duplicate stream class ID: "
1309 "id=%" PRId64
, (int64_t) stream_id
);
1315 if (bt_ctf_stream_class_set_id_no_check(stream_class
,
1317 /* TODO Should retry with a different stream id */
1318 BT_LOGE("Cannot set stream class's ID: "
1319 "id=%" PRId64
, (int64_t) stream_id
);
1326 * At this point all the field types in the validation output
1327 * are valid. Validate the semantics of some scopes according to
1328 * the CTF specification.
1330 if (!packet_header_field_type_is_valid(trace
,
1331 trace_sc_validation_output
.packet_header_type
)) {
1332 BT_LOGW_STR("Invalid trace's packet header field type.");
1337 if (!packet_context_field_type_is_valid(trace
,
1339 trace_sc_validation_output
.packet_context_type
)) {
1340 BT_LOGW_STR("Invalid stream class's packet context field type.");
1345 if (!event_header_field_type_is_valid(trace
,
1347 trace_sc_validation_output
.event_header_type
)) {
1348 BT_LOGW_STR("Invalid steam class's event header field type.");
1354 * Now is the time to automatically map specific field types of
1355 * the stream class's packet context and event header field
1356 * types to the stream class's clock's class if they are not
1357 * mapped to a clock class yet. We do it here because we know
1358 * that after this point, everything is frozen so it won't be
1359 * possible for the user to modify the stream class's clock, or
1360 * to map those field types to other clock classes.
1362 if (trace
->is_created_by_writer
) {
1363 if (bt_ctf_stream_class_map_clock_class(stream_class
,
1364 trace_sc_validation_output
.packet_context_type
,
1365 trace_sc_validation_output
.event_header_type
)) {
1366 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1372 bt_object_set_parent(stream_class
, trace
);
1373 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1376 * At this point we know that the function will be successful.
1377 * Therefore we can replace the trace and stream class field
1378 * types with what's in their validation output structure and
1379 * mark them as valid. We can also replace the field types of
1380 * all the event classes of the stream class and mark them as
1383 bt_ctf_validation_replace_types(trace
, stream_class
, NULL
,
1384 &trace_sc_validation_output
, trace_sc_validation_flags
);
1386 stream_class
->valid
= 1;
1389 * Put what was not moved in bt_ctf_validation_replace_types().
1391 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1393 for (i
= 0; i
< event_class_count
; i
++) {
1394 struct bt_ctf_event_class
*event_class
=
1395 bt_ctf_stream_class_get_event_class_by_index(
1398 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
1399 &ec_validation_outputs
[i
], ec_validation_flags
);
1400 event_class
->valid
= 1;
1401 BT_PUT(event_class
);
1404 * Put what was not moved in
1405 * bt_ctf_validation_replace_types().
1407 bt_ctf_validation_output_put_types(&ec_validation_outputs
[i
]);
1411 * Freeze the trace and the stream class.
1413 bt_ctf_stream_class_freeze(stream_class
);
1414 bt_ctf_trace_freeze(trace
);
1416 /* Notifiy listeners of the trace's schema modification. */
1417 bt_ctf_stream_class_visit(stream_class
,
1418 bt_ctf_trace_object_modification
, trace
);
1419 BT_LOGD("Added stream class to trace: "
1420 "trace-addr=%p, trace-name=\"%s\", "
1421 "stream-class-addr=%p, stream-class-name=\"%s\", "
1422 "stream-class-id=%" PRId64
,
1423 trace
, bt_ctf_trace_get_name(trace
),
1424 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1425 bt_ctf_stream_class_get_id(stream_class
));
1429 bt_object_set_parent(stream_class
, NULL
);
1431 if (ec_validation_outputs
) {
1432 for (i
= 0; i
< event_class_count
; i
++) {
1433 bt_ctf_validation_output_put_types(
1434 &ec_validation_outputs
[i
]);
1439 g_free(ec_validation_outputs
);
1440 bt_ctf_validation_output_put_types(&trace_sc_validation_output
);
1441 bt_put(current_parent_trace
);
1442 assert(!packet_header_type
);
1443 assert(!packet_context_type
);
1444 assert(!event_header_type
);
1445 assert(!stream_event_ctx_type
);
1449 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
1454 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1459 ret
= (int64_t) trace
->streams
->len
;
1465 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
1466 struct bt_ctf_trace
*trace
,
1469 struct bt_ctf_stream
*stream
= NULL
;
1472 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1476 if (index
>= trace
->streams
->len
) {
1477 BT_LOGW("Invalid parameter: index is out of bounds: "
1478 "addr=%p, name=\"%s\", "
1479 "index=%" PRIu64
", count=%u",
1480 trace
, bt_ctf_trace_get_name(trace
),
1481 index
, trace
->streams
->len
);
1485 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1491 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
1496 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1501 ret
= (int64_t) trace
->stream_classes
->len
;
1506 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
1507 struct bt_ctf_trace
*trace
, uint64_t index
)
1509 struct bt_ctf_stream_class
*stream_class
= NULL
;
1512 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1516 if (index
>= trace
->stream_classes
->len
) {
1517 BT_LOGW("Invalid parameter: index is out of bounds: "
1518 "addr=%p, name=\"%s\", "
1519 "index=%" PRIu64
", count=%u",
1520 trace
, bt_ctf_trace_get_name(trace
),
1521 index
, trace
->stream_classes
->len
);
1525 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1526 bt_get(stream_class
);
1528 return stream_class
;
1531 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
1532 struct bt_ctf_trace
*trace
, uint64_t id_param
)
1535 struct bt_ctf_stream_class
*stream_class
= NULL
;
1536 int64_t id
= (int64_t) id_param
;
1539 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1544 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1545 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64
,
1546 trace
, bt_ctf_trace_get_name(trace
), id_param
);
1550 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1551 struct bt_ctf_stream_class
*stream_class_candidate
;
1553 stream_class_candidate
=
1554 g_ptr_array_index(trace
->stream_classes
, i
);
1556 if (bt_ctf_stream_class_get_id(stream_class_candidate
) ==
1558 stream_class
= stream_class_candidate
;
1559 bt_get(stream_class
);
1565 return stream_class
;
1568 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
1569 struct bt_ctf_trace
*trace
, const char *name
)
1572 struct bt_ctf_clock_class
*clock_class
= NULL
;
1575 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1580 BT_LOGW_STR("Invalid parameter: name is NULL.");
1584 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1585 struct bt_ctf_clock_class
*cur_clk
=
1586 g_ptr_array_index(trace
->clocks
, i
);
1587 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
1589 if (!cur_clk_name
) {
1593 if (!strcmp(cur_clk_name
, name
)) {
1594 clock_class
= cur_clk
;
1595 bt_get(clock_class
);
1605 bt_bool
bt_ctf_trace_has_clock_class(struct bt_ctf_trace
*trace
,
1606 struct bt_ctf_clock_class
*clock_class
)
1608 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1611 assert(clock_class
);
1613 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1618 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order
)
1622 switch (byte_order
) {
1623 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1626 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1629 case BT_CTF_BYTE_ORDER_NATIVE
:
1640 int append_trace_metadata(struct bt_ctf_trace
*trace
,
1641 struct metadata_context
*context
)
1643 unsigned char *uuid
= trace
->uuid
;
1646 if (trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
1647 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE at this point; "
1648 "set it with bt_ctf_trace_set_native_byte_order(): "
1649 "addr=%p, name=\"%s\"",
1650 trace
, bt_ctf_trace_get_name(trace
));
1655 g_string_append(context
->string
, "trace {\n");
1656 g_string_append(context
->string
, "\tmajor = 1;\n");
1657 g_string_append(context
->string
, "\tminor = 8;\n");
1658 assert(trace
->native_byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1659 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1660 trace
->native_byte_order
== BT_CTF_BYTE_ORDER_NETWORK
);
1662 if (trace
->uuid_set
) {
1663 g_string_append_printf(context
->string
,
1664 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1665 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1666 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1667 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1668 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1671 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1672 get_byte_order_string(trace
->native_byte_order
));
1674 if (trace
->packet_header_type
) {
1675 g_string_append(context
->string
, "\tpacket.header := ");
1676 context
->current_indentation_level
++;
1677 g_string_assign(context
->field_name
, "");
1678 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1679 ret
= bt_ctf_field_type_serialize(trace
->packet_header_type
,
1684 context
->current_indentation_level
--;
1687 g_string_append(context
->string
, ";\n};\n\n");
1693 void append_env_metadata(struct bt_ctf_trace
*trace
,
1694 struct metadata_context
*context
)
1699 env_size
= bt_ctf_attributes_get_count(trace
->environment
);
1700 if (env_size
<= 0) {
1704 g_string_append(context
->string
, "env {\n");
1706 for (i
= 0; i
< env_size
; i
++) {
1707 struct bt_value
*env_field_value_obj
= NULL
;
1708 const char *entry_name
;
1710 entry_name
= bt_ctf_attributes_get_field_name(
1711 trace
->environment
, i
);
1712 env_field_value_obj
= bt_ctf_attributes_get_field_value(
1713 trace
->environment
, i
);
1716 assert(env_field_value_obj
);
1718 switch (bt_value_get_type(env_field_value_obj
)) {
1719 case BT_VALUE_TYPE_INTEGER
:
1724 ret
= bt_value_integer_get(env_field_value_obj
,
1727 g_string_append_printf(context
->string
,
1728 "\t%s = %" PRId64
";\n", entry_name
,
1732 case BT_VALUE_TYPE_STRING
:
1735 const char *str_value
;
1736 char *escaped_str
= NULL
;
1738 ret
= bt_value_string_get(env_field_value_obj
,
1741 escaped_str
= g_strescape(str_value
, NULL
);
1743 BT_LOGE("Cannot escape string: string=\"%s\"",
1748 g_string_append_printf(context
->string
,
1749 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1758 BT_PUT(env_field_value_obj
);
1761 g_string_append(context
->string
, "};\n\n");
1764 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
1766 char *metadata
= NULL
;
1767 struct metadata_context
*context
= NULL
;
1772 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1776 context
= g_new0(struct metadata_context
, 1);
1778 BT_LOGE_STR("Failed to allocate one metadata context.");
1782 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1783 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1784 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1785 if (append_trace_metadata(trace
, context
)) {
1786 /* append_trace_metadata() logs errors */
1789 append_env_metadata(trace
, context
);
1790 g_ptr_array_foreach(trace
->clocks
,
1791 (GFunc
)bt_ctf_clock_class_serialize
, context
);
1793 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1794 /* bt_ctf_stream_class_serialize() logs details */
1795 err
= bt_ctf_stream_class_serialize(
1796 trace
->stream_classes
->pdata
[i
], context
);
1798 /* bt_ctf_stream_class_serialize() logs errors */
1803 metadata
= context
->string
->str
;
1806 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1807 g_string_free(context
->field_name
, TRUE
);
1814 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
1815 struct bt_ctf_trace
*trace
)
1817 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1820 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1824 ret
= trace
->native_byte_order
;
1830 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
1831 enum bt_ctf_byte_order byte_order
)
1836 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1841 if (trace
->frozen
) {
1842 BT_LOGW("Invalid parameter: trace is frozen: "
1843 "addr=%p, name=\"%s\"",
1844 trace
, bt_ctf_trace_get_name(trace
));
1849 if (byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
1850 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
1851 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
1852 BT_LOGW("Invalid parameter: invalid byte order: "
1853 "addr=%p, name=\"%s\", bo=%s",
1854 trace
, bt_ctf_trace_get_name(trace
),
1855 bt_ctf_byte_order_string(byte_order
));
1860 trace
->native_byte_order
= byte_order
;
1861 BT_LOGV("Set trace's native byte order: "
1862 "addr=%p, name=\"%s\", bo=%s",
1863 trace
, bt_ctf_trace_get_name(trace
),
1864 bt_ctf_byte_order_string(byte_order
));
1870 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_type(
1871 struct bt_ctf_trace
*trace
)
1873 struct bt_ctf_field_type
*field_type
= NULL
;
1876 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1880 bt_get(trace
->packet_header_type
);
1881 field_type
= trace
->packet_header_type
;
1886 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace
*trace
,
1887 struct bt_ctf_field_type
*packet_header_type
)
1892 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1897 if (trace
->frozen
) {
1898 BT_LOGW("Invalid parameter: trace is frozen: "
1899 "addr=%p, name=\"%s\"",
1900 trace
, bt_ctf_trace_get_name(trace
));
1905 /* packet_header_type must be a structure. */
1906 if (packet_header_type
&&
1907 !bt_ctf_field_type_is_structure(packet_header_type
)) {
1908 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1909 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1910 trace
, bt_ctf_trace_get_name(trace
),
1912 bt_ctf_field_type_id_string(packet_header_type
->id
));
1917 bt_put(trace
->packet_header_type
);
1918 trace
->packet_header_type
= bt_get(packet_header_type
);
1919 BT_LOGV("Set trace's packet header field type: "
1920 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1921 trace
, bt_ctf_trace_get_name(trace
), packet_header_type
);
1927 int64_t get_stream_class_count(void *element
)
1929 return bt_ctf_trace_get_stream_class_count(
1930 (struct bt_ctf_trace
*) element
);
1934 void *get_stream_class(void *element
, int i
)
1936 return bt_ctf_trace_get_stream_class_by_index(
1937 (struct bt_ctf_trace
*) element
, i
);
1941 int visit_stream_class(void *object
, bt_ctf_visitor visitor
,void *data
)
1943 return bt_ctf_stream_class_visit(object
, visitor
, data
);
1946 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
1947 bt_ctf_visitor visitor
, void *data
)
1950 struct bt_ctf_object obj
=
1951 { .object
= trace
, .type
= BT_CTF_OBJECT_TYPE_TRACE
};
1954 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1960 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1965 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1966 trace
, bt_ctf_trace_get_name(trace
));
1967 ret
= visitor_helper(&obj
, get_stream_class_count
,
1968 get_stream_class
, visit_stream_class
, visitor
, data
);
1974 int invoke_listener(struct bt_ctf_object
*object
, void *data
)
1976 struct listener_wrapper
*listener_wrapper
= data
;
1978 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1982 // TODO: add logging to this function once we use it internally.
1983 int bt_ctf_trace_add_listener(struct bt_ctf_trace
*trace
,
1984 bt_ctf_listener_cb listener
, void *listener_data
)
1987 struct listener_wrapper
*listener_wrapper
=
1988 g_new0(struct listener_wrapper
, 1);
1990 if (!trace
|| !listener
|| !listener_wrapper
) {
1995 listener_wrapper
->listener
= listener
;
1996 listener_wrapper
->data
= listener_data
;
1998 /* Visit the current schema. */
1999 ret
= bt_ctf_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2005 * Add listener to the array of callbacks which will be invoked on
2008 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2011 g_free(listener_wrapper
);
2016 int bt_ctf_trace_object_modification(struct bt_ctf_object
*object
,
2020 struct bt_ctf_trace
*trace
= trace_ptr
;
2025 if (trace
->listeners
->len
== 0) {
2029 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2030 struct listener_wrapper
*listener
=
2031 g_ptr_array_index(trace
->listeners
, i
);
2033 listener
->listener(object
, listener
->data
);
2040 struct bt_ctf_field_type
*get_field_type(enum field_type_alias alias
)
2043 unsigned int alignment
, size
;
2044 struct bt_ctf_field_type
*field_type
= NULL
;
2046 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2050 alignment
= field_type_aliases_alignments
[alias
];
2051 size
= field_type_aliases_sizes
[alias
];
2052 field_type
= bt_ctf_field_type_integer_create(size
);
2053 ret
= bt_ctf_field_type_set_alignment(field_type
, alignment
);
2062 void bt_ctf_trace_freeze(struct bt_ctf_trace
*trace
)
2066 if (trace
->frozen
) {
2070 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2071 trace
, bt_ctf_trace_get_name(trace
));
2072 BT_LOGD_STR("Freezing packet header field type.");
2073 bt_ctf_field_type_freeze(trace
->packet_header_type
);
2074 BT_LOGD_STR("Freezing environment attributes.");
2075 bt_ctf_attributes_freeze(trace
->environment
);
2077 if (trace
->clocks
->len
> 0) {
2078 BT_LOGD_STR("Freezing clock classes.");
2081 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2082 struct bt_ctf_clock_class
*clock_class
=
2083 g_ptr_array_index(trace
->clocks
, i
);
2085 bt_ctf_clock_class_freeze(clock_class
);
2091 bt_bool
bt_ctf_trace_is_static(struct bt_ctf_trace
*trace
)
2093 bt_bool is_static
= BT_FALSE
;
2096 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2100 is_static
= trace
->is_static
;
2106 int bt_ctf_trace_set_is_static(struct bt_ctf_trace
*trace
)
2111 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2116 trace
->is_static
= BT_TRUE
;
2117 bt_ctf_trace_freeze(trace
);
2118 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2119 trace
, bt_ctf_trace_get_name(trace
));