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/ctf-ir/utils-internal.h>
47 #include <babeltrace/compiler-internal.h>
48 #include <babeltrace/values.h>
49 #include <babeltrace/values-internal.h>
50 #include <babeltrace/ref.h>
51 #include <babeltrace/types.h>
52 #include <babeltrace/endian-internal.h>
58 #define DEFAULT_IDENTIFIER_SIZE 128
59 #define DEFAULT_METADATA_STRING_SIZE 4096
61 struct listener_wrapper
{
62 bt_listener_cb listener
;
66 struct bt_trace_is_static_listener_elem
{
67 bt_trace_is_static_listener func
;
68 bt_trace_listener_removed removed
;
73 void bt_trace_destroy(struct bt_object
*obj
);
75 void bt_trace_freeze(struct bt_trace
*trace
);
78 const unsigned int field_type_aliases_alignments
[] = {
79 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
80 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
81 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
82 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
86 const unsigned int field_type_aliases_sizes
[] = {
87 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
88 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
89 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
90 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
91 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
92 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
95 struct bt_trace
*bt_trace_create(void)
97 struct bt_trace
*trace
= NULL
;
99 trace
= g_new0(struct bt_trace
, 1);
101 BT_LOGE_STR("Failed to allocate one trace.");
105 BT_LOGD_STR("Creating trace object.");
106 trace
->native_byte_order
= BT_BYTE_ORDER_UNSPECIFIED
;
107 bt_object_init(trace
, bt_trace_destroy
);
108 trace
->clocks
= g_ptr_array_new_with_free_func(
109 (GDestroyNotify
) bt_put
);
110 if (!trace
->clocks
) {
111 BT_LOGE_STR("Failed to allocate one GPtrArray.");
115 trace
->streams
= g_ptr_array_new_with_free_func(
116 (GDestroyNotify
) bt_object_release
);
117 if (!trace
->streams
) {
118 BT_LOGE_STR("Failed to allocate one GPtrArray.");
122 trace
->stream_classes
= g_ptr_array_new_with_free_func(
123 (GDestroyNotify
) bt_object_release
);
124 if (!trace
->stream_classes
) {
125 BT_LOGE_STR("Failed to allocate one GPtrArray.");
129 /* Create the environment array object */
130 trace
->environment
= bt_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 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
144 sizeof(struct bt_trace_is_static_listener_elem
));
145 if (!trace
->is_static_listeners
) {
146 BT_LOGE_STR("Failed to allocate one GArray.");
150 BT_LOGD("Created trace object: addr=%p", trace
);
158 const char *bt_trace_get_name(struct bt_trace
*trace
)
160 const char *name
= NULL
;
163 BT_LOGW_STR("Invalid parameter: trace is NULL.");
171 name
= trace
->name
->str
;
176 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
181 BT_LOGW_STR("Invalid parameter: trace is NULL.");
187 BT_LOGW_STR("Invalid parameter: name is NULL.");
193 BT_LOGW("Invalid parameter: trace is frozen: "
194 "addr=%p, name=\"%s\"",
195 trace
, bt_trace_get_name(trace
));
200 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
203 BT_LOGE_STR("Failed to allocate one GString.");
208 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
214 const unsigned char *bt_trace_get_uuid(struct bt_trace
*trace
)
216 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
219 int bt_trace_set_uuid(struct bt_trace
*trace
, const unsigned char *uuid
)
224 BT_LOGW_STR("Invalid parameter: trace is NULL.");
230 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
236 BT_LOGW("Invalid parameter: trace is frozen: "
237 "addr=%p, name=\"%s\"",
238 trace
, bt_trace_get_name(trace
));
243 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
244 trace
->uuid_set
= BT_TRUE
;
245 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
246 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
247 trace
, bt_trace_get_name(trace
),
248 (unsigned int) uuid
[0],
249 (unsigned int) uuid
[1],
250 (unsigned int) uuid
[2],
251 (unsigned int) uuid
[3],
252 (unsigned int) uuid
[4],
253 (unsigned int) uuid
[5],
254 (unsigned int) uuid
[6],
255 (unsigned int) uuid
[7],
256 (unsigned int) uuid
[8],
257 (unsigned int) uuid
[9],
258 (unsigned int) uuid
[10],
259 (unsigned int) uuid
[11],
260 (unsigned int) uuid
[12],
261 (unsigned int) uuid
[13],
262 (unsigned int) uuid
[14],
263 (unsigned int) uuid
[15]);
269 void bt_trace_destroy(struct bt_object
*obj
)
271 struct bt_trace
*trace
;
273 trace
= container_of(obj
, struct bt_trace
, base
);
275 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
276 trace
, bt_trace_get_name(trace
));
279 * Call remove listeners first so that everything else still
280 * exists in the trace.
282 if (trace
->is_static_listeners
) {
285 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
286 struct bt_trace_is_static_listener_elem elem
=
287 g_array_index(trace
->is_static_listeners
,
288 struct bt_trace_is_static_listener_elem
, i
);
291 elem
.removed(trace
, elem
.data
);
295 g_array_free(trace
->is_static_listeners
, TRUE
);
298 if (trace
->listeners
) {
299 g_ptr_array_free(trace
->listeners
, TRUE
);
302 if (trace
->environment
) {
303 BT_LOGD_STR("Destroying environment attributes.");
304 bt_attributes_destroy(trace
->environment
);
308 g_string_free(trace
->name
, TRUE
);
312 BT_LOGD_STR("Putting clock classes.");
313 g_ptr_array_free(trace
->clocks
, TRUE
);
316 if (trace
->streams
) {
317 BT_LOGD_STR("Destroying streams.");
318 g_ptr_array_free(trace
->streams
, TRUE
);
321 if (trace
->stream_classes
) {
322 BT_LOGD_STR("Destroying stream classes.");
323 g_ptr_array_free(trace
->stream_classes
, TRUE
);
326 BT_LOGD_STR("Putting packet header field type.");
327 bt_put(trace
->packet_header_type
);
331 int bt_trace_set_environment_field(struct bt_trace
*trace
,
332 const char *name
, struct bt_value
*value
)
337 BT_LOGW_STR("Invalid parameter: trace is NULL.");
343 BT_LOGW_STR("Invalid parameter: name is NULL.");
349 BT_LOGW_STR("Invalid parameter: value is NULL.");
354 if (!bt_identifier_is_valid(name
)) {
355 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
356 "trace-addr=%p, trace-name=\"%s\", "
358 trace
, bt_trace_get_name(trace
), name
);
363 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
364 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
365 "trace-addr=%p, trace-name=\"%s\", "
366 "env-name=\"%s\", env-value-type=%s",
367 trace
, bt_trace_get_name(trace
), name
,
368 bt_value_type_string(bt_value_get_type(value
)));
373 if (trace
->is_static
) {
374 BT_LOGW("Invalid parameter: trace is static: "
375 "addr=%p, name=\"%s\"",
376 trace
, bt_trace_get_name(trace
));
383 * New environment fields may be added to a frozen trace,
384 * but existing fields may not be changed.
386 * The object passed is frozen like all other attributes.
388 struct bt_value
*attribute
=
389 bt_attributes_get_field_value_by_name(
390 trace
->environment
, name
);
393 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
394 "trace-addr=%p, trace-name=\"%s\", "
396 trace
, bt_trace_get_name(trace
), name
);
402 bt_value_freeze(value
);
405 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
408 BT_LOGE("Cannot set environment field's value: "
409 "trace-addr=%p, trace-name=\"%s\", "
411 trace
, bt_trace_get_name(trace
), name
);
413 BT_LOGV("Set environment field's value: "
414 "trace-addr=%p, trace-name=\"%s\", "
415 "env-name=\"%s\", value-addr=%p",
416 trace
, bt_trace_get_name(trace
), name
, value
);
423 int bt_trace_set_environment_field_string(struct bt_trace
*trace
,
424 const char *name
, const char *value
)
427 struct bt_value
*env_value_string_obj
= NULL
;
430 BT_LOGW_STR("Invalid parameter: value is NULL.");
435 env_value_string_obj
= bt_value_string_create_init(value
);
436 if (!env_value_string_obj
) {
437 BT_LOGE_STR("Cannot create string value object.");
442 /* bt_trace_set_environment_field() logs errors */
443 ret
= bt_trace_set_environment_field(trace
, name
,
444 env_value_string_obj
);
447 bt_put(env_value_string_obj
);
451 int bt_trace_set_environment_field_integer(struct bt_trace
*trace
,
452 const char *name
, int64_t value
)
455 struct bt_value
*env_value_integer_obj
= NULL
;
457 env_value_integer_obj
= bt_value_integer_create_init(value
);
458 if (!env_value_integer_obj
) {
459 BT_LOGE_STR("Cannot create integer value object.");
464 /* bt_trace_set_environment_field() logs errors */
465 ret
= bt_trace_set_environment_field(trace
, name
,
466 env_value_integer_obj
);
469 bt_put(env_value_integer_obj
);
473 int64_t bt_trace_get_environment_field_count(struct bt_trace
*trace
)
478 BT_LOGW_STR("Invalid parameter: trace is NULL.");
483 ret
= bt_attributes_get_count(trace
->environment
);
491 bt_trace_get_environment_field_name_by_index(struct bt_trace
*trace
,
494 const char *ret
= NULL
;
497 BT_LOGW_STR("Invalid parameter: trace is NULL.");
501 ret
= bt_attributes_get_field_name(trace
->environment
, index
);
507 struct bt_value
*bt_trace_get_environment_field_value_by_index(
508 struct bt_trace
*trace
, uint64_t index
)
510 struct bt_value
*ret
= NULL
;
513 BT_LOGW_STR("Invalid parameter: trace is NULL.");
517 ret
= bt_attributes_get_field_value(trace
->environment
, index
);
523 struct bt_value
*bt_trace_get_environment_field_value_by_name(
524 struct bt_trace
*trace
, const char *name
)
526 struct bt_value
*ret
= NULL
;
529 BT_LOGW_STR("Invalid parameter: trace is NULL.");
534 BT_LOGW_STR("Invalid parameter: name is NULL.");
538 ret
= bt_attributes_get_field_value_by_name(trace
->environment
,
545 int bt_trace_add_clock_class(struct bt_trace
*trace
,
546 struct bt_clock_class
*clock_class
)
551 BT_LOGW_STR("Invalid parameter: trace is NULL.");
556 if (trace
->is_static
) {
557 BT_LOGW("Invalid parameter: trace is static: "
558 "addr=%p, name=\"%s\"",
559 trace
, bt_trace_get_name(trace
));
564 if (!bt_clock_class_is_valid(clock_class
)) {
565 BT_LOGW("Invalid parameter: clock class is invalid: "
566 "trace-addr=%p, trace-name=\"%s\", "
567 "clock-class-addr=%p, clock-class-name=\"%s\"",
568 trace
, bt_trace_get_name(trace
),
569 clock_class
, bt_clock_class_get_name(clock_class
));
574 /* Check for duplicate clock classes */
575 if (bt_trace_has_clock_class(trace
, clock_class
)) {
576 BT_LOGW("Invalid parameter: clock class already exists in trace: "
577 "trace-addr=%p, trace-name=\"%s\", "
578 "clock-class-addr=%p, clock-class-name=\"%s\"",
579 trace
, bt_trace_get_name(trace
),
580 clock_class
, bt_clock_class_get_name(clock_class
));
586 g_ptr_array_add(trace
->clocks
, clock_class
);
589 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
590 bt_clock_class_freeze(clock_class
);
593 BT_LOGV("Added clock class to trace: "
594 "trace-addr=%p, trace-name=\"%s\", "
595 "clock-class-addr=%p, clock-class-name=\"%s\"",
596 trace
, bt_trace_get_name(trace
),
597 clock_class
, bt_clock_class_get_name(clock_class
));
603 int64_t bt_trace_get_clock_class_count(struct bt_trace
*trace
)
605 int64_t ret
= (int64_t) -1;
608 BT_LOGW_STR("Invalid parameter: trace is NULL.");
612 ret
= trace
->clocks
->len
;
617 struct bt_clock_class
*bt_trace_get_clock_class_by_index(
618 struct bt_trace
*trace
, uint64_t index
)
620 struct bt_clock_class
*clock_class
= NULL
;
623 BT_LOGW_STR("Invalid parameter: trace is NULL.");
627 if (index
>= trace
->clocks
->len
) {
628 BT_LOGW("Invalid parameter: index is out of bounds: "
629 "addr=%p, name=\"%s\", "
630 "index=%" PRIu64
", count=%u",
631 trace
, bt_trace_get_name(trace
),
632 index
, trace
->clocks
->len
);
636 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
643 bool packet_header_field_type_is_valid(struct bt_trace
*trace
,
644 struct bt_field_type
*packet_header_type
)
647 bool is_valid
= true;
648 struct bt_field_type
*field_type
= NULL
;
650 if (!packet_header_type
) {
652 * No packet header field type: trace must have only
653 * one stream. At this point the stream class being
654 * added is not part of the trace yet, so we validate
655 * that the trace contains no stream classes yet.
657 if (trace
->stream_classes
->len
>= 1) {
658 BT_LOGW_STR("Invalid packet header field type: "
659 "packet header field type does not exist but there's more than one stream class in the trace.");
663 /* No packet header field type: valid at this point */
667 /* Packet header field type, if it exists, must be a structure */
668 if (!bt_field_type_is_structure(packet_header_type
)) {
669 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
670 "ft-addr=%p, ft-id=%s",
672 bt_field_type_id_string(packet_header_type
->id
));
677 * If there's a `magic` field, it must be a 32-bit unsigned
678 * integer field type. Also it must be the first field of the
679 * packet header field type.
681 field_type
= bt_field_type_structure_get_field_type_by_name(
682 packet_header_type
, "magic");
684 const char *field_name
;
686 if (!bt_field_type_is_integer(field_type
)) {
687 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
688 "magic-ft-addr=%p, magic-ft-id=%s",
690 bt_field_type_id_string(field_type
->id
));
694 if (bt_field_type_integer_is_signed(field_type
)) {
695 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
696 "magic-ft-addr=%p", field_type
);
700 if (bt_field_type_integer_get_size(field_type
) != 32) {
701 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
702 "magic-ft-addr=%p, magic-ft-size=%u",
704 bt_field_type_integer_get_size(field_type
));
708 ret
= bt_field_type_structure_get_field_by_index(
709 packet_header_type
, &field_name
, NULL
, 0);
712 if (strcmp(field_name
, "magic") != 0) {
713 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
714 "magic-ft-addr=%p, first-field-name=\"%s\"",
715 field_type
, field_name
);
723 * If there's a `uuid` field, it must be an array field type of
724 * length 16 with an 8-bit unsigned integer element field type.
726 field_type
= bt_field_type_structure_get_field_type_by_name(
727 packet_header_type
, "uuid");
729 struct bt_field_type
*elem_ft
;
731 if (!bt_field_type_is_array(field_type
)) {
732 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
733 "uuid-ft-addr=%p, uuid-ft-id=%s",
735 bt_field_type_id_string(field_type
->id
));
739 if (bt_field_type_array_get_length(field_type
) != 16) {
740 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
741 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
743 bt_field_type_array_get_length(field_type
));
747 elem_ft
= bt_field_type_array_get_element_type(field_type
);
750 if (!bt_field_type_is_integer(elem_ft
)) {
751 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
752 "elem-ft-addr=%p, elem-ft-id=%s",
754 bt_field_type_id_string(elem_ft
->id
));
759 if (bt_field_type_integer_is_signed(elem_ft
)) {
760 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
761 "elem-ft-addr=%p", elem_ft
);
766 if (bt_field_type_integer_get_size(elem_ft
) != 8) {
767 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
768 "elem-ft-addr=%p, elem-ft-size=%u",
770 bt_field_type_integer_get_size(elem_ft
));
780 * The `stream_id` field must exist if there's more than one
781 * stream classes in the trace.
783 field_type
= bt_field_type_structure_get_field_type_by_name(
784 packet_header_type
, "stream_id");
786 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
787 BT_LOGW_STR("Invalid packet header field type: "
788 "`stream_id` field does not exist but there's more than one stream class in the trace.");
793 * If there's a `stream_id` field, it must be an unsigned
794 * integer field type.
797 if (!bt_field_type_is_integer(field_type
)) {
798 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
799 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
801 bt_field_type_id_string(field_type
->id
));
805 if (bt_field_type_integer_is_signed(field_type
)) {
806 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
807 "stream-id-ft-addr=%p", field_type
);
815 * If there's a `packet_seq_num` field, it must be an unsigned
816 * integer field type.
818 field_type
= bt_field_type_structure_get_field_type_by_name(
819 packet_header_type
, "packet_seq_num");
821 if (!bt_field_type_is_integer(field_type
)) {
822 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
823 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
825 bt_field_type_id_string(field_type
->id
));
829 if (bt_field_type_integer_is_signed(field_type
)) {
830 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
831 "packet-seq-num-ft-addr=%p", field_type
);
849 bool packet_context_field_type_is_valid(struct bt_trace
*trace
,
850 struct bt_stream_class
*stream_class
,
851 struct bt_field_type
*packet_context_type
)
853 bool is_valid
= true;
854 struct bt_field_type
*field_type
= NULL
;
856 if (!packet_context_type
) {
857 /* No packet context field type: valid at this point */
861 /* Packet context field type, if it exists, must be a structure */
862 if (!bt_field_type_is_structure(packet_context_type
)) {
863 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
864 "ft-addr=%p, ft-id=%s",
866 bt_field_type_id_string(packet_context_type
->id
));
871 * If there's a `packet_size` field, it must be an unsigned
872 * integer field type.
874 field_type
= bt_field_type_structure_get_field_type_by_name(
875 packet_context_type
, "packet_size");
877 if (!bt_field_type_is_integer(field_type
)) {
878 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
879 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
881 bt_field_type_id_string(field_type
->id
));
885 if (bt_field_type_integer_is_signed(field_type
)) {
886 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
887 "packet-size-ft-addr=%p", field_type
);
895 * If there's a `content_size` field, it must be an unsigned
896 * integer field type.
898 field_type
= bt_field_type_structure_get_field_type_by_name(
899 packet_context_type
, "content_size");
901 if (!bt_field_type_is_integer(field_type
)) {
902 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
903 "content-size-ft-addr=%p, content-size-ft-id=%s",
905 bt_field_type_id_string(field_type
->id
));
909 if (bt_field_type_integer_is_signed(field_type
)) {
910 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
911 "content-size-ft-addr=%p", field_type
);
919 * If there's a `events_discarded` field, it must be an unsigned
920 * integer field type.
922 field_type
= bt_field_type_structure_get_field_type_by_name(
923 packet_context_type
, "events_discarded");
925 if (!bt_field_type_is_integer(field_type
)) {
926 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
927 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
929 bt_field_type_id_string(field_type
->id
));
933 if (bt_field_type_integer_is_signed(field_type
)) {
934 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
935 "events-discarded-ft-addr=%p", field_type
);
943 * If there's a `timestamp_begin` field, it must be an unsigned
944 * integer field type. Also, if the trace is not a CTF writer's
945 * trace, then we cannot automatically set the mapped clock
946 * class of this field, so it must have a mapped clock class.
948 field_type
= bt_field_type_structure_get_field_type_by_name(
949 packet_context_type
, "timestamp_begin");
951 if (!bt_field_type_is_integer(field_type
)) {
952 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
953 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
955 bt_field_type_id_string(field_type
->id
));
959 if (bt_field_type_integer_is_signed(field_type
)) {
960 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
961 "timestamp-begin-ft-addr=%p", field_type
);
965 if (!trace
->is_created_by_writer
) {
966 struct bt_clock_class
*clock_class
=
967 bt_field_type_integer_get_mapped_clock_class(
972 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
973 "timestamp-begin-ft-addr=%p", field_type
);
982 * If there's a `timestamp_end` field, it must be an unsigned
983 * integer field type. Also, if the trace is not a CTF writer's
984 * trace, then we cannot automatically set the mapped clock
985 * class of this field, so it must have a mapped clock class.
987 field_type
= bt_field_type_structure_get_field_type_by_name(
988 packet_context_type
, "timestamp_end");
990 if (!bt_field_type_is_integer(field_type
)) {
991 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
992 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
994 bt_field_type_id_string(field_type
->id
));
998 if (bt_field_type_integer_is_signed(field_type
)) {
999 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
1000 "timestamp-end-ft-addr=%p", field_type
);
1004 if (!trace
->is_created_by_writer
) {
1005 struct bt_clock_class
*clock_class
=
1006 bt_field_type_integer_get_mapped_clock_class(
1009 bt_put(clock_class
);
1011 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
1012 "timestamp-end-ft-addr=%p", field_type
);
1031 bool event_header_field_type_is_valid(struct bt_trace
*trace
,
1032 struct bt_stream_class
*stream_class
,
1033 struct bt_field_type
*event_header_type
)
1035 bool is_valid
= true;
1036 struct bt_field_type
*field_type
= NULL
;
1039 * We do not validate that the `timestamp` field exists here
1040 * because CTF does not require this exact name to be mapped to
1044 if (!event_header_type
) {
1046 * No event header field type: stream class must have
1047 * only one event class.
1049 if (bt_stream_class_get_event_class_count(stream_class
) > 1) {
1050 BT_LOGW_STR("Invalid event header field type: "
1051 "event header field type does not exist but there's more than one event class in the stream class.");
1055 /* No event header field type: valid at this point */
1059 /* Event header field type, if it exists, must be a structure */
1060 if (!bt_field_type_is_structure(event_header_type
)) {
1061 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1062 "ft-addr=%p, ft-id=%s",
1064 bt_field_type_id_string(event_header_type
->id
));
1069 * If there's an `id` field, it must be an unsigned integer
1070 * field type or an enumeration field type with an unsigned
1071 * integer container field type.
1073 field_type
= bt_field_type_structure_get_field_type_by_name(
1074 event_header_type
, "id");
1076 struct bt_field_type
*int_ft
;
1078 if (bt_field_type_is_integer(field_type
)) {
1079 int_ft
= bt_get(field_type
);
1080 } else if (bt_field_type_is_enumeration(field_type
)) {
1081 int_ft
= bt_field_type_enumeration_get_container_type(
1084 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1085 "id-ft-addr=%p, id-ft-id=%s",
1087 bt_field_type_id_string(field_type
->id
));
1092 if (bt_field_type_integer_is_signed(int_ft
)) {
1093 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1094 "id-ft-addr=%p", int_ft
);
1113 int check_packet_header_type_has_no_clock_class(struct bt_trace
*trace
)
1117 if (trace
->packet_header_type
) {
1118 struct bt_clock_class
*clock_class
= NULL
;
1120 ret
= bt_validate_single_clock_class(trace
->packet_header_type
,
1122 bt_put(clock_class
);
1123 if (ret
|| clock_class
) {
1124 BT_LOGW("Trace's packet header field type cannot "
1125 "contain a field type which is mapped to "
1127 "trace-addr=%p, trace-name=\"%s\", "
1128 "clock-class-name=\"%s\"",
1129 trace
, bt_trace_get_name(trace
),
1131 bt_clock_class_get_name(clock_class
) :
1140 int bt_trace_add_stream_class(struct bt_trace
*trace
,
1141 struct bt_stream_class
*stream_class
)
1146 struct bt_validation_output trace_sc_validation_output
= { 0 };
1147 struct bt_validation_output
*ec_validation_outputs
= NULL
;
1148 const enum bt_validation_flag trace_sc_validation_flags
=
1149 BT_VALIDATION_FLAG_TRACE
|
1150 BT_VALIDATION_FLAG_STREAM
;
1151 const enum bt_validation_flag ec_validation_flags
=
1152 BT_VALIDATION_FLAG_EVENT
;
1153 struct bt_field_type
*packet_header_type
= NULL
;
1154 struct bt_field_type
*packet_context_type
= NULL
;
1155 struct bt_field_type
*event_header_type
= NULL
;
1156 struct bt_field_type
*stream_event_ctx_type
= NULL
;
1157 int64_t event_class_count
;
1158 struct bt_trace
*current_parent_trace
= NULL
;
1159 struct bt_clock_class
*expected_clock_class
= NULL
;
1162 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1167 if (!stream_class
) {
1168 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1173 if (trace
->is_static
) {
1174 BT_LOGW_STR("Invalid parameter: trace is static.");
1179 BT_LOGD("Adding stream class to trace: "
1180 "trace-addr=%p, trace-name=\"%s\", "
1181 "stream-class-addr=%p, stream-class-name=\"%s\", "
1182 "stream-class-id=%" PRId64
,
1183 trace
, bt_trace_get_name(trace
),
1184 stream_class
, bt_stream_class_get_name(stream_class
),
1185 bt_stream_class_get_id(stream_class
));
1187 current_parent_trace
= bt_stream_class_get_trace(stream_class
);
1188 if (current_parent_trace
) {
1189 /* Stream class is already associated to a trace, abort. */
1190 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1191 "stream-class-trace-addr=%p, "
1192 "stream-class-trace-name=\"%s\"",
1193 current_parent_trace
,
1194 bt_trace_get_name(current_parent_trace
));
1200 bt_stream_class_get_event_class_count(stream_class
);
1201 assert(event_class_count
>= 0);
1203 if (stream_class
->clock
) {
1204 struct bt_clock_class
*stream_clock_class
=
1205 stream_class
->clock
->clock_class
;
1207 if (trace
->is_created_by_writer
) {
1209 * Make sure this clock was also added to the
1210 * trace (potentially through its CTF writer
1215 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1216 if (trace
->clocks
->pdata
[i
] ==
1217 stream_clock_class
) {
1223 if (i
== trace
->clocks
->len
) {
1225 BT_LOGW("Stream class's clock's class is not part of the trace: "
1226 "clock-class-addr=%p, clock-class-name=\"%s\"",
1228 bt_clock_class_get_name(stream_clock_class
));
1234 * This trace was NOT created by a CTF writer,
1235 * thus do not allow the stream class to add to
1236 * have a clock at all. Those are two
1237 * independent APIs (non-writer and writer
1238 * APIs), and isolating them simplifies things.
1240 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1241 "clock-class-addr=%p, clock-class-name=\"%s\"",
1243 bt_clock_class_get_name(stream_clock_class
));
1248 if (stream_class
->clock_class
&&
1249 stream_class
->clock_class
!=
1250 stream_class
->clock
->clock_class
) {
1252 * Stream class already has an expected clock
1253 * class, but it does not match its clock's
1256 BT_LOGW("Invalid parameter: stream class's clock's "
1257 "class does not match stream class's "
1258 "expected clock class: "
1259 "stream-class-addr=%p, "
1260 "stream-class-id=%" PRId64
", "
1261 "stream-class-name=\"%s\", "
1262 "expected-clock-class-addr=%p, "
1263 "expected-clock-class-name=\"%s\"",
1265 bt_stream_class_get_id(stream_class
),
1266 bt_stream_class_get_name(stream_class
),
1267 expected_clock_class
,
1268 bt_clock_class_get_name(expected_clock_class
));
1269 } else if (!stream_class
->clock_class
) {
1271 * Set expected clock class to stream class's
1274 expected_clock_class
=
1275 bt_get(stream_class
->clock
->clock_class
);
1279 if (!stream_class
->frozen
) {
1281 * Stream class is not frozen yet. Validate that the
1282 * stream class contains at most a single clock class
1283 * because the previous
1284 * bt_stream_class_add_event_class() calls did not make
1285 * this validation since the stream class's direct field
1286 * types (packet context, event header, event context)
1287 * could change afterwards. This stream class is about
1288 * to be frozen and those field types won't be changed
1289 * if this function succeeds.
1291 * At this point we're also sure that the stream class's
1292 * clock, if any, has the same class as the stream
1293 * class's expected clock class, if any. This is why, if
1294 * bt_stream_class_validate_single_clock_class()
1295 * succeeds below, the call to
1296 * bt_stream_class_map_clock_class() at the end of this
1297 * function is safe because it maps to the same, single
1300 ret
= bt_stream_class_validate_single_clock_class(stream_class
,
1301 &expected_clock_class
);
1303 BT_LOGW("Invalid parameter: stream class or one of its "
1304 "event classes contains a field type which is "
1305 "not recursively mapped to the expected "
1307 "stream-class-addr=%p, "
1308 "stream-class-id=%" PRId64
", "
1309 "stream-class-name=\"%s\", "
1310 "expected-clock-class-addr=%p, "
1311 "expected-clock-class-name=\"%s\"",
1312 stream_class
, bt_stream_class_get_id(stream_class
),
1313 bt_stream_class_get_name(stream_class
),
1314 expected_clock_class
,
1315 expected_clock_class
?
1316 bt_clock_class_get_name(expected_clock_class
) :
1322 ret
= check_packet_header_type_has_no_clock_class(trace
);
1324 /* check_packet_header_type_has_no_clock_class() logs errors */
1329 * We're about to freeze both the trace and the stream class.
1330 * Also, each event class contained in this stream class are
1333 * This trace, this stream class, and all its event classes
1334 * should be valid at this point.
1336 * Validate trace and stream class first, then each event
1337 * class of this stream class can be validated individually.
1339 packet_header_type
=
1340 bt_trace_get_packet_header_type(trace
);
1341 packet_context_type
=
1342 bt_stream_class_get_packet_context_type(stream_class
);
1344 bt_stream_class_get_event_header_type(stream_class
);
1345 stream_event_ctx_type
=
1346 bt_stream_class_get_event_context_type(stream_class
);
1348 BT_LOGD("Validating trace and stream class field types.");
1349 ret
= bt_validate_class_types(trace
->environment
,
1350 packet_header_type
, packet_context_type
, event_header_type
,
1351 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1352 stream_class
->valid
, 1, &trace_sc_validation_output
,
1353 trace_sc_validation_flags
);
1354 BT_PUT(packet_header_type
);
1355 BT_PUT(packet_context_type
);
1356 BT_PUT(event_header_type
);
1357 BT_PUT(stream_event_ctx_type
);
1361 * This means something went wrong during the validation
1362 * process, not that the objects are invalid.
1364 BT_LOGE("Failed to validate trace and stream class field types: "
1369 if ((trace_sc_validation_output
.valid_flags
&
1370 trace_sc_validation_flags
) !=
1371 trace_sc_validation_flags
) {
1372 /* Invalid trace/stream class */
1373 BT_LOGW("Invalid trace or stream class field types: "
1375 trace_sc_validation_output
.valid_flags
);
1380 if (event_class_count
> 0) {
1381 ec_validation_outputs
= g_new0(struct bt_validation_output
,
1383 if (!ec_validation_outputs
) {
1384 BT_LOGE_STR("Failed to allocate one validation output structure.");
1390 /* Validate each event class individually */
1391 for (i
= 0; i
< event_class_count
; i
++) {
1392 struct bt_event_class
*event_class
=
1393 bt_stream_class_get_event_class_by_index(
1395 struct bt_field_type
*event_context_type
= NULL
;
1396 struct bt_field_type
*event_payload_type
= NULL
;
1398 event_context_type
=
1399 bt_event_class_get_context_type(event_class
);
1400 event_payload_type
=
1401 bt_event_class_get_payload_type(event_class
);
1404 * It is important to use the field types returned by
1405 * the previous trace and stream class validation here
1406 * because copies could have been made.
1408 BT_LOGD("Validating event class's field types: "
1409 "addr=%p, name=\"%s\", id=%" PRId64
,
1410 event_class
, bt_event_class_get_name(event_class
),
1411 bt_event_class_get_id(event_class
));
1412 ret
= bt_validate_class_types(trace
->environment
,
1413 trace_sc_validation_output
.packet_header_type
,
1414 trace_sc_validation_output
.packet_context_type
,
1415 trace_sc_validation_output
.event_header_type
,
1416 trace_sc_validation_output
.stream_event_ctx_type
,
1417 event_context_type
, event_payload_type
,
1418 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1419 ec_validation_flags
);
1420 BT_PUT(event_context_type
);
1421 BT_PUT(event_payload_type
);
1422 BT_PUT(event_class
);
1425 BT_LOGE("Failed to validate event class field types: "
1430 if ((ec_validation_outputs
[i
].valid_flags
&
1431 ec_validation_flags
) != ec_validation_flags
) {
1432 /* Invalid event class */
1433 BT_LOGW("Invalid event class field types: "
1435 ec_validation_outputs
[i
].valid_flags
);
1441 stream_id
= bt_stream_class_get_id(stream_class
);
1442 if (stream_id
< 0) {
1443 stream_id
= trace
->next_stream_id
++;
1444 if (stream_id
< 0) {
1445 BT_LOGE_STR("No more stream class IDs available.");
1450 /* Try to assign a new stream id */
1451 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1452 if (stream_id
== bt_stream_class_get_id(
1453 trace
->stream_classes
->pdata
[i
])) {
1454 /* Duplicate stream id found */
1455 BT_LOGW("Duplicate stream class ID: "
1456 "id=%" PRId64
, (int64_t) stream_id
);
1462 if (bt_stream_class_set_id_no_check(stream_class
,
1464 /* TODO Should retry with a different stream id */
1465 BT_LOGE("Cannot set stream class's ID: "
1466 "id=%" PRId64
, (int64_t) stream_id
);
1473 * At this point all the field types in the validation output
1474 * are valid. Validate the semantics of some scopes according to
1475 * the CTF specification.
1477 if (!packet_header_field_type_is_valid(trace
,
1478 trace_sc_validation_output
.packet_header_type
)) {
1479 BT_LOGW_STR("Invalid trace's packet header field type.");
1484 if (!packet_context_field_type_is_valid(trace
,
1486 trace_sc_validation_output
.packet_context_type
)) {
1487 BT_LOGW_STR("Invalid stream class's packet context field type.");
1492 if (!event_header_field_type_is_valid(trace
,
1494 trace_sc_validation_output
.event_header_type
)) {
1495 BT_LOGW_STR("Invalid steam class's event header field type.");
1501 * Now is the time to automatically map specific field types of
1502 * the stream class's packet context and event header field
1503 * types to the stream class's clock's class if they are not
1504 * mapped to a clock class yet. We do it here because we know
1505 * that after this point, everything is frozen so it won't be
1506 * possible for the user to modify the stream class's clock, or
1507 * to map those field types to other clock classes.
1509 if (trace
->is_created_by_writer
) {
1510 if (bt_stream_class_map_clock_class(stream_class
,
1511 trace_sc_validation_output
.packet_context_type
,
1512 trace_sc_validation_output
.event_header_type
)) {
1513 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1519 bt_object_set_parent(stream_class
, trace
);
1520 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1523 * At this point we know that the function will be successful.
1524 * Therefore we can replace the trace and stream class field
1525 * types with what's in their validation output structure and
1526 * mark them as valid. We can also replace the field types of
1527 * all the event classes of the stream class and mark them as
1530 bt_validation_replace_types(trace
, stream_class
, NULL
,
1531 &trace_sc_validation_output
, trace_sc_validation_flags
);
1533 stream_class
->valid
= 1;
1536 * Put what was not moved in bt_validation_replace_types().
1538 bt_validation_output_put_types(&trace_sc_validation_output
);
1540 for (i
= 0; i
< event_class_count
; i
++) {
1541 struct bt_event_class
*event_class
=
1542 bt_stream_class_get_event_class_by_index(
1545 bt_validation_replace_types(NULL
, NULL
, event_class
,
1546 &ec_validation_outputs
[i
], ec_validation_flags
);
1547 event_class
->valid
= 1;
1548 BT_PUT(event_class
);
1551 * Put what was not moved in
1552 * bt_validation_replace_types().
1554 bt_validation_output_put_types(&ec_validation_outputs
[i
]);
1558 * Freeze the trace and the stream class.
1560 bt_stream_class_freeze(stream_class
);
1561 bt_trace_freeze(trace
);
1564 * It is safe to set the stream class's unique clock class
1565 * now because the stream class is frozen.
1567 if (expected_clock_class
) {
1568 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
1571 /* Notifiy listeners of the trace's schema modification. */
1572 bt_stream_class_visit(stream_class
,
1573 bt_trace_object_modification
, trace
);
1574 BT_LOGD("Added stream class to trace: "
1575 "trace-addr=%p, trace-name=\"%s\", "
1576 "stream-class-addr=%p, stream-class-name=\"%s\", "
1577 "stream-class-id=%" PRId64
,
1578 trace
, bt_trace_get_name(trace
),
1579 stream_class
, bt_stream_class_get_name(stream_class
),
1580 bt_stream_class_get_id(stream_class
));
1584 bt_object_set_parent(stream_class
, NULL
);
1586 if (ec_validation_outputs
) {
1587 for (i
= 0; i
< event_class_count
; i
++) {
1588 bt_validation_output_put_types(
1589 &ec_validation_outputs
[i
]);
1594 g_free(ec_validation_outputs
);
1595 bt_validation_output_put_types(&trace_sc_validation_output
);
1596 bt_put(current_parent_trace
);
1597 bt_put(expected_clock_class
);
1598 assert(!packet_header_type
);
1599 assert(!packet_context_type
);
1600 assert(!event_header_type
);
1601 assert(!stream_event_ctx_type
);
1605 int64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
1610 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1615 ret
= (int64_t) trace
->streams
->len
;
1621 struct bt_stream
*bt_trace_get_stream_by_index(
1622 struct bt_trace
*trace
,
1625 struct bt_stream
*stream
= NULL
;
1628 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1632 if (index
>= trace
->streams
->len
) {
1633 BT_LOGW("Invalid parameter: index is out of bounds: "
1634 "addr=%p, name=\"%s\", "
1635 "index=%" PRIu64
", count=%u",
1636 trace
, bt_trace_get_name(trace
),
1637 index
, trace
->streams
->len
);
1641 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1647 int64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
1652 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1657 ret
= (int64_t) trace
->stream_classes
->len
;
1662 struct bt_stream_class
*bt_trace_get_stream_class_by_index(
1663 struct bt_trace
*trace
, uint64_t index
)
1665 struct bt_stream_class
*stream_class
= NULL
;
1668 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1672 if (index
>= trace
->stream_classes
->len
) {
1673 BT_LOGW("Invalid parameter: index is out of bounds: "
1674 "addr=%p, name=\"%s\", "
1675 "index=%" PRIu64
", count=%u",
1676 trace
, bt_trace_get_name(trace
),
1677 index
, trace
->stream_classes
->len
);
1681 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1682 bt_get(stream_class
);
1684 return stream_class
;
1687 struct bt_stream_class
*bt_trace_get_stream_class_by_id(
1688 struct bt_trace
*trace
, uint64_t id_param
)
1691 struct bt_stream_class
*stream_class
= NULL
;
1692 int64_t id
= (int64_t) id_param
;
1695 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1700 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1701 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64
,
1702 trace
, bt_trace_get_name(trace
), id_param
);
1706 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1707 struct bt_stream_class
*stream_class_candidate
;
1709 stream_class_candidate
=
1710 g_ptr_array_index(trace
->stream_classes
, i
);
1712 if (bt_stream_class_get_id(stream_class_candidate
) ==
1714 stream_class
= stream_class_candidate
;
1715 bt_get(stream_class
);
1721 return stream_class
;
1724 struct bt_clock_class
*bt_trace_get_clock_class_by_name(
1725 struct bt_trace
*trace
, const char *name
)
1728 struct bt_clock_class
*clock_class
= NULL
;
1731 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1736 BT_LOGW_STR("Invalid parameter: name is NULL.");
1740 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1741 struct bt_clock_class
*cur_clk
=
1742 g_ptr_array_index(trace
->clocks
, i
);
1743 const char *cur_clk_name
= bt_clock_class_get_name(cur_clk
);
1745 if (!cur_clk_name
) {
1749 if (!strcmp(cur_clk_name
, name
)) {
1750 clock_class
= cur_clk
;
1751 bt_get(clock_class
);
1761 bt_bool
bt_trace_has_clock_class(struct bt_trace
*trace
,
1762 struct bt_clock_class
*clock_class
)
1764 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1767 assert(clock_class
);
1769 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1774 const char *get_byte_order_string(enum bt_byte_order byte_order
)
1778 switch (byte_order
) {
1779 case BT_BYTE_ORDER_LITTLE_ENDIAN
:
1782 case BT_BYTE_ORDER_BIG_ENDIAN
:
1785 case BT_BYTE_ORDER_NATIVE
:
1796 int append_trace_metadata(struct bt_trace
*trace
,
1797 struct metadata_context
*context
)
1799 unsigned char *uuid
= trace
->uuid
;
1802 if (trace
->native_byte_order
== BT_BYTE_ORDER_NATIVE
||
1803 trace
->native_byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
1804 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_BYTE_ORDER_NATIVE or BT_BYTE_ORDER_UNSPECIFIED at this point; "
1805 "set it with bt_trace_set_native_byte_order(): "
1806 "addr=%p, name=\"%s\"",
1807 trace
, bt_trace_get_name(trace
));
1812 g_string_append(context
->string
, "trace {\n");
1813 g_string_append(context
->string
, "\tmajor = 1;\n");
1814 g_string_append(context
->string
, "\tminor = 8;\n");
1815 assert(trace
->native_byte_order
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
1816 trace
->native_byte_order
== BT_BYTE_ORDER_BIG_ENDIAN
||
1817 trace
->native_byte_order
== BT_BYTE_ORDER_NETWORK
);
1819 if (trace
->uuid_set
) {
1820 g_string_append_printf(context
->string
,
1821 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1822 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1823 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1824 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1825 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1828 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1829 get_byte_order_string(trace
->native_byte_order
));
1831 if (trace
->packet_header_type
) {
1832 g_string_append(context
->string
, "\tpacket.header := ");
1833 context
->current_indentation_level
++;
1834 g_string_assign(context
->field_name
, "");
1835 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1836 ret
= bt_field_type_serialize(trace
->packet_header_type
,
1841 context
->current_indentation_level
--;
1844 g_string_append(context
->string
, ";\n};\n\n");
1850 void append_env_metadata(struct bt_trace
*trace
,
1851 struct metadata_context
*context
)
1856 env_size
= bt_attributes_get_count(trace
->environment
);
1857 if (env_size
<= 0) {
1861 g_string_append(context
->string
, "env {\n");
1863 for (i
= 0; i
< env_size
; i
++) {
1864 struct bt_value
*env_field_value_obj
= NULL
;
1865 const char *entry_name
;
1867 entry_name
= bt_attributes_get_field_name(
1868 trace
->environment
, i
);
1869 env_field_value_obj
= bt_attributes_get_field_value(
1870 trace
->environment
, i
);
1873 assert(env_field_value_obj
);
1875 switch (bt_value_get_type(env_field_value_obj
)) {
1876 case BT_VALUE_TYPE_INTEGER
:
1881 ret
= bt_value_integer_get(env_field_value_obj
,
1884 g_string_append_printf(context
->string
,
1885 "\t%s = %" PRId64
";\n", entry_name
,
1889 case BT_VALUE_TYPE_STRING
:
1892 const char *str_value
;
1893 char *escaped_str
= NULL
;
1895 ret
= bt_value_string_get(env_field_value_obj
,
1898 escaped_str
= g_strescape(str_value
, NULL
);
1900 BT_LOGE("Cannot escape string: string=\"%s\"",
1905 g_string_append_printf(context
->string
,
1906 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1915 BT_PUT(env_field_value_obj
);
1918 g_string_append(context
->string
, "};\n\n");
1921 char *bt_trace_get_metadata_string(struct bt_trace
*trace
)
1923 char *metadata
= NULL
;
1924 struct metadata_context
*context
= NULL
;
1929 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1933 context
= g_new0(struct metadata_context
, 1);
1935 BT_LOGE_STR("Failed to allocate one metadata context.");
1939 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1940 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1941 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1942 if (append_trace_metadata(trace
, context
)) {
1943 /* append_trace_metadata() logs errors */
1946 append_env_metadata(trace
, context
);
1947 g_ptr_array_foreach(trace
->clocks
,
1948 (GFunc
)bt_clock_class_serialize
, context
);
1950 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1951 /* bt_stream_class_serialize() logs details */
1952 err
= bt_stream_class_serialize(
1953 trace
->stream_classes
->pdata
[i
], context
);
1955 /* bt_stream_class_serialize() logs errors */
1960 metadata
= context
->string
->str
;
1963 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1964 g_string_free(context
->field_name
, TRUE
);
1971 enum bt_byte_order
bt_trace_get_native_byte_order(
1972 struct bt_trace
*trace
)
1974 enum bt_byte_order ret
= BT_BYTE_ORDER_UNKNOWN
;
1977 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1981 ret
= trace
->native_byte_order
;
1987 int bt_trace_set_native_byte_order(struct bt_trace
*trace
,
1988 enum bt_byte_order byte_order
)
1993 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1998 if (trace
->frozen
) {
1999 BT_LOGW("Invalid parameter: trace is frozen: "
2000 "addr=%p, name=\"%s\"",
2001 trace
, bt_trace_get_name(trace
));
2006 if (trace
->is_created_by_writer
&&
2007 byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
2008 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
2009 "addr=%p, name=\"%s\"",
2010 trace
, bt_trace_get_name(trace
));
2015 if (byte_order
!= BT_BYTE_ORDER_LITTLE_ENDIAN
&&
2016 byte_order
!= BT_BYTE_ORDER_BIG_ENDIAN
&&
2017 byte_order
!= BT_BYTE_ORDER_NETWORK
) {
2018 BT_LOGW("Invalid parameter: invalid byte order: "
2019 "addr=%p, name=\"%s\", bo=%s",
2020 trace
, bt_trace_get_name(trace
),
2021 bt_byte_order_string(byte_order
));
2026 trace
->native_byte_order
= byte_order
;
2027 BT_LOGV("Set trace's native byte order: "
2028 "addr=%p, name=\"%s\", bo=%s",
2029 trace
, bt_trace_get_name(trace
),
2030 bt_byte_order_string(byte_order
));
2036 struct bt_field_type
*bt_trace_get_packet_header_type(
2037 struct bt_trace
*trace
)
2039 struct bt_field_type
*field_type
= NULL
;
2042 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2046 bt_get(trace
->packet_header_type
);
2047 field_type
= trace
->packet_header_type
;
2052 int bt_trace_set_packet_header_type(struct bt_trace
*trace
,
2053 struct bt_field_type
*packet_header_type
)
2058 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2063 if (trace
->frozen
) {
2064 BT_LOGW("Invalid parameter: trace is frozen: "
2065 "addr=%p, name=\"%s\"",
2066 trace
, bt_trace_get_name(trace
));
2071 /* packet_header_type must be a structure. */
2072 if (packet_header_type
&&
2073 !bt_field_type_is_structure(packet_header_type
)) {
2074 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
2075 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
2076 trace
, bt_trace_get_name(trace
),
2078 bt_field_type_id_string(packet_header_type
->id
));
2083 bt_put(trace
->packet_header_type
);
2084 trace
->packet_header_type
= bt_get(packet_header_type
);
2085 BT_LOGV("Set trace's packet header field type: "
2086 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
2087 trace
, bt_trace_get_name(trace
), packet_header_type
);
2093 int64_t get_stream_class_count(void *element
)
2095 return bt_trace_get_stream_class_count(
2096 (struct bt_trace
*) element
);
2100 void *get_stream_class(void *element
, int i
)
2102 return bt_trace_get_stream_class_by_index(
2103 (struct bt_trace
*) element
, i
);
2107 int visit_stream_class(void *object
, bt_visitor visitor
,void *data
)
2109 return bt_stream_class_visit(object
, visitor
, data
);
2112 int bt_trace_visit(struct bt_trace
*trace
,
2113 bt_visitor visitor
, void *data
)
2116 struct bt_visitor_object obj
= {
2118 .type
= BT_VISITOR_OBJECT_TYPE_TRACE
2122 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2128 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
2133 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
2134 trace
, bt_trace_get_name(trace
));
2135 ret
= visitor_helper(&obj
, get_stream_class_count
,
2136 get_stream_class
, visit_stream_class
, visitor
, data
);
2142 int invoke_listener(struct bt_visitor_object
*object
, void *data
)
2144 struct listener_wrapper
*listener_wrapper
= data
;
2146 listener_wrapper
->listener(object
, listener_wrapper
->data
);
2150 // TODO: add logging to this function once we use it internally.
2151 int bt_trace_add_listener(struct bt_trace
*trace
,
2152 bt_listener_cb listener
, void *listener_data
)
2155 struct listener_wrapper
*listener_wrapper
=
2156 g_new0(struct listener_wrapper
, 1);
2158 if (!trace
|| !listener
|| !listener_wrapper
) {
2163 listener_wrapper
->listener
= listener
;
2164 listener_wrapper
->data
= listener_data
;
2166 /* Visit the current schema. */
2167 ret
= bt_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2173 * Add listener to the array of callbacks which will be invoked on
2176 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2179 g_free(listener_wrapper
);
2184 int bt_trace_object_modification(struct bt_visitor_object
*object
,
2188 struct bt_trace
*trace
= trace_ptr
;
2193 if (trace
->listeners
->len
== 0) {
2197 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2198 struct listener_wrapper
*listener
=
2199 g_ptr_array_index(trace
->listeners
, i
);
2201 listener
->listener(object
, listener
->data
);
2208 struct bt_field_type
*get_field_type(enum field_type_alias alias
)
2211 unsigned int alignment
, size
;
2212 struct bt_field_type
*field_type
= NULL
;
2214 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2218 alignment
= field_type_aliases_alignments
[alias
];
2219 size
= field_type_aliases_sizes
[alias
];
2220 field_type
= bt_field_type_integer_create(size
);
2221 ret
= bt_field_type_set_alignment(field_type
, alignment
);
2230 void bt_trace_freeze(struct bt_trace
*trace
)
2234 if (trace
->frozen
) {
2238 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2239 trace
, bt_trace_get_name(trace
));
2240 BT_LOGD_STR("Freezing packet header field type.");
2241 bt_field_type_freeze(trace
->packet_header_type
);
2242 BT_LOGD_STR("Freezing environment attributes.");
2243 bt_attributes_freeze(trace
->environment
);
2245 if (trace
->clocks
->len
> 0) {
2246 BT_LOGD_STR("Freezing clock classes.");
2249 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2250 struct bt_clock_class
*clock_class
=
2251 g_ptr_array_index(trace
->clocks
, i
);
2253 bt_clock_class_freeze(clock_class
);
2259 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
2261 bt_bool is_static
= BT_FALSE
;
2264 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2268 is_static
= trace
->is_static
;
2274 int bt_trace_set_is_static(struct bt_trace
*trace
)
2280 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2285 ret
= check_packet_header_type_has_no_clock_class(trace
);
2287 /* check_packet_header_type_has_no_clock_class() logs errors */
2291 trace
->is_static
= BT_TRUE
;
2292 bt_trace_freeze(trace
);
2293 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2294 trace
, bt_trace_get_name(trace
));
2296 /* Call all the "trace is static" listeners */
2297 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2298 struct bt_trace_is_static_listener_elem elem
=
2299 g_array_index(trace
->is_static_listeners
,
2300 struct bt_trace_is_static_listener_elem
, i
);
2303 elem
.func(trace
, elem
.data
);
2311 int bt_trace_add_is_static_listener(struct bt_trace
*trace
,
2312 bt_trace_is_static_listener listener
,
2313 bt_trace_listener_removed listener_removed
, void *data
)
2316 struct bt_trace_is_static_listener_elem new_elem
= {
2318 .removed
= listener_removed
,
2323 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2329 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2334 if (trace
->is_static
) {
2335 BT_LOGW("Invalid parameter: trace is already static: "
2336 "addr=%p, name=\"%s\"",
2337 trace
, bt_trace_get_name(trace
));
2342 if (trace
->in_remove_listener
) {
2343 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2344 "addr=%p, name=\"%s\"",
2345 trace
, bt_trace_get_name(trace
));
2350 /* Find the next available spot */
2351 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2352 struct bt_trace_is_static_listener_elem elem
=
2353 g_array_index(trace
->is_static_listeners
,
2354 struct bt_trace_is_static_listener_elem
, i
);
2361 if (i
== trace
->is_static_listeners
->len
) {
2362 g_array_append_val(trace
->is_static_listeners
, new_elem
);
2364 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
2367 BT_LOGV("Added \"trace is static\" listener: "
2368 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2369 "data-addr=%p, listener-id=%d",
2370 trace
, bt_trace_get_name(trace
), listener
, data
, i
);
2376 int bt_trace_remove_is_static_listener(
2377 struct bt_trace
*trace
, int listener_id
)
2380 struct bt_trace_is_static_listener_elem
*elem
;
2383 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2388 if (trace
->in_remove_listener
) {
2389 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2390 "addr=%p, name=\"%s\", listener-id=%d",
2391 trace
, bt_trace_get_name(trace
),
2397 if (listener_id
< 0) {
2398 BT_LOGW("Invalid listener ID: must be zero or positive: "
2399 "listener-id=%d", listener_id
);
2404 if (listener_id
>= trace
->is_static_listeners
->len
) {
2405 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2406 "addr=%p, name=\"%s\", listener-id=%d",
2407 trace
, bt_trace_get_name(trace
),
2413 elem
= &g_array_index(trace
->is_static_listeners
,
2414 struct bt_trace_is_static_listener_elem
,
2417 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2418 "addr=%p, name=\"%s\", listener-id=%d",
2419 trace
, bt_trace_get_name(trace
),
2425 if (elem
->removed
) {
2426 /* Call remove listener */
2427 BT_LOGV("Calling remove listener: "
2428 "trace-addr=%p, trace-name=\"%s\", "
2429 "listener-id=%d", trace
, bt_trace_get_name(trace
),
2431 trace
->in_remove_listener
= BT_TRUE
;
2432 elem
->removed(trace
, elem
->data
);
2433 trace
->in_remove_listener
= BT_FALSE
;
2437 elem
->removed
= NULL
;
2439 BT_LOGV("Removed \"trace is static\" listener: "
2440 "trace-addr=%p, trace-name=\"%s\", "
2441 "listener-id=%d", trace
, bt_trace_get_name(trace
),