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>
53 #include <babeltrace/assert-internal.h>
59 #define DEFAULT_IDENTIFIER_SIZE 128
60 #define DEFAULT_METADATA_STRING_SIZE 4096
62 struct listener_wrapper
{
63 bt_listener_cb listener
;
67 struct bt_trace_is_static_listener_elem
{
68 bt_trace_is_static_listener func
;
69 bt_trace_listener_removed removed
;
74 void bt_trace_destroy(struct bt_object
*obj
);
76 void bt_trace_freeze(struct bt_trace
*trace
);
79 const unsigned int field_type_aliases_alignments
[] = {
80 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
81 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
82 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
83 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
87 const unsigned int field_type_aliases_sizes
[] = {
88 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
89 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
90 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
91 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
92 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
93 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
96 struct bt_trace
*bt_trace_create(void)
98 struct bt_trace
*trace
= NULL
;
100 trace
= g_new0(struct bt_trace
, 1);
102 BT_LOGE_STR("Failed to allocate one trace.");
106 BT_LOGD_STR("Creating trace object.");
107 trace
->native_byte_order
= BT_BYTE_ORDER_UNSPECIFIED
;
108 bt_object_init(trace
, bt_trace_destroy
);
109 trace
->clocks
= g_ptr_array_new_with_free_func(
110 (GDestroyNotify
) bt_put
);
111 if (!trace
->clocks
) {
112 BT_LOGE_STR("Failed to allocate one GPtrArray.");
116 trace
->streams
= g_ptr_array_new_with_free_func(
117 (GDestroyNotify
) bt_object_release
);
118 if (!trace
->streams
) {
119 BT_LOGE_STR("Failed to allocate one GPtrArray.");
123 trace
->stream_classes
= g_ptr_array_new_with_free_func(
124 (GDestroyNotify
) bt_object_release
);
125 if (!trace
->stream_classes
) {
126 BT_LOGE_STR("Failed to allocate one GPtrArray.");
130 /* Create the environment array object */
131 trace
->environment
= bt_attributes_create();
132 if (!trace
->environment
) {
133 BT_LOGE_STR("Cannot create empty attributes object.");
137 trace
->listeners
= g_ptr_array_new_with_free_func(
138 (GDestroyNotify
) g_free
);
139 if (!trace
->listeners
) {
140 BT_LOGE_STR("Failed to allocate one GPtrArray.");
144 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
145 sizeof(struct bt_trace_is_static_listener_elem
));
146 if (!trace
->is_static_listeners
) {
147 BT_LOGE_STR("Failed to allocate one GArray.");
151 BT_LOGD("Created trace object: addr=%p", trace
);
159 const char *bt_trace_get_name(struct bt_trace
*trace
)
161 const char *name
= NULL
;
164 BT_LOGW_STR("Invalid parameter: trace is NULL.");
172 name
= trace
->name
->str
;
177 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
182 BT_LOGW_STR("Invalid parameter: trace is NULL.");
188 BT_LOGW_STR("Invalid parameter: name is NULL.");
194 BT_LOGW("Invalid parameter: trace is frozen: "
195 "addr=%p, name=\"%s\"",
196 trace
, bt_trace_get_name(trace
));
201 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
204 BT_LOGE_STR("Failed to allocate one GString.");
209 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
215 const unsigned char *bt_trace_get_uuid(struct bt_trace
*trace
)
217 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
220 int bt_trace_set_uuid(struct bt_trace
*trace
, const unsigned char *uuid
)
225 BT_LOGW_STR("Invalid parameter: trace is NULL.");
231 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
237 BT_LOGW("Invalid parameter: trace is frozen: "
238 "addr=%p, name=\"%s\"",
239 trace
, bt_trace_get_name(trace
));
244 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
245 trace
->uuid_set
= BT_TRUE
;
246 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
247 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
248 trace
, bt_trace_get_name(trace
),
249 (unsigned int) uuid
[0],
250 (unsigned int) uuid
[1],
251 (unsigned int) uuid
[2],
252 (unsigned int) uuid
[3],
253 (unsigned int) uuid
[4],
254 (unsigned int) uuid
[5],
255 (unsigned int) uuid
[6],
256 (unsigned int) uuid
[7],
257 (unsigned int) uuid
[8],
258 (unsigned int) uuid
[9],
259 (unsigned int) uuid
[10],
260 (unsigned int) uuid
[11],
261 (unsigned int) uuid
[12],
262 (unsigned int) uuid
[13],
263 (unsigned int) uuid
[14],
264 (unsigned int) uuid
[15]);
270 void bt_trace_destroy(struct bt_object
*obj
)
272 struct bt_trace
*trace
;
274 trace
= container_of(obj
, struct bt_trace
, base
);
276 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
277 trace
, bt_trace_get_name(trace
));
280 * Call remove listeners first so that everything else still
281 * exists in the trace.
283 if (trace
->is_static_listeners
) {
286 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
287 struct bt_trace_is_static_listener_elem elem
=
288 g_array_index(trace
->is_static_listeners
,
289 struct bt_trace_is_static_listener_elem
, i
);
292 elem
.removed(trace
, elem
.data
);
296 g_array_free(trace
->is_static_listeners
, TRUE
);
299 if (trace
->listeners
) {
300 g_ptr_array_free(trace
->listeners
, TRUE
);
303 if (trace
->environment
) {
304 BT_LOGD_STR("Destroying environment attributes.");
305 bt_attributes_destroy(trace
->environment
);
309 g_string_free(trace
->name
, TRUE
);
313 BT_LOGD_STR("Putting clock classes.");
314 g_ptr_array_free(trace
->clocks
, TRUE
);
317 if (trace
->streams
) {
318 BT_LOGD_STR("Destroying streams.");
319 g_ptr_array_free(trace
->streams
, TRUE
);
322 if (trace
->stream_classes
) {
323 BT_LOGD_STR("Destroying stream classes.");
324 g_ptr_array_free(trace
->stream_classes
, TRUE
);
327 BT_LOGD_STR("Putting packet header field type.");
328 bt_put(trace
->packet_header_type
);
332 int bt_trace_set_environment_field(struct bt_trace
*trace
,
333 const char *name
, struct bt_value
*value
)
338 BT_LOGW_STR("Invalid parameter: trace is NULL.");
344 BT_LOGW_STR("Invalid parameter: name is NULL.");
350 BT_LOGW_STR("Invalid parameter: value is NULL.");
355 if (!bt_identifier_is_valid(name
)) {
356 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
357 "trace-addr=%p, trace-name=\"%s\", "
359 trace
, bt_trace_get_name(trace
), name
);
364 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
365 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
366 "trace-addr=%p, trace-name=\"%s\", "
367 "env-name=\"%s\", env-value-type=%s",
368 trace
, bt_trace_get_name(trace
), name
,
369 bt_value_type_string(bt_value_get_type(value
)));
374 if (trace
->is_static
) {
375 BT_LOGW("Invalid parameter: trace is static: "
376 "addr=%p, name=\"%s\"",
377 trace
, bt_trace_get_name(trace
));
384 * New environment fields may be added to a frozen trace,
385 * but existing fields may not be changed.
387 * The object passed is frozen like all other attributes.
389 struct bt_value
*attribute
=
390 bt_attributes_get_field_value_by_name(
391 trace
->environment
, name
);
394 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
395 "trace-addr=%p, trace-name=\"%s\", "
397 trace
, bt_trace_get_name(trace
), name
);
403 bt_value_freeze(value
);
406 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
409 BT_LOGE("Cannot set environment field's value: "
410 "trace-addr=%p, trace-name=\"%s\", "
412 trace
, bt_trace_get_name(trace
), name
);
414 BT_LOGV("Set environment field's value: "
415 "trace-addr=%p, trace-name=\"%s\", "
416 "env-name=\"%s\", value-addr=%p",
417 trace
, bt_trace_get_name(trace
), name
, value
);
424 int bt_trace_set_environment_field_string(struct bt_trace
*trace
,
425 const char *name
, const char *value
)
428 struct bt_value
*env_value_string_obj
= NULL
;
431 BT_LOGW_STR("Invalid parameter: value is NULL.");
436 env_value_string_obj
= bt_value_string_create_init(value
);
437 if (!env_value_string_obj
) {
438 BT_LOGE_STR("Cannot create string value object.");
443 /* bt_trace_set_environment_field() logs errors */
444 ret
= bt_trace_set_environment_field(trace
, name
,
445 env_value_string_obj
);
448 bt_put(env_value_string_obj
);
452 int bt_trace_set_environment_field_integer(struct bt_trace
*trace
,
453 const char *name
, int64_t value
)
456 struct bt_value
*env_value_integer_obj
= NULL
;
458 env_value_integer_obj
= bt_value_integer_create_init(value
);
459 if (!env_value_integer_obj
) {
460 BT_LOGE_STR("Cannot create integer value object.");
465 /* bt_trace_set_environment_field() logs errors */
466 ret
= bt_trace_set_environment_field(trace
, name
,
467 env_value_integer_obj
);
470 bt_put(env_value_integer_obj
);
474 int64_t bt_trace_get_environment_field_count(struct bt_trace
*trace
)
479 BT_LOGW_STR("Invalid parameter: trace is NULL.");
484 ret
= bt_attributes_get_count(trace
->environment
);
492 bt_trace_get_environment_field_name_by_index(struct bt_trace
*trace
,
495 const char *ret
= NULL
;
498 BT_LOGW_STR("Invalid parameter: trace is NULL.");
502 ret
= bt_attributes_get_field_name(trace
->environment
, index
);
508 struct bt_value
*bt_trace_get_environment_field_value_by_index(
509 struct bt_trace
*trace
, uint64_t index
)
511 struct bt_value
*ret
= NULL
;
514 BT_LOGW_STR("Invalid parameter: trace is NULL.");
518 ret
= bt_attributes_get_field_value(trace
->environment
, index
);
524 struct bt_value
*bt_trace_get_environment_field_value_by_name(
525 struct bt_trace
*trace
, const char *name
)
527 struct bt_value
*ret
= NULL
;
530 BT_LOGW_STR("Invalid parameter: trace is NULL.");
535 BT_LOGW_STR("Invalid parameter: name is NULL.");
539 ret
= bt_attributes_get_field_value_by_name(trace
->environment
,
546 int bt_trace_add_clock_class(struct bt_trace
*trace
,
547 struct bt_clock_class
*clock_class
)
552 BT_LOGW_STR("Invalid parameter: trace is NULL.");
557 if (trace
->is_static
) {
558 BT_LOGW("Invalid parameter: trace is static: "
559 "addr=%p, name=\"%s\"",
560 trace
, bt_trace_get_name(trace
));
565 if (!bt_clock_class_is_valid(clock_class
)) {
566 BT_LOGW("Invalid parameter: clock class is invalid: "
567 "trace-addr=%p, trace-name=\"%s\", "
568 "clock-class-addr=%p, clock-class-name=\"%s\"",
569 trace
, bt_trace_get_name(trace
),
570 clock_class
, bt_clock_class_get_name(clock_class
));
575 /* Check for duplicate clock classes */
576 if (bt_trace_has_clock_class(trace
, clock_class
)) {
577 BT_LOGW("Invalid parameter: clock class already exists in trace: "
578 "trace-addr=%p, trace-name=\"%s\", "
579 "clock-class-addr=%p, clock-class-name=\"%s\"",
580 trace
, bt_trace_get_name(trace
),
581 clock_class
, bt_clock_class_get_name(clock_class
));
587 g_ptr_array_add(trace
->clocks
, clock_class
);
590 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
591 bt_clock_class_freeze(clock_class
);
594 BT_LOGV("Added clock class to trace: "
595 "trace-addr=%p, trace-name=\"%s\", "
596 "clock-class-addr=%p, clock-class-name=\"%s\"",
597 trace
, bt_trace_get_name(trace
),
598 clock_class
, bt_clock_class_get_name(clock_class
));
604 int64_t bt_trace_get_clock_class_count(struct bt_trace
*trace
)
606 int64_t ret
= (int64_t) -1;
609 BT_LOGW_STR("Invalid parameter: trace is NULL.");
613 ret
= trace
->clocks
->len
;
618 struct bt_clock_class
*bt_trace_get_clock_class_by_index(
619 struct bt_trace
*trace
, uint64_t index
)
621 struct bt_clock_class
*clock_class
= NULL
;
624 BT_LOGW_STR("Invalid parameter: trace is NULL.");
628 if (index
>= trace
->clocks
->len
) {
629 BT_LOGW("Invalid parameter: index is out of bounds: "
630 "addr=%p, name=\"%s\", "
631 "index=%" PRIu64
", count=%u",
632 trace
, bt_trace_get_name(trace
),
633 index
, trace
->clocks
->len
);
637 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
644 bool packet_header_field_type_is_valid(struct bt_trace
*trace
,
645 struct bt_field_type
*packet_header_type
)
648 bool is_valid
= true;
649 struct bt_field_type
*field_type
= NULL
;
651 if (!packet_header_type
) {
653 * No packet header field type: trace must have only
654 * one stream. At this point the stream class being
655 * added is not part of the trace yet, so we validate
656 * that the trace contains no stream classes yet.
658 if (trace
->stream_classes
->len
>= 1) {
659 BT_LOGW_STR("Invalid packet header field type: "
660 "packet header field type does not exist but there's more than one stream class in the trace.");
664 /* No packet header field type: valid at this point */
668 /* Packet header field type, if it exists, must be a structure */
669 if (!bt_field_type_is_structure(packet_header_type
)) {
670 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
671 "ft-addr=%p, ft-id=%s",
673 bt_field_type_id_string(packet_header_type
->id
));
678 * If there's a `magic` field, it must be a 32-bit unsigned
679 * integer field type. Also it must be the first field of the
680 * packet header field type.
682 field_type
= bt_field_type_structure_get_field_type_by_name(
683 packet_header_type
, "magic");
685 const char *field_name
;
687 if (!bt_field_type_is_integer(field_type
)) {
688 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
689 "magic-ft-addr=%p, magic-ft-id=%s",
691 bt_field_type_id_string(field_type
->id
));
695 if (bt_field_type_integer_is_signed(field_type
)) {
696 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
697 "magic-ft-addr=%p", field_type
);
701 if (bt_field_type_integer_get_size(field_type
) != 32) {
702 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
703 "magic-ft-addr=%p, magic-ft-size=%u",
705 bt_field_type_integer_get_size(field_type
));
709 ret
= bt_field_type_structure_get_field_by_index(
710 packet_header_type
, &field_name
, NULL
, 0);
713 if (strcmp(field_name
, "magic") != 0) {
714 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
715 "magic-ft-addr=%p, first-field-name=\"%s\"",
716 field_type
, field_name
);
724 * If there's a `uuid` field, it must be an array field type of
725 * length 16 with an 8-bit unsigned integer element field type.
727 field_type
= bt_field_type_structure_get_field_type_by_name(
728 packet_header_type
, "uuid");
730 struct bt_field_type
*elem_ft
;
732 if (!bt_field_type_is_array(field_type
)) {
733 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
734 "uuid-ft-addr=%p, uuid-ft-id=%s",
736 bt_field_type_id_string(field_type
->id
));
740 if (bt_field_type_array_get_length(field_type
) != 16) {
741 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
742 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
744 bt_field_type_array_get_length(field_type
));
748 elem_ft
= bt_field_type_array_get_element_type(field_type
);
751 if (!bt_field_type_is_integer(elem_ft
)) {
752 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
753 "elem-ft-addr=%p, elem-ft-id=%s",
755 bt_field_type_id_string(elem_ft
->id
));
760 if (bt_field_type_integer_is_signed(elem_ft
)) {
761 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
762 "elem-ft-addr=%p", elem_ft
);
767 if (bt_field_type_integer_get_size(elem_ft
) != 8) {
768 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
769 "elem-ft-addr=%p, elem-ft-size=%u",
771 bt_field_type_integer_get_size(elem_ft
));
781 * The `stream_id` field must exist if there's more than one
782 * stream classes in the trace.
784 field_type
= bt_field_type_structure_get_field_type_by_name(
785 packet_header_type
, "stream_id");
787 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
788 BT_LOGW_STR("Invalid packet header field type: "
789 "`stream_id` field does not exist but there's more than one stream class in the trace.");
794 * If there's a `stream_id` field, it must be an unsigned
795 * integer field type.
798 if (!bt_field_type_is_integer(field_type
)) {
799 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
800 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
802 bt_field_type_id_string(field_type
->id
));
806 if (bt_field_type_integer_is_signed(field_type
)) {
807 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
808 "stream-id-ft-addr=%p", field_type
);
816 * If there's a `packet_seq_num` field, it must be an unsigned
817 * integer field type.
819 field_type
= bt_field_type_structure_get_field_type_by_name(
820 packet_header_type
, "packet_seq_num");
822 if (!bt_field_type_is_integer(field_type
)) {
823 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
824 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
826 bt_field_type_id_string(field_type
->id
));
830 if (bt_field_type_integer_is_signed(field_type
)) {
831 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
832 "packet-seq-num-ft-addr=%p", field_type
);
850 bool packet_context_field_type_is_valid(struct bt_trace
*trace
,
851 struct bt_stream_class
*stream_class
,
852 struct bt_field_type
*packet_context_type
)
854 bool is_valid
= true;
855 struct bt_field_type
*field_type
= NULL
;
857 if (!packet_context_type
) {
858 /* No packet context field type: valid at this point */
862 /* Packet context field type, if it exists, must be a structure */
863 if (!bt_field_type_is_structure(packet_context_type
)) {
864 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
865 "ft-addr=%p, ft-id=%s",
867 bt_field_type_id_string(packet_context_type
->id
));
872 * If there's a `packet_size` field, it must be an unsigned
873 * integer field type.
875 field_type
= bt_field_type_structure_get_field_type_by_name(
876 packet_context_type
, "packet_size");
878 if (!bt_field_type_is_integer(field_type
)) {
879 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
880 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
882 bt_field_type_id_string(field_type
->id
));
886 if (bt_field_type_integer_is_signed(field_type
)) {
887 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
888 "packet-size-ft-addr=%p", field_type
);
896 * If there's a `content_size` field, it must be an unsigned
897 * integer field type.
899 field_type
= bt_field_type_structure_get_field_type_by_name(
900 packet_context_type
, "content_size");
902 if (!bt_field_type_is_integer(field_type
)) {
903 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
904 "content-size-ft-addr=%p, content-size-ft-id=%s",
906 bt_field_type_id_string(field_type
->id
));
910 if (bt_field_type_integer_is_signed(field_type
)) {
911 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
912 "content-size-ft-addr=%p", field_type
);
920 * If there's a `events_discarded` field, it must be an unsigned
921 * integer field type.
923 field_type
= bt_field_type_structure_get_field_type_by_name(
924 packet_context_type
, "events_discarded");
926 if (!bt_field_type_is_integer(field_type
)) {
927 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
928 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
930 bt_field_type_id_string(field_type
->id
));
934 if (bt_field_type_integer_is_signed(field_type
)) {
935 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
936 "events-discarded-ft-addr=%p", field_type
);
944 * If there's a `timestamp_begin` field, it must be an unsigned
945 * integer field type. Also, if the trace is not a CTF writer's
946 * trace, then we cannot automatically set the mapped clock
947 * class of this field, so it must have a mapped clock class.
949 field_type
= bt_field_type_structure_get_field_type_by_name(
950 packet_context_type
, "timestamp_begin");
952 if (!bt_field_type_is_integer(field_type
)) {
953 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
954 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
956 bt_field_type_id_string(field_type
->id
));
960 if (bt_field_type_integer_is_signed(field_type
)) {
961 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
962 "timestamp-begin-ft-addr=%p", field_type
);
966 if (!trace
->is_created_by_writer
) {
967 struct bt_clock_class
*clock_class
=
968 bt_field_type_integer_get_mapped_clock_class(
973 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
974 "timestamp-begin-ft-addr=%p", field_type
);
983 * If there's a `timestamp_end` field, it must be an unsigned
984 * integer field type. Also, if the trace is not a CTF writer's
985 * trace, then we cannot automatically set the mapped clock
986 * class of this field, so it must have a mapped clock class.
988 field_type
= bt_field_type_structure_get_field_type_by_name(
989 packet_context_type
, "timestamp_end");
991 if (!bt_field_type_is_integer(field_type
)) {
992 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
993 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
995 bt_field_type_id_string(field_type
->id
));
999 if (bt_field_type_integer_is_signed(field_type
)) {
1000 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
1001 "timestamp-end-ft-addr=%p", field_type
);
1005 if (!trace
->is_created_by_writer
) {
1006 struct bt_clock_class
*clock_class
=
1007 bt_field_type_integer_get_mapped_clock_class(
1010 bt_put(clock_class
);
1012 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
1013 "timestamp-end-ft-addr=%p", field_type
);
1032 bool event_header_field_type_is_valid(struct bt_trace
*trace
,
1033 struct bt_stream_class
*stream_class
,
1034 struct bt_field_type
*event_header_type
)
1036 bool is_valid
= true;
1037 struct bt_field_type
*field_type
= NULL
;
1040 * We do not validate that the `timestamp` field exists here
1041 * because CTF does not require this exact name to be mapped to
1045 if (!event_header_type
) {
1047 * No event header field type: stream class must have
1048 * only one event class.
1050 if (bt_stream_class_get_event_class_count(stream_class
) > 1) {
1051 BT_LOGW_STR("Invalid event header field type: "
1052 "event header field type does not exist but there's more than one event class in the stream class.");
1056 /* No event header field type: valid at this point */
1060 /* Event header field type, if it exists, must be a structure */
1061 if (!bt_field_type_is_structure(event_header_type
)) {
1062 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1063 "ft-addr=%p, ft-id=%s",
1065 bt_field_type_id_string(event_header_type
->id
));
1070 * If there's an `id` field, it must be an unsigned integer
1071 * field type or an enumeration field type with an unsigned
1072 * integer container field type.
1074 field_type
= bt_field_type_structure_get_field_type_by_name(
1075 event_header_type
, "id");
1077 struct bt_field_type
*int_ft
;
1079 if (bt_field_type_is_integer(field_type
)) {
1080 int_ft
= bt_get(field_type
);
1081 } else if (bt_field_type_is_enumeration(field_type
)) {
1082 int_ft
= bt_field_type_enumeration_get_container_type(
1085 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1086 "id-ft-addr=%p, id-ft-id=%s",
1088 bt_field_type_id_string(field_type
->id
));
1093 if (bt_field_type_integer_is_signed(int_ft
)) {
1094 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1095 "id-ft-addr=%p", int_ft
);
1114 int check_packet_header_type_has_no_clock_class(struct bt_trace
*trace
)
1118 if (trace
->packet_header_type
) {
1119 struct bt_clock_class
*clock_class
= NULL
;
1121 ret
= bt_validate_single_clock_class(trace
->packet_header_type
,
1123 bt_put(clock_class
);
1124 if (ret
|| clock_class
) {
1125 BT_LOGW("Trace's packet header field type cannot "
1126 "contain a field type which is mapped to "
1128 "trace-addr=%p, trace-name=\"%s\", "
1129 "clock-class-name=\"%s\"",
1130 trace
, bt_trace_get_name(trace
),
1132 bt_clock_class_get_name(clock_class
) :
1141 int bt_trace_add_stream_class(struct bt_trace
*trace
,
1142 struct bt_stream_class
*stream_class
)
1147 struct bt_validation_output trace_sc_validation_output
= { 0 };
1148 struct bt_validation_output
*ec_validation_outputs
= NULL
;
1149 const enum bt_validation_flag trace_sc_validation_flags
=
1150 BT_VALIDATION_FLAG_TRACE
|
1151 BT_VALIDATION_FLAG_STREAM
;
1152 const enum bt_validation_flag ec_validation_flags
=
1153 BT_VALIDATION_FLAG_EVENT
;
1154 struct bt_field_type
*packet_header_type
= NULL
;
1155 struct bt_field_type
*packet_context_type
= NULL
;
1156 struct bt_field_type
*event_header_type
= NULL
;
1157 struct bt_field_type
*stream_event_ctx_type
= NULL
;
1158 int64_t event_class_count
;
1159 struct bt_trace
*current_parent_trace
= NULL
;
1160 struct bt_clock_class
*expected_clock_class
= NULL
;
1163 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1168 if (!stream_class
) {
1169 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1174 if (trace
->is_static
) {
1175 BT_LOGW_STR("Invalid parameter: trace is static.");
1180 BT_LOGD("Adding stream class to trace: "
1181 "trace-addr=%p, trace-name=\"%s\", "
1182 "stream-class-addr=%p, stream-class-name=\"%s\", "
1183 "stream-class-id=%" PRId64
,
1184 trace
, bt_trace_get_name(trace
),
1185 stream_class
, bt_stream_class_get_name(stream_class
),
1186 bt_stream_class_get_id(stream_class
));
1188 current_parent_trace
= bt_stream_class_get_trace(stream_class
);
1189 if (current_parent_trace
) {
1190 /* Stream class is already associated to a trace, abort. */
1191 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1192 "stream-class-trace-addr=%p, "
1193 "stream-class-trace-name=\"%s\"",
1194 current_parent_trace
,
1195 bt_trace_get_name(current_parent_trace
));
1201 bt_stream_class_get_event_class_count(stream_class
);
1202 BT_ASSERT(event_class_count
>= 0);
1204 if (stream_class
->clock
) {
1205 struct bt_clock_class
*stream_clock_class
=
1206 stream_class
->clock
->clock_class
;
1208 if (trace
->is_created_by_writer
) {
1210 * Make sure this clock was also added to the
1211 * trace (potentially through its CTF writer
1216 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1217 if (trace
->clocks
->pdata
[i
] ==
1218 stream_clock_class
) {
1224 if (i
== trace
->clocks
->len
) {
1226 BT_LOGW("Stream class's clock's class is not part of the trace: "
1227 "clock-class-addr=%p, clock-class-name=\"%s\"",
1229 bt_clock_class_get_name(stream_clock_class
));
1235 * This trace was NOT created by a CTF writer,
1236 * thus do not allow the stream class to add to
1237 * have a clock at all. Those are two
1238 * independent APIs (non-writer and writer
1239 * APIs), and isolating them simplifies things.
1241 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1242 "clock-class-addr=%p, clock-class-name=\"%s\"",
1244 bt_clock_class_get_name(stream_clock_class
));
1249 if (stream_class
->clock_class
&&
1250 stream_class
->clock_class
!=
1251 stream_class
->clock
->clock_class
) {
1253 * Stream class already has an expected clock
1254 * class, but it does not match its clock's
1257 BT_LOGW("Invalid parameter: stream class's clock's "
1258 "class does not match stream class's "
1259 "expected clock class: "
1260 "stream-class-addr=%p, "
1261 "stream-class-id=%" PRId64
", "
1262 "stream-class-name=\"%s\", "
1263 "expected-clock-class-addr=%p, "
1264 "expected-clock-class-name=\"%s\"",
1266 bt_stream_class_get_id(stream_class
),
1267 bt_stream_class_get_name(stream_class
),
1268 expected_clock_class
,
1269 bt_clock_class_get_name(expected_clock_class
));
1270 } else if (!stream_class
->clock_class
) {
1272 * Set expected clock class to stream class's
1275 expected_clock_class
=
1276 bt_get(stream_class
->clock
->clock_class
);
1280 if (!stream_class
->frozen
) {
1282 * Stream class is not frozen yet. Validate that the
1283 * stream class contains at most a single clock class
1284 * because the previous
1285 * bt_stream_class_add_event_class() calls did not make
1286 * this validation since the stream class's direct field
1287 * types (packet context, event header, event context)
1288 * could change afterwards. This stream class is about
1289 * to be frozen and those field types won't be changed
1290 * if this function succeeds.
1292 * At this point we're also sure that the stream class's
1293 * clock, if any, has the same class as the stream
1294 * class's expected clock class, if any. This is why, if
1295 * bt_stream_class_validate_single_clock_class()
1296 * succeeds below, the call to
1297 * bt_stream_class_map_clock_class() at the end of this
1298 * function is safe because it maps to the same, single
1301 ret
= bt_stream_class_validate_single_clock_class(stream_class
,
1302 &expected_clock_class
);
1304 BT_LOGW("Invalid parameter: stream class or one of its "
1305 "event classes contains a field type which is "
1306 "not recursively mapped to the expected "
1308 "stream-class-addr=%p, "
1309 "stream-class-id=%" PRId64
", "
1310 "stream-class-name=\"%s\", "
1311 "expected-clock-class-addr=%p, "
1312 "expected-clock-class-name=\"%s\"",
1313 stream_class
, bt_stream_class_get_id(stream_class
),
1314 bt_stream_class_get_name(stream_class
),
1315 expected_clock_class
,
1316 expected_clock_class
?
1317 bt_clock_class_get_name(expected_clock_class
) :
1323 ret
= check_packet_header_type_has_no_clock_class(trace
);
1325 /* check_packet_header_type_has_no_clock_class() logs errors */
1330 * We're about to freeze both the trace and the stream class.
1331 * Also, each event class contained in this stream class are
1334 * This trace, this stream class, and all its event classes
1335 * should be valid at this point.
1337 * Validate trace and stream class first, then each event
1338 * class of this stream class can be validated individually.
1340 packet_header_type
=
1341 bt_trace_get_packet_header_type(trace
);
1342 packet_context_type
=
1343 bt_stream_class_get_packet_context_type(stream_class
);
1345 bt_stream_class_get_event_header_type(stream_class
);
1346 stream_event_ctx_type
=
1347 bt_stream_class_get_event_context_type(stream_class
);
1349 BT_LOGD("Validating trace and stream class field types.");
1350 ret
= bt_validate_class_types(trace
->environment
,
1351 packet_header_type
, packet_context_type
, event_header_type
,
1352 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1353 stream_class
->valid
, 1, &trace_sc_validation_output
,
1354 trace_sc_validation_flags
);
1355 BT_PUT(packet_header_type
);
1356 BT_PUT(packet_context_type
);
1357 BT_PUT(event_header_type
);
1358 BT_PUT(stream_event_ctx_type
);
1362 * This means something went wrong during the validation
1363 * process, not that the objects are invalid.
1365 BT_LOGE("Failed to validate trace and stream class field types: "
1370 if ((trace_sc_validation_output
.valid_flags
&
1371 trace_sc_validation_flags
) !=
1372 trace_sc_validation_flags
) {
1373 /* Invalid trace/stream class */
1374 BT_LOGW("Invalid trace or stream class field types: "
1376 trace_sc_validation_output
.valid_flags
);
1381 if (event_class_count
> 0) {
1382 ec_validation_outputs
= g_new0(struct bt_validation_output
,
1384 if (!ec_validation_outputs
) {
1385 BT_LOGE_STR("Failed to allocate one validation output structure.");
1391 /* Validate each event class individually */
1392 for (i
= 0; i
< event_class_count
; i
++) {
1393 struct bt_event_class
*event_class
=
1394 bt_stream_class_get_event_class_by_index(
1396 struct bt_field_type
*event_context_type
= NULL
;
1397 struct bt_field_type
*event_payload_type
= NULL
;
1399 event_context_type
=
1400 bt_event_class_get_context_type(event_class
);
1401 event_payload_type
=
1402 bt_event_class_get_payload_type(event_class
);
1405 * It is important to use the field types returned by
1406 * the previous trace and stream class validation here
1407 * because copies could have been made.
1409 BT_LOGD("Validating event class's field types: "
1410 "addr=%p, name=\"%s\", id=%" PRId64
,
1411 event_class
, bt_event_class_get_name(event_class
),
1412 bt_event_class_get_id(event_class
));
1413 ret
= bt_validate_class_types(trace
->environment
,
1414 trace_sc_validation_output
.packet_header_type
,
1415 trace_sc_validation_output
.packet_context_type
,
1416 trace_sc_validation_output
.event_header_type
,
1417 trace_sc_validation_output
.stream_event_ctx_type
,
1418 event_context_type
, event_payload_type
,
1419 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1420 ec_validation_flags
);
1421 BT_PUT(event_context_type
);
1422 BT_PUT(event_payload_type
);
1423 BT_PUT(event_class
);
1426 BT_LOGE("Failed to validate event class field types: "
1431 if ((ec_validation_outputs
[i
].valid_flags
&
1432 ec_validation_flags
) != ec_validation_flags
) {
1433 /* Invalid event class */
1434 BT_LOGW("Invalid event class field types: "
1436 ec_validation_outputs
[i
].valid_flags
);
1442 stream_id
= bt_stream_class_get_id(stream_class
);
1443 if (stream_id
< 0) {
1444 stream_id
= trace
->next_stream_id
++;
1445 if (stream_id
< 0) {
1446 BT_LOGE_STR("No more stream class IDs available.");
1451 /* Try to assign a new stream id */
1452 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1453 if (stream_id
== bt_stream_class_get_id(
1454 trace
->stream_classes
->pdata
[i
])) {
1455 /* Duplicate stream id found */
1456 BT_LOGW("Duplicate stream class ID: "
1457 "id=%" PRId64
, (int64_t) stream_id
);
1463 if (bt_stream_class_set_id_no_check(stream_class
,
1465 /* TODO Should retry with a different stream id */
1466 BT_LOGE("Cannot set stream class's ID: "
1467 "id=%" PRId64
, (int64_t) stream_id
);
1474 * At this point all the field types in the validation output
1475 * are valid. Validate the semantics of some scopes according to
1476 * the CTF specification.
1478 if (!packet_header_field_type_is_valid(trace
,
1479 trace_sc_validation_output
.packet_header_type
)) {
1480 BT_LOGW_STR("Invalid trace's packet header field type.");
1485 if (!packet_context_field_type_is_valid(trace
,
1487 trace_sc_validation_output
.packet_context_type
)) {
1488 BT_LOGW_STR("Invalid stream class's packet context field type.");
1493 if (!event_header_field_type_is_valid(trace
,
1495 trace_sc_validation_output
.event_header_type
)) {
1496 BT_LOGW_STR("Invalid steam class's event header field type.");
1502 * Now is the time to automatically map specific field types of
1503 * the stream class's packet context and event header field
1504 * types to the stream class's clock's class if they are not
1505 * mapped to a clock class yet. We do it here because we know
1506 * that after this point, everything is frozen so it won't be
1507 * possible for the user to modify the stream class's clock, or
1508 * to map those field types to other clock classes.
1510 if (trace
->is_created_by_writer
) {
1511 if (bt_stream_class_map_clock_class(stream_class
,
1512 trace_sc_validation_output
.packet_context_type
,
1513 trace_sc_validation_output
.event_header_type
)) {
1514 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1520 bt_object_set_parent(stream_class
, trace
);
1521 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1524 * At this point we know that the function will be successful.
1525 * Therefore we can replace the trace and stream class field
1526 * types with what's in their validation output structure and
1527 * mark them as valid. We can also replace the field types of
1528 * all the event classes of the stream class and mark them as
1531 bt_validation_replace_types(trace
, stream_class
, NULL
,
1532 &trace_sc_validation_output
, trace_sc_validation_flags
);
1534 stream_class
->valid
= 1;
1537 * Put what was not moved in bt_validation_replace_types().
1539 bt_validation_output_put_types(&trace_sc_validation_output
);
1541 for (i
= 0; i
< event_class_count
; i
++) {
1542 struct bt_event_class
*event_class
=
1543 bt_stream_class_get_event_class_by_index(
1546 bt_validation_replace_types(NULL
, NULL
, event_class
,
1547 &ec_validation_outputs
[i
], ec_validation_flags
);
1548 event_class
->valid
= 1;
1549 BT_PUT(event_class
);
1552 * Put what was not moved in
1553 * bt_validation_replace_types().
1555 bt_validation_output_put_types(&ec_validation_outputs
[i
]);
1559 * Freeze the trace and the stream class.
1561 bt_stream_class_freeze(stream_class
);
1562 bt_trace_freeze(trace
);
1565 * It is safe to set the stream class's unique clock class
1566 * now because the stream class is frozen.
1568 if (expected_clock_class
) {
1569 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
1572 /* Notifiy listeners of the trace's schema modification. */
1573 bt_stream_class_visit(stream_class
,
1574 bt_trace_object_modification
, trace
);
1575 BT_LOGD("Added stream class to trace: "
1576 "trace-addr=%p, trace-name=\"%s\", "
1577 "stream-class-addr=%p, stream-class-name=\"%s\", "
1578 "stream-class-id=%" PRId64
,
1579 trace
, bt_trace_get_name(trace
),
1580 stream_class
, bt_stream_class_get_name(stream_class
),
1581 bt_stream_class_get_id(stream_class
));
1585 bt_object_set_parent(stream_class
, NULL
);
1587 if (ec_validation_outputs
) {
1588 for (i
= 0; i
< event_class_count
; i
++) {
1589 bt_validation_output_put_types(
1590 &ec_validation_outputs
[i
]);
1595 g_free(ec_validation_outputs
);
1596 bt_validation_output_put_types(&trace_sc_validation_output
);
1597 bt_put(current_parent_trace
);
1598 bt_put(expected_clock_class
);
1599 BT_ASSERT(!packet_header_type
);
1600 BT_ASSERT(!packet_context_type
);
1601 BT_ASSERT(!event_header_type
);
1602 BT_ASSERT(!stream_event_ctx_type
);
1606 int64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
1611 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1616 ret
= (int64_t) trace
->streams
->len
;
1622 struct bt_stream
*bt_trace_get_stream_by_index(
1623 struct bt_trace
*trace
,
1626 struct bt_stream
*stream
= NULL
;
1629 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1633 if (index
>= trace
->streams
->len
) {
1634 BT_LOGW("Invalid parameter: index is out of bounds: "
1635 "addr=%p, name=\"%s\", "
1636 "index=%" PRIu64
", count=%u",
1637 trace
, bt_trace_get_name(trace
),
1638 index
, trace
->streams
->len
);
1642 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1648 int64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
1653 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1658 ret
= (int64_t) trace
->stream_classes
->len
;
1663 struct bt_stream_class
*bt_trace_get_stream_class_by_index(
1664 struct bt_trace
*trace
, uint64_t index
)
1666 struct bt_stream_class
*stream_class
= NULL
;
1669 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1673 if (index
>= trace
->stream_classes
->len
) {
1674 BT_LOGW("Invalid parameter: index is out of bounds: "
1675 "addr=%p, name=\"%s\", "
1676 "index=%" PRIu64
", count=%u",
1677 trace
, bt_trace_get_name(trace
),
1678 index
, trace
->stream_classes
->len
);
1682 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1683 bt_get(stream_class
);
1685 return stream_class
;
1688 struct bt_stream_class
*bt_trace_get_stream_class_by_id(
1689 struct bt_trace
*trace
, uint64_t id_param
)
1692 struct bt_stream_class
*stream_class
= NULL
;
1693 int64_t id
= (int64_t) id_param
;
1696 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1701 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1702 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64
,
1703 trace
, bt_trace_get_name(trace
), id_param
);
1707 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1708 struct bt_stream_class
*stream_class_candidate
;
1710 stream_class_candidate
=
1711 g_ptr_array_index(trace
->stream_classes
, i
);
1713 if (bt_stream_class_get_id(stream_class_candidate
) ==
1715 stream_class
= stream_class_candidate
;
1716 bt_get(stream_class
);
1722 return stream_class
;
1725 struct bt_clock_class
*bt_trace_get_clock_class_by_name(
1726 struct bt_trace
*trace
, const char *name
)
1729 struct bt_clock_class
*clock_class
= NULL
;
1732 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1737 BT_LOGW_STR("Invalid parameter: name is NULL.");
1741 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1742 struct bt_clock_class
*cur_clk
=
1743 g_ptr_array_index(trace
->clocks
, i
);
1744 const char *cur_clk_name
= bt_clock_class_get_name(cur_clk
);
1746 if (!cur_clk_name
) {
1750 if (!strcmp(cur_clk_name
, name
)) {
1751 clock_class
= cur_clk
;
1752 bt_get(clock_class
);
1762 bt_bool
bt_trace_has_clock_class(struct bt_trace
*trace
,
1763 struct bt_clock_class
*clock_class
)
1765 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1768 BT_ASSERT(clock_class
);
1770 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1775 const char *get_byte_order_string(enum bt_byte_order byte_order
)
1779 switch (byte_order
) {
1780 case BT_BYTE_ORDER_LITTLE_ENDIAN
:
1783 case BT_BYTE_ORDER_BIG_ENDIAN
:
1786 case BT_BYTE_ORDER_NATIVE
:
1797 int append_trace_metadata(struct bt_trace
*trace
,
1798 struct metadata_context
*context
)
1800 unsigned char *uuid
= trace
->uuid
;
1803 if (trace
->native_byte_order
== BT_BYTE_ORDER_NATIVE
||
1804 trace
->native_byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
1805 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_BYTE_ORDER_NATIVE or BT_BYTE_ORDER_UNSPECIFIED at this point; "
1806 "set it with bt_trace_set_native_byte_order(): "
1807 "addr=%p, name=\"%s\"",
1808 trace
, bt_trace_get_name(trace
));
1813 g_string_append(context
->string
, "trace {\n");
1814 g_string_append(context
->string
, "\tmajor = 1;\n");
1815 g_string_append(context
->string
, "\tminor = 8;\n");
1816 BT_ASSERT(trace
->native_byte_order
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
1817 trace
->native_byte_order
== BT_BYTE_ORDER_BIG_ENDIAN
||
1818 trace
->native_byte_order
== BT_BYTE_ORDER_NETWORK
);
1820 if (trace
->uuid_set
) {
1821 g_string_append_printf(context
->string
,
1822 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1823 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1824 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1825 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1826 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1829 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1830 get_byte_order_string(trace
->native_byte_order
));
1832 if (trace
->packet_header_type
) {
1833 g_string_append(context
->string
, "\tpacket.header := ");
1834 context
->current_indentation_level
++;
1835 g_string_assign(context
->field_name
, "");
1836 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1837 ret
= bt_field_type_serialize(trace
->packet_header_type
,
1842 context
->current_indentation_level
--;
1845 g_string_append(context
->string
, ";\n};\n\n");
1851 void append_env_metadata(struct bt_trace
*trace
,
1852 struct metadata_context
*context
)
1857 env_size
= bt_attributes_get_count(trace
->environment
);
1858 if (env_size
<= 0) {
1862 g_string_append(context
->string
, "env {\n");
1864 for (i
= 0; i
< env_size
; i
++) {
1865 struct bt_value
*env_field_value_obj
= NULL
;
1866 const char *entry_name
;
1868 entry_name
= bt_attributes_get_field_name(
1869 trace
->environment
, i
);
1870 env_field_value_obj
= bt_attributes_get_field_value(
1871 trace
->environment
, i
);
1873 BT_ASSERT(entry_name
);
1874 BT_ASSERT(env_field_value_obj
);
1876 switch (bt_value_get_type(env_field_value_obj
)) {
1877 case BT_VALUE_TYPE_INTEGER
:
1882 ret
= bt_value_integer_get(env_field_value_obj
,
1884 BT_ASSERT(ret
== 0);
1885 g_string_append_printf(context
->string
,
1886 "\t%s = %" PRId64
";\n", entry_name
,
1890 case BT_VALUE_TYPE_STRING
:
1893 const char *str_value
;
1894 char *escaped_str
= NULL
;
1896 ret
= bt_value_string_get(env_field_value_obj
,
1898 BT_ASSERT(ret
== 0);
1899 escaped_str
= g_strescape(str_value
, NULL
);
1901 BT_LOGE("Cannot escape string: string=\"%s\"",
1906 g_string_append_printf(context
->string
,
1907 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1916 BT_PUT(env_field_value_obj
);
1919 g_string_append(context
->string
, "};\n\n");
1922 char *bt_trace_get_metadata_string(struct bt_trace
*trace
)
1924 char *metadata
= NULL
;
1925 struct metadata_context
*context
= NULL
;
1930 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1934 context
= g_new0(struct metadata_context
, 1);
1936 BT_LOGE_STR("Failed to allocate one metadata context.");
1940 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1941 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1942 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1943 if (append_trace_metadata(trace
, context
)) {
1944 /* append_trace_metadata() logs errors */
1947 append_env_metadata(trace
, context
);
1948 g_ptr_array_foreach(trace
->clocks
,
1949 (GFunc
)bt_clock_class_serialize
, context
);
1951 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1952 /* bt_stream_class_serialize() logs details */
1953 err
= bt_stream_class_serialize(
1954 trace
->stream_classes
->pdata
[i
], context
);
1956 /* bt_stream_class_serialize() logs errors */
1961 metadata
= context
->string
->str
;
1964 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1965 g_string_free(context
->field_name
, TRUE
);
1972 enum bt_byte_order
bt_trace_get_native_byte_order(
1973 struct bt_trace
*trace
)
1975 enum bt_byte_order ret
= BT_BYTE_ORDER_UNKNOWN
;
1978 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1982 ret
= trace
->native_byte_order
;
1988 int bt_trace_set_native_byte_order(struct bt_trace
*trace
,
1989 enum bt_byte_order byte_order
)
1994 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1999 if (trace
->frozen
) {
2000 BT_LOGW("Invalid parameter: trace is frozen: "
2001 "addr=%p, name=\"%s\"",
2002 trace
, bt_trace_get_name(trace
));
2007 if (trace
->is_created_by_writer
&&
2008 byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
2009 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
2010 "addr=%p, name=\"%s\"",
2011 trace
, bt_trace_get_name(trace
));
2016 if (byte_order
!= BT_BYTE_ORDER_LITTLE_ENDIAN
&&
2017 byte_order
!= BT_BYTE_ORDER_BIG_ENDIAN
&&
2018 byte_order
!= BT_BYTE_ORDER_NETWORK
) {
2019 BT_LOGW("Invalid parameter: invalid byte order: "
2020 "addr=%p, name=\"%s\", bo=%s",
2021 trace
, bt_trace_get_name(trace
),
2022 bt_byte_order_string(byte_order
));
2027 trace
->native_byte_order
= byte_order
;
2028 BT_LOGV("Set trace's native byte order: "
2029 "addr=%p, name=\"%s\", bo=%s",
2030 trace
, bt_trace_get_name(trace
),
2031 bt_byte_order_string(byte_order
));
2037 struct bt_field_type
*bt_trace_get_packet_header_type(
2038 struct bt_trace
*trace
)
2040 struct bt_field_type
*field_type
= NULL
;
2043 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2047 bt_get(trace
->packet_header_type
);
2048 field_type
= trace
->packet_header_type
;
2053 int bt_trace_set_packet_header_type(struct bt_trace
*trace
,
2054 struct bt_field_type
*packet_header_type
)
2059 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2064 if (trace
->frozen
) {
2065 BT_LOGW("Invalid parameter: trace is frozen: "
2066 "addr=%p, name=\"%s\"",
2067 trace
, bt_trace_get_name(trace
));
2072 /* packet_header_type must be a structure. */
2073 if (packet_header_type
&&
2074 !bt_field_type_is_structure(packet_header_type
)) {
2075 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
2076 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
2077 trace
, bt_trace_get_name(trace
),
2079 bt_field_type_id_string(packet_header_type
->id
));
2084 bt_put(trace
->packet_header_type
);
2085 trace
->packet_header_type
= bt_get(packet_header_type
);
2086 BT_LOGV("Set trace's packet header field type: "
2087 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
2088 trace
, bt_trace_get_name(trace
), packet_header_type
);
2094 int64_t get_stream_class_count(void *element
)
2096 return bt_trace_get_stream_class_count(
2097 (struct bt_trace
*) element
);
2101 void *get_stream_class(void *element
, int i
)
2103 return bt_trace_get_stream_class_by_index(
2104 (struct bt_trace
*) element
, i
);
2108 int visit_stream_class(void *object
, bt_visitor visitor
,void *data
)
2110 return bt_stream_class_visit(object
, visitor
, data
);
2113 int bt_trace_visit(struct bt_trace
*trace
,
2114 bt_visitor visitor
, void *data
)
2117 struct bt_visitor_object obj
= {
2119 .type
= BT_VISITOR_OBJECT_TYPE_TRACE
2123 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2129 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
2134 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
2135 trace
, bt_trace_get_name(trace
));
2136 ret
= visitor_helper(&obj
, get_stream_class_count
,
2137 get_stream_class
, visit_stream_class
, visitor
, data
);
2143 int invoke_listener(struct bt_visitor_object
*object
, void *data
)
2145 struct listener_wrapper
*listener_wrapper
= data
;
2147 listener_wrapper
->listener(object
, listener_wrapper
->data
);
2151 // TODO: add logging to this function once we use it internally.
2152 int bt_trace_add_listener(struct bt_trace
*trace
,
2153 bt_listener_cb listener
, void *listener_data
)
2156 struct listener_wrapper
*listener_wrapper
=
2157 g_new0(struct listener_wrapper
, 1);
2159 if (!trace
|| !listener
|| !listener_wrapper
) {
2164 listener_wrapper
->listener
= listener
;
2165 listener_wrapper
->data
= listener_data
;
2167 /* Visit the current schema. */
2168 ret
= bt_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2174 * Add listener to the array of callbacks which will be invoked on
2177 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2180 g_free(listener_wrapper
);
2185 int bt_trace_object_modification(struct bt_visitor_object
*object
,
2189 struct bt_trace
*trace
= trace_ptr
;
2194 if (trace
->listeners
->len
== 0) {
2198 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2199 struct listener_wrapper
*listener
=
2200 g_ptr_array_index(trace
->listeners
, i
);
2202 listener
->listener(object
, listener
->data
);
2209 struct bt_field_type
*get_field_type(enum field_type_alias alias
)
2212 unsigned int alignment
, size
;
2213 struct bt_field_type
*field_type
= NULL
;
2215 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2219 alignment
= field_type_aliases_alignments
[alias
];
2220 size
= field_type_aliases_sizes
[alias
];
2221 field_type
= bt_field_type_integer_create(size
);
2222 ret
= bt_field_type_set_alignment(field_type
, alignment
);
2231 void bt_trace_freeze(struct bt_trace
*trace
)
2235 if (trace
->frozen
) {
2239 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2240 trace
, bt_trace_get_name(trace
));
2241 BT_LOGD_STR("Freezing packet header field type.");
2242 bt_field_type_freeze(trace
->packet_header_type
);
2243 BT_LOGD_STR("Freezing environment attributes.");
2244 bt_attributes_freeze(trace
->environment
);
2246 if (trace
->clocks
->len
> 0) {
2247 BT_LOGD_STR("Freezing clock classes.");
2250 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2251 struct bt_clock_class
*clock_class
=
2252 g_ptr_array_index(trace
->clocks
, i
);
2254 bt_clock_class_freeze(clock_class
);
2260 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
2262 bt_bool is_static
= BT_FALSE
;
2265 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2269 is_static
= trace
->is_static
;
2275 int bt_trace_set_is_static(struct bt_trace
*trace
)
2281 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2286 ret
= check_packet_header_type_has_no_clock_class(trace
);
2288 /* check_packet_header_type_has_no_clock_class() logs errors */
2292 trace
->is_static
= BT_TRUE
;
2293 bt_trace_freeze(trace
);
2294 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2295 trace
, bt_trace_get_name(trace
));
2297 /* Call all the "trace is static" listeners */
2298 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2299 struct bt_trace_is_static_listener_elem elem
=
2300 g_array_index(trace
->is_static_listeners
,
2301 struct bt_trace_is_static_listener_elem
, i
);
2304 elem
.func(trace
, elem
.data
);
2312 int bt_trace_add_is_static_listener(struct bt_trace
*trace
,
2313 bt_trace_is_static_listener listener
,
2314 bt_trace_listener_removed listener_removed
, void *data
)
2317 struct bt_trace_is_static_listener_elem new_elem
= {
2319 .removed
= listener_removed
,
2324 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2330 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2335 if (trace
->is_static
) {
2336 BT_LOGW("Invalid parameter: trace is already static: "
2337 "addr=%p, name=\"%s\"",
2338 trace
, bt_trace_get_name(trace
));
2343 if (trace
->in_remove_listener
) {
2344 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2345 "addr=%p, name=\"%s\"",
2346 trace
, bt_trace_get_name(trace
));
2351 /* Find the next available spot */
2352 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2353 struct bt_trace_is_static_listener_elem elem
=
2354 g_array_index(trace
->is_static_listeners
,
2355 struct bt_trace_is_static_listener_elem
, i
);
2362 if (i
== trace
->is_static_listeners
->len
) {
2363 g_array_append_val(trace
->is_static_listeners
, new_elem
);
2365 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
2368 BT_LOGV("Added \"trace is static\" listener: "
2369 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2370 "data-addr=%p, listener-id=%d",
2371 trace
, bt_trace_get_name(trace
), listener
, data
, i
);
2377 int bt_trace_remove_is_static_listener(
2378 struct bt_trace
*trace
, int listener_id
)
2381 struct bt_trace_is_static_listener_elem
*elem
;
2384 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2389 if (trace
->in_remove_listener
) {
2390 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2391 "addr=%p, name=\"%s\", listener-id=%d",
2392 trace
, bt_trace_get_name(trace
),
2398 if (listener_id
< 0) {
2399 BT_LOGW("Invalid listener ID: must be zero or positive: "
2400 "listener-id=%d", listener_id
);
2405 if (listener_id
>= trace
->is_static_listeners
->len
) {
2406 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2407 "addr=%p, name=\"%s\", listener-id=%d",
2408 trace
, bt_trace_get_name(trace
),
2414 elem
= &g_array_index(trace
->is_static_listeners
,
2415 struct bt_trace_is_static_listener_elem
,
2418 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2419 "addr=%p, name=\"%s\", listener-id=%d",
2420 trace
, bt_trace_get_name(trace
),
2426 if (elem
->removed
) {
2427 /* Call remove listener */
2428 BT_LOGV("Calling remove listener: "
2429 "trace-addr=%p, trace-name=\"%s\", "
2430 "listener-id=%d", trace
, bt_trace_get_name(trace
),
2432 trace
->in_remove_listener
= BT_TRUE
;
2433 elem
->removed(trace
, elem
->data
);
2434 trace
->in_remove_listener
= BT_FALSE
;
2438 elem
->removed
= NULL
;
2440 BT_LOGV("Removed \"trace is static\" listener: "
2441 "trace-addr=%p, trace-name=\"%s\", "
2442 "listener-id=%d", trace
, bt_trace_get_name(trace
),