4 * Babeltrace CTF IR - Trace
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "TRACE"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-ir/trace-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/stream-internal.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/event-class.h>
38 #include <babeltrace/ctf-ir/event-class-internal.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/ctf-writer/clock-internal.h>
41 #include <babeltrace/ctf-ir/field-types-internal.h>
42 #include <babeltrace/ctf-ir/attributes-internal.h>
43 #include <babeltrace/ctf-ir/validation-internal.h>
44 #include <babeltrace/ctf-ir/visitor-internal.h>
45 #include <babeltrace/ctf-ir/utils.h>
46 #include <babeltrace/compiler-internal.h>
47 #include <babeltrace/values.h>
48 #include <babeltrace/values-internal.h>
49 #include <babeltrace/ref.h>
50 #include <babeltrace/types.h>
51 #include <babeltrace/endian-internal.h>
57 #define DEFAULT_IDENTIFIER_SIZE 128
58 #define DEFAULT_METADATA_STRING_SIZE 4096
60 struct listener_wrapper
{
61 bt_listener_cb listener
;
65 struct bt_trace_is_static_listener_elem
{
66 bt_trace_is_static_listener func
;
67 bt_trace_listener_removed removed
;
72 void bt_trace_destroy(struct bt_object
*obj
);
74 void bt_trace_freeze(struct bt_trace
*trace
);
77 const unsigned int field_type_aliases_alignments
[] = {
78 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
79 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
80 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
81 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
85 const unsigned int field_type_aliases_sizes
[] = {
86 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
87 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
88 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
89 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
90 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
91 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
94 struct bt_trace
*bt_trace_create(void)
96 struct bt_trace
*trace
= NULL
;
98 trace
= g_new0(struct bt_trace
, 1);
100 BT_LOGE_STR("Failed to allocate one trace.");
104 BT_LOGD_STR("Creating trace object.");
105 trace
->native_byte_order
= BT_BYTE_ORDER_UNSPECIFIED
;
106 bt_object_init(trace
, bt_trace_destroy
);
107 trace
->clocks
= g_ptr_array_new_with_free_func(
108 (GDestroyNotify
) bt_put
);
109 if (!trace
->clocks
) {
110 BT_LOGE_STR("Failed to allocate one GPtrArray.");
114 trace
->streams
= g_ptr_array_new_with_free_func(
115 (GDestroyNotify
) bt_object_release
);
116 if (!trace
->streams
) {
117 BT_LOGE_STR("Failed to allocate one GPtrArray.");
121 trace
->stream_classes
= g_ptr_array_new_with_free_func(
122 (GDestroyNotify
) bt_object_release
);
123 if (!trace
->stream_classes
) {
124 BT_LOGE_STR("Failed to allocate one GPtrArray.");
128 /* Create the environment array object */
129 trace
->environment
= bt_attributes_create();
130 if (!trace
->environment
) {
131 BT_LOGE_STR("Cannot create empty attributes object.");
135 trace
->listeners
= g_ptr_array_new_with_free_func(
136 (GDestroyNotify
) g_free
);
137 if (!trace
->listeners
) {
138 BT_LOGE_STR("Failed to allocate one GPtrArray.");
142 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
143 sizeof(struct bt_trace_is_static_listener_elem
));
144 if (!trace
->is_static_listeners
) {
145 BT_LOGE_STR("Failed to allocate one GArray.");
149 BT_LOGD("Created trace object: addr=%p", trace
);
157 const char *bt_trace_get_name(struct bt_trace
*trace
)
159 const char *name
= NULL
;
162 BT_LOGW_STR("Invalid parameter: trace is NULL.");
170 name
= trace
->name
->str
;
175 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
180 BT_LOGW_STR("Invalid parameter: trace is NULL.");
186 BT_LOGW_STR("Invalid parameter: name is NULL.");
192 BT_LOGW("Invalid parameter: trace is frozen: "
193 "addr=%p, name=\"%s\"",
194 trace
, bt_trace_get_name(trace
));
199 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
202 BT_LOGE_STR("Failed to allocate one GString.");
207 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
213 const unsigned char *bt_trace_get_uuid(struct bt_trace
*trace
)
215 return trace
&& trace
->uuid_set
? trace
->uuid
: NULL
;
218 int bt_trace_set_uuid(struct bt_trace
*trace
, const unsigned char *uuid
)
223 BT_LOGW_STR("Invalid parameter: trace is NULL.");
229 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
235 BT_LOGW("Invalid parameter: trace is frozen: "
236 "addr=%p, name=\"%s\"",
237 trace
, bt_trace_get_name(trace
));
242 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
243 trace
->uuid_set
= BT_TRUE
;
244 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
245 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
246 trace
, bt_trace_get_name(trace
),
247 (unsigned int) uuid
[0],
248 (unsigned int) uuid
[1],
249 (unsigned int) uuid
[2],
250 (unsigned int) uuid
[3],
251 (unsigned int) uuid
[4],
252 (unsigned int) uuid
[5],
253 (unsigned int) uuid
[6],
254 (unsigned int) uuid
[7],
255 (unsigned int) uuid
[8],
256 (unsigned int) uuid
[9],
257 (unsigned int) uuid
[10],
258 (unsigned int) uuid
[11],
259 (unsigned int) uuid
[12],
260 (unsigned int) uuid
[13],
261 (unsigned int) uuid
[14],
262 (unsigned int) uuid
[15]);
268 void bt_trace_destroy(struct bt_object
*obj
)
270 struct bt_trace
*trace
;
272 trace
= container_of(obj
, struct bt_trace
, base
);
274 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
275 trace
, bt_trace_get_name(trace
));
278 * Call remove listeners first so that everything else still
279 * exists in the trace.
281 if (trace
->is_static_listeners
) {
284 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
285 struct bt_trace_is_static_listener_elem elem
=
286 g_array_index(trace
->is_static_listeners
,
287 struct bt_trace_is_static_listener_elem
, i
);
290 elem
.removed(trace
, elem
.data
);
294 g_array_free(trace
->is_static_listeners
, TRUE
);
297 if (trace
->listeners
) {
298 g_ptr_array_free(trace
->listeners
, TRUE
);
301 if (trace
->environment
) {
302 BT_LOGD_STR("Destroying environment attributes.");
303 bt_attributes_destroy(trace
->environment
);
307 g_string_free(trace
->name
, TRUE
);
311 BT_LOGD_STR("Putting clock classes.");
312 g_ptr_array_free(trace
->clocks
, TRUE
);
315 if (trace
->streams
) {
316 BT_LOGD_STR("Destroying streams.");
317 g_ptr_array_free(trace
->streams
, TRUE
);
320 if (trace
->stream_classes
) {
321 BT_LOGD_STR("Destroying stream classes.");
322 g_ptr_array_free(trace
->stream_classes
, TRUE
);
325 BT_LOGD_STR("Putting packet header field type.");
326 bt_put(trace
->packet_header_type
);
330 int bt_trace_set_environment_field(struct bt_trace
*trace
,
331 const char *name
, struct bt_value
*value
)
336 BT_LOGW_STR("Invalid parameter: trace is NULL.");
342 BT_LOGW_STR("Invalid parameter: name is NULL.");
348 BT_LOGW_STR("Invalid parameter: value is NULL.");
353 if (!bt_identifier_is_valid(name
)) {
354 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
355 "trace-addr=%p, trace-name=\"%s\", "
357 trace
, bt_trace_get_name(trace
), name
);
362 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
363 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
364 "trace-addr=%p, trace-name=\"%s\", "
365 "env-name=\"%s\", env-value-type=%s",
366 trace
, bt_trace_get_name(trace
), name
,
367 bt_value_type_string(bt_value_get_type(value
)));
372 if (trace
->is_static
) {
373 BT_LOGW("Invalid parameter: trace is static: "
374 "addr=%p, name=\"%s\"",
375 trace
, bt_trace_get_name(trace
));
382 * New environment fields may be added to a frozen trace,
383 * but existing fields may not be changed.
385 * The object passed is frozen like all other attributes.
387 struct bt_value
*attribute
=
388 bt_attributes_get_field_value_by_name(
389 trace
->environment
, name
);
392 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
393 "trace-addr=%p, trace-name=\"%s\", "
395 trace
, bt_trace_get_name(trace
), name
);
401 bt_value_freeze(value
);
404 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
407 BT_LOGE("Cannot set environment field's value: "
408 "trace-addr=%p, trace-name=\"%s\", "
410 trace
, bt_trace_get_name(trace
), name
);
412 BT_LOGV("Set environment field's value: "
413 "trace-addr=%p, trace-name=\"%s\", "
414 "env-name=\"%s\", value-addr=%p",
415 trace
, bt_trace_get_name(trace
), name
, value
);
422 int bt_trace_set_environment_field_string(struct bt_trace
*trace
,
423 const char *name
, const char *value
)
426 struct bt_value
*env_value_string_obj
= NULL
;
429 BT_LOGW_STR("Invalid parameter: value is NULL.");
434 env_value_string_obj
= bt_value_string_create_init(value
);
435 if (!env_value_string_obj
) {
436 BT_LOGE_STR("Cannot create string value object.");
441 /* bt_trace_set_environment_field() logs errors */
442 ret
= bt_trace_set_environment_field(trace
, name
,
443 env_value_string_obj
);
446 bt_put(env_value_string_obj
);
450 int bt_trace_set_environment_field_integer(struct bt_trace
*trace
,
451 const char *name
, int64_t value
)
454 struct bt_value
*env_value_integer_obj
= NULL
;
456 env_value_integer_obj
= bt_value_integer_create_init(value
);
457 if (!env_value_integer_obj
) {
458 BT_LOGE_STR("Cannot create integer value object.");
463 /* bt_trace_set_environment_field() logs errors */
464 ret
= bt_trace_set_environment_field(trace
, name
,
465 env_value_integer_obj
);
468 bt_put(env_value_integer_obj
);
472 int64_t bt_trace_get_environment_field_count(struct bt_trace
*trace
)
477 BT_LOGW_STR("Invalid parameter: trace is NULL.");
482 ret
= bt_attributes_get_count(trace
->environment
);
490 bt_trace_get_environment_field_name_by_index(struct bt_trace
*trace
,
493 const char *ret
= NULL
;
496 BT_LOGW_STR("Invalid parameter: trace is NULL.");
500 ret
= bt_attributes_get_field_name(trace
->environment
, index
);
506 struct bt_value
*bt_trace_get_environment_field_value_by_index(
507 struct bt_trace
*trace
, uint64_t index
)
509 struct bt_value
*ret
= NULL
;
512 BT_LOGW_STR("Invalid parameter: trace is NULL.");
516 ret
= bt_attributes_get_field_value(trace
->environment
, index
);
522 struct bt_value
*bt_trace_get_environment_field_value_by_name(
523 struct bt_trace
*trace
, const char *name
)
525 struct bt_value
*ret
= NULL
;
528 BT_LOGW_STR("Invalid parameter: trace is NULL.");
533 BT_LOGW_STR("Invalid parameter: name is NULL.");
537 ret
= bt_attributes_get_field_value_by_name(trace
->environment
,
544 int bt_trace_add_clock_class(struct bt_trace
*trace
,
545 struct bt_clock_class
*clock_class
)
550 BT_LOGW_STR("Invalid parameter: trace is NULL.");
555 if (trace
->is_static
) {
556 BT_LOGW("Invalid parameter: trace is static: "
557 "addr=%p, name=\"%s\"",
558 trace
, bt_trace_get_name(trace
));
563 if (!bt_clock_class_is_valid(clock_class
)) {
564 BT_LOGW("Invalid parameter: clock class is invalid: "
565 "trace-addr=%p, trace-name=\"%s\", "
566 "clock-class-addr=%p, clock-class-name=\"%s\"",
567 trace
, bt_trace_get_name(trace
),
568 clock_class
, bt_clock_class_get_name(clock_class
));
573 /* Check for duplicate clock classes */
574 if (bt_trace_has_clock_class(trace
, clock_class
)) {
575 BT_LOGW("Invalid parameter: clock class already exists in trace: "
576 "trace-addr=%p, trace-name=\"%s\", "
577 "clock-class-addr=%p, clock-class-name=\"%s\"",
578 trace
, bt_trace_get_name(trace
),
579 clock_class
, bt_clock_class_get_name(clock_class
));
585 g_ptr_array_add(trace
->clocks
, clock_class
);
588 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
589 bt_clock_class_freeze(clock_class
);
592 BT_LOGV("Added clock class to trace: "
593 "trace-addr=%p, trace-name=\"%s\", "
594 "clock-class-addr=%p, clock-class-name=\"%s\"",
595 trace
, bt_trace_get_name(trace
),
596 clock_class
, bt_clock_class_get_name(clock_class
));
602 int64_t bt_trace_get_clock_class_count(struct bt_trace
*trace
)
604 int64_t ret
= (int64_t) -1;
607 BT_LOGW_STR("Invalid parameter: trace is NULL.");
611 ret
= trace
->clocks
->len
;
616 struct bt_clock_class
*bt_trace_get_clock_class_by_index(
617 struct bt_trace
*trace
, uint64_t index
)
619 struct bt_clock_class
*clock_class
= NULL
;
622 BT_LOGW_STR("Invalid parameter: trace is NULL.");
626 if (index
>= trace
->clocks
->len
) {
627 BT_LOGW("Invalid parameter: index is out of bounds: "
628 "addr=%p, name=\"%s\", "
629 "index=%" PRIu64
", count=%u",
630 trace
, bt_trace_get_name(trace
),
631 index
, trace
->clocks
->len
);
635 clock_class
= g_ptr_array_index(trace
->clocks
, index
);
642 bool packet_header_field_type_is_valid(struct bt_trace
*trace
,
643 struct bt_field_type
*packet_header_type
)
646 bool is_valid
= true;
647 struct bt_field_type
*field_type
= NULL
;
649 if (!packet_header_type
) {
651 * No packet header field type: trace must have only
652 * one stream. At this point the stream class being
653 * added is not part of the trace yet, so we validate
654 * that the trace contains no stream classes yet.
656 if (trace
->stream_classes
->len
>= 1) {
657 BT_LOGW_STR("Invalid packet header field type: "
658 "packet header field type does not exist but there's more than one stream class in the trace.");
662 /* No packet header field type: valid at this point */
666 /* Packet header field type, if it exists, must be a structure */
667 if (!bt_field_type_is_structure(packet_header_type
)) {
668 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
669 "ft-addr=%p, ft-id=%s",
671 bt_field_type_id_string(packet_header_type
->id
));
676 * If there's a `magic` field, it must be a 32-bit unsigned
677 * integer field type. Also it must be the first field of the
678 * packet header field type.
680 field_type
= bt_field_type_structure_get_field_type_by_name(
681 packet_header_type
, "magic");
683 const char *field_name
;
685 if (!bt_field_type_is_integer(field_type
)) {
686 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
687 "magic-ft-addr=%p, magic-ft-id=%s",
689 bt_field_type_id_string(field_type
->id
));
693 if (bt_field_type_integer_is_signed(field_type
)) {
694 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
695 "magic-ft-addr=%p", field_type
);
699 if (bt_field_type_integer_get_size(field_type
) != 32) {
700 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
701 "magic-ft-addr=%p, magic-ft-size=%u",
703 bt_field_type_integer_get_size(field_type
));
707 ret
= bt_field_type_structure_get_field_by_index(
708 packet_header_type
, &field_name
, NULL
, 0);
711 if (strcmp(field_name
, "magic") != 0) {
712 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
713 "magic-ft-addr=%p, first-field-name=\"%s\"",
714 field_type
, field_name
);
722 * If there's a `uuid` field, it must be an array field type of
723 * length 16 with an 8-bit unsigned integer element field type.
725 field_type
= bt_field_type_structure_get_field_type_by_name(
726 packet_header_type
, "uuid");
728 struct bt_field_type
*elem_ft
;
730 if (!bt_field_type_is_array(field_type
)) {
731 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
732 "uuid-ft-addr=%p, uuid-ft-id=%s",
734 bt_field_type_id_string(field_type
->id
));
738 if (bt_field_type_array_get_length(field_type
) != 16) {
739 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
740 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
742 bt_field_type_array_get_length(field_type
));
746 elem_ft
= bt_field_type_array_get_element_type(field_type
);
749 if (!bt_field_type_is_integer(elem_ft
)) {
750 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
751 "elem-ft-addr=%p, elem-ft-id=%s",
753 bt_field_type_id_string(elem_ft
->id
));
758 if (bt_field_type_integer_is_signed(elem_ft
)) {
759 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
760 "elem-ft-addr=%p", elem_ft
);
765 if (bt_field_type_integer_get_size(elem_ft
) != 8) {
766 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
767 "elem-ft-addr=%p, elem-ft-size=%u",
769 bt_field_type_integer_get_size(elem_ft
));
779 * The `stream_id` field must exist if there's more than one
780 * stream classes in the trace.
782 field_type
= bt_field_type_structure_get_field_type_by_name(
783 packet_header_type
, "stream_id");
785 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
786 BT_LOGW_STR("Invalid packet header field type: "
787 "`stream_id` field does not exist but there's more than one stream class in the trace.");
792 * If there's a `stream_id` field, it must be an unsigned
793 * integer field type.
796 if (!bt_field_type_is_integer(field_type
)) {
797 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
798 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
800 bt_field_type_id_string(field_type
->id
));
804 if (bt_field_type_integer_is_signed(field_type
)) {
805 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
806 "stream-id-ft-addr=%p", field_type
);
814 * If there's a `packet_seq_num` field, it must be an unsigned
815 * integer field type.
817 field_type
= bt_field_type_structure_get_field_type_by_name(
818 packet_header_type
, "packet_seq_num");
820 if (!bt_field_type_is_integer(field_type
)) {
821 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
822 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
824 bt_field_type_id_string(field_type
->id
));
828 if (bt_field_type_integer_is_signed(field_type
)) {
829 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
830 "packet-seq-num-ft-addr=%p", field_type
);
848 bool packet_context_field_type_is_valid(struct bt_trace
*trace
,
849 struct bt_stream_class
*stream_class
,
850 struct bt_field_type
*packet_context_type
)
852 bool is_valid
= true;
853 struct bt_field_type
*field_type
= NULL
;
855 if (!packet_context_type
) {
856 /* No packet context field type: valid at this point */
860 /* Packet context field type, if it exists, must be a structure */
861 if (!bt_field_type_is_structure(packet_context_type
)) {
862 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
863 "ft-addr=%p, ft-id=%s",
865 bt_field_type_id_string(packet_context_type
->id
));
870 * If there's a `packet_size` field, it must be an unsigned
871 * integer field type.
873 field_type
= bt_field_type_structure_get_field_type_by_name(
874 packet_context_type
, "packet_size");
876 if (!bt_field_type_is_integer(field_type
)) {
877 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
878 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
880 bt_field_type_id_string(field_type
->id
));
884 if (bt_field_type_integer_is_signed(field_type
)) {
885 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
886 "packet-size-ft-addr=%p", field_type
);
894 * If there's a `content_size` field, it must be an unsigned
895 * integer field type.
897 field_type
= bt_field_type_structure_get_field_type_by_name(
898 packet_context_type
, "content_size");
900 if (!bt_field_type_is_integer(field_type
)) {
901 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
902 "content-size-ft-addr=%p, content-size-ft-id=%s",
904 bt_field_type_id_string(field_type
->id
));
908 if (bt_field_type_integer_is_signed(field_type
)) {
909 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
910 "content-size-ft-addr=%p", field_type
);
918 * If there's a `events_discarded` field, it must be an unsigned
919 * integer field type.
921 field_type
= bt_field_type_structure_get_field_type_by_name(
922 packet_context_type
, "events_discarded");
924 if (!bt_field_type_is_integer(field_type
)) {
925 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
926 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
928 bt_field_type_id_string(field_type
->id
));
932 if (bt_field_type_integer_is_signed(field_type
)) {
933 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
934 "events-discarded-ft-addr=%p", field_type
);
942 * If there's a `timestamp_begin` field, it must be an unsigned
943 * integer field type. Also, if the trace is not a CTF writer's
944 * trace, then we cannot automatically set the mapped clock
945 * class of this field, so it must have a mapped clock class.
947 field_type
= bt_field_type_structure_get_field_type_by_name(
948 packet_context_type
, "timestamp_begin");
950 if (!bt_field_type_is_integer(field_type
)) {
951 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
952 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
954 bt_field_type_id_string(field_type
->id
));
958 if (bt_field_type_integer_is_signed(field_type
)) {
959 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
960 "timestamp-begin-ft-addr=%p", field_type
);
964 if (!trace
->is_created_by_writer
) {
965 struct bt_clock_class
*clock_class
=
966 bt_field_type_integer_get_mapped_clock_class(
971 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
972 "timestamp-begin-ft-addr=%p", field_type
);
981 * If there's a `timestamp_end` field, it must be an unsigned
982 * integer field type. Also, if the trace is not a CTF writer's
983 * trace, then we cannot automatically set the mapped clock
984 * class of this field, so it must have a mapped clock class.
986 field_type
= bt_field_type_structure_get_field_type_by_name(
987 packet_context_type
, "timestamp_end");
989 if (!bt_field_type_is_integer(field_type
)) {
990 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
991 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
993 bt_field_type_id_string(field_type
->id
));
997 if (bt_field_type_integer_is_signed(field_type
)) {
998 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
999 "timestamp-end-ft-addr=%p", field_type
);
1003 if (!trace
->is_created_by_writer
) {
1004 struct bt_clock_class
*clock_class
=
1005 bt_field_type_integer_get_mapped_clock_class(
1008 bt_put(clock_class
);
1010 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
1011 "timestamp-end-ft-addr=%p", field_type
);
1030 bool event_header_field_type_is_valid(struct bt_trace
*trace
,
1031 struct bt_stream_class
*stream_class
,
1032 struct bt_field_type
*event_header_type
)
1034 bool is_valid
= true;
1035 struct bt_field_type
*field_type
= NULL
;
1038 * We do not validate that the `timestamp` field exists here
1039 * because CTF does not require this exact name to be mapped to
1043 if (!event_header_type
) {
1045 * No event header field type: stream class must have
1046 * only one event class.
1048 if (bt_stream_class_get_event_class_count(stream_class
) > 1) {
1049 BT_LOGW_STR("Invalid event header field type: "
1050 "event header field type does not exist but there's more than one event class in the stream class.");
1054 /* No event header field type: valid at this point */
1058 /* Event header field type, if it exists, must be a structure */
1059 if (!bt_field_type_is_structure(event_header_type
)) {
1060 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1061 "ft-addr=%p, ft-id=%s",
1063 bt_field_type_id_string(event_header_type
->id
));
1068 * If there's an `id` field, it must be an unsigned integer
1069 * field type or an enumeration field type with an unsigned
1070 * integer container field type.
1072 field_type
= bt_field_type_structure_get_field_type_by_name(
1073 event_header_type
, "id");
1075 struct bt_field_type
*int_ft
;
1077 if (bt_field_type_is_integer(field_type
)) {
1078 int_ft
= bt_get(field_type
);
1079 } else if (bt_field_type_is_enumeration(field_type
)) {
1080 int_ft
= bt_field_type_enumeration_get_container_type(
1083 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1084 "id-ft-addr=%p, id-ft-id=%s",
1086 bt_field_type_id_string(field_type
->id
));
1091 if (bt_field_type_integer_is_signed(int_ft
)) {
1092 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1093 "id-ft-addr=%p", int_ft
);
1111 int bt_trace_add_stream_class(struct bt_trace
*trace
,
1112 struct bt_stream_class
*stream_class
)
1117 struct bt_validation_output trace_sc_validation_output
= { 0 };
1118 struct bt_validation_output
*ec_validation_outputs
= NULL
;
1119 const enum bt_validation_flag trace_sc_validation_flags
=
1120 BT_VALIDATION_FLAG_TRACE
|
1121 BT_VALIDATION_FLAG_STREAM
;
1122 const enum bt_validation_flag ec_validation_flags
=
1123 BT_VALIDATION_FLAG_EVENT
;
1124 struct bt_field_type
*packet_header_type
= NULL
;
1125 struct bt_field_type
*packet_context_type
= NULL
;
1126 struct bt_field_type
*event_header_type
= NULL
;
1127 struct bt_field_type
*stream_event_ctx_type
= NULL
;
1128 int64_t event_class_count
;
1129 struct bt_trace
*current_parent_trace
= NULL
;
1132 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1137 if (!stream_class
) {
1138 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1143 if (trace
->is_static
) {
1144 BT_LOGW_STR("Invalid parameter: trace is static.");
1149 BT_LOGD("Adding stream class to trace: "
1150 "trace-addr=%p, trace-name=\"%s\", "
1151 "stream-class-addr=%p, stream-class-name=\"%s\", "
1152 "stream-class-id=%" PRId64
,
1153 trace
, bt_trace_get_name(trace
),
1154 stream_class
, bt_stream_class_get_name(stream_class
),
1155 bt_stream_class_get_id(stream_class
));
1157 current_parent_trace
= bt_stream_class_get_trace(stream_class
);
1158 if (current_parent_trace
) {
1159 /* Stream class is already associated to a trace, abort. */
1160 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1161 "stream-class-trace-addr=%p, "
1162 "stream-class-trace-name=\"%s\"",
1163 current_parent_trace
,
1164 bt_trace_get_name(current_parent_trace
));
1170 bt_stream_class_get_event_class_count(stream_class
);
1171 assert(event_class_count
>= 0);
1173 if (stream_class
->clock
) {
1174 struct bt_clock_class
*stream_clock_class
=
1175 stream_class
->clock
->clock_class
;
1177 if (trace
->is_created_by_writer
) {
1179 * Make sure this clock was also added to the
1180 * trace (potentially through its CTF writer
1185 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1186 if (trace
->clocks
->pdata
[i
] ==
1187 stream_clock_class
) {
1193 if (i
== trace
->clocks
->len
) {
1195 BT_LOGW("Stream class's clock's class is not part of the trace: "
1196 "clock-class-addr=%p, clock-class-name=\"%s\"",
1198 bt_clock_class_get_name(stream_clock_class
));
1204 * This trace was NOT created by a CTF writer,
1205 * thus do not allow the stream class to add to
1206 * have a clock at all. Those are two
1207 * independent APIs (non-writer and writer
1208 * APIs), and isolating them simplifies things.
1210 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1211 "clock-class-addr=%p, clock-class-name=\"%s\"",
1213 bt_clock_class_get_name(stream_clock_class
));
1220 * We're about to freeze both the trace and the stream class.
1221 * Also, each event class contained in this stream class are
1224 * This trace, this stream class, and all its event classes
1225 * should be valid at this point.
1227 * Validate trace and stream class first, then each event
1228 * class of this stream class can be validated individually.
1230 packet_header_type
=
1231 bt_trace_get_packet_header_type(trace
);
1232 packet_context_type
=
1233 bt_stream_class_get_packet_context_type(stream_class
);
1235 bt_stream_class_get_event_header_type(stream_class
);
1236 stream_event_ctx_type
=
1237 bt_stream_class_get_event_context_type(stream_class
);
1239 BT_LOGD("Validating trace and stream class field types.");
1240 ret
= bt_validate_class_types(trace
->environment
,
1241 packet_header_type
, packet_context_type
, event_header_type
,
1242 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1243 stream_class
->valid
, 1, &trace_sc_validation_output
,
1244 trace_sc_validation_flags
);
1245 BT_PUT(packet_header_type
);
1246 BT_PUT(packet_context_type
);
1247 BT_PUT(event_header_type
);
1248 BT_PUT(stream_event_ctx_type
);
1252 * This means something went wrong during the validation
1253 * process, not that the objects are invalid.
1255 BT_LOGE("Failed to validate trace and stream class field types: "
1260 if ((trace_sc_validation_output
.valid_flags
&
1261 trace_sc_validation_flags
) !=
1262 trace_sc_validation_flags
) {
1263 /* Invalid trace/stream class */
1264 BT_LOGW("Invalid trace or stream class field types: "
1266 trace_sc_validation_output
.valid_flags
);
1271 if (event_class_count
> 0) {
1272 ec_validation_outputs
= g_new0(struct bt_validation_output
,
1274 if (!ec_validation_outputs
) {
1275 BT_LOGE_STR("Failed to allocate one validation output structure.");
1281 /* Validate each event class individually */
1282 for (i
= 0; i
< event_class_count
; i
++) {
1283 struct bt_event_class
*event_class
=
1284 bt_stream_class_get_event_class_by_index(
1286 struct bt_field_type
*event_context_type
= NULL
;
1287 struct bt_field_type
*event_payload_type
= NULL
;
1289 event_context_type
=
1290 bt_event_class_get_context_type(event_class
);
1291 event_payload_type
=
1292 bt_event_class_get_payload_type(event_class
);
1295 * It is important to use the field types returned by
1296 * the previous trace and stream class validation here
1297 * because copies could have been made.
1299 BT_LOGD("Validating event class's field types: "
1300 "addr=%p, name=\"%s\", id=%" PRId64
,
1301 event_class
, bt_event_class_get_name(event_class
),
1302 bt_event_class_get_id(event_class
));
1303 ret
= bt_validate_class_types(trace
->environment
,
1304 trace_sc_validation_output
.packet_header_type
,
1305 trace_sc_validation_output
.packet_context_type
,
1306 trace_sc_validation_output
.event_header_type
,
1307 trace_sc_validation_output
.stream_event_ctx_type
,
1308 event_context_type
, event_payload_type
,
1309 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1310 ec_validation_flags
);
1311 BT_PUT(event_context_type
);
1312 BT_PUT(event_payload_type
);
1313 BT_PUT(event_class
);
1316 BT_LOGE("Failed to validate event class field types: "
1321 if ((ec_validation_outputs
[i
].valid_flags
&
1322 ec_validation_flags
) != ec_validation_flags
) {
1323 /* Invalid event class */
1324 BT_LOGW("Invalid event class field types: "
1326 ec_validation_outputs
[i
].valid_flags
);
1332 stream_id
= bt_stream_class_get_id(stream_class
);
1333 if (stream_id
< 0) {
1334 stream_id
= trace
->next_stream_id
++;
1335 if (stream_id
< 0) {
1336 BT_LOGE_STR("No more stream class IDs available.");
1341 /* Try to assign a new stream id */
1342 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1343 if (stream_id
== bt_stream_class_get_id(
1344 trace
->stream_classes
->pdata
[i
])) {
1345 /* Duplicate stream id found */
1346 BT_LOGW("Duplicate stream class ID: "
1347 "id=%" PRId64
, (int64_t) stream_id
);
1353 if (bt_stream_class_set_id_no_check(stream_class
,
1355 /* TODO Should retry with a different stream id */
1356 BT_LOGE("Cannot set stream class's ID: "
1357 "id=%" PRId64
, (int64_t) stream_id
);
1364 * At this point all the field types in the validation output
1365 * are valid. Validate the semantics of some scopes according to
1366 * the CTF specification.
1368 if (!packet_header_field_type_is_valid(trace
,
1369 trace_sc_validation_output
.packet_header_type
)) {
1370 BT_LOGW_STR("Invalid trace's packet header field type.");
1375 if (!packet_context_field_type_is_valid(trace
,
1377 trace_sc_validation_output
.packet_context_type
)) {
1378 BT_LOGW_STR("Invalid stream class's packet context field type.");
1383 if (!event_header_field_type_is_valid(trace
,
1385 trace_sc_validation_output
.event_header_type
)) {
1386 BT_LOGW_STR("Invalid steam class's event header field type.");
1392 * Now is the time to automatically map specific field types of
1393 * the stream class's packet context and event header field
1394 * types to the stream class's clock's class if they are not
1395 * mapped to a clock class yet. We do it here because we know
1396 * that after this point, everything is frozen so it won't be
1397 * possible for the user to modify the stream class's clock, or
1398 * to map those field types to other clock classes.
1400 if (trace
->is_created_by_writer
) {
1401 if (bt_stream_class_map_clock_class(stream_class
,
1402 trace_sc_validation_output
.packet_context_type
,
1403 trace_sc_validation_output
.event_header_type
)) {
1404 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1410 bt_object_set_parent(stream_class
, trace
);
1411 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1414 * At this point we know that the function will be successful.
1415 * Therefore we can replace the trace and stream class field
1416 * types with what's in their validation output structure and
1417 * mark them as valid. We can also replace the field types of
1418 * all the event classes of the stream class and mark them as
1421 bt_validation_replace_types(trace
, stream_class
, NULL
,
1422 &trace_sc_validation_output
, trace_sc_validation_flags
);
1424 stream_class
->valid
= 1;
1427 * Put what was not moved in bt_validation_replace_types().
1429 bt_validation_output_put_types(&trace_sc_validation_output
);
1431 for (i
= 0; i
< event_class_count
; i
++) {
1432 struct bt_event_class
*event_class
=
1433 bt_stream_class_get_event_class_by_index(
1436 bt_validation_replace_types(NULL
, NULL
, event_class
,
1437 &ec_validation_outputs
[i
], ec_validation_flags
);
1438 event_class
->valid
= 1;
1439 BT_PUT(event_class
);
1442 * Put what was not moved in
1443 * bt_validation_replace_types().
1445 bt_validation_output_put_types(&ec_validation_outputs
[i
]);
1449 * Freeze the trace and the stream class.
1451 bt_stream_class_freeze(stream_class
);
1452 bt_trace_freeze(trace
);
1454 /* Notifiy listeners of the trace's schema modification. */
1455 bt_stream_class_visit(stream_class
,
1456 bt_trace_object_modification
, trace
);
1457 BT_LOGD("Added stream class to trace: "
1458 "trace-addr=%p, trace-name=\"%s\", "
1459 "stream-class-addr=%p, stream-class-name=\"%s\", "
1460 "stream-class-id=%" PRId64
,
1461 trace
, bt_trace_get_name(trace
),
1462 stream_class
, bt_stream_class_get_name(stream_class
),
1463 bt_stream_class_get_id(stream_class
));
1467 bt_object_set_parent(stream_class
, NULL
);
1469 if (ec_validation_outputs
) {
1470 for (i
= 0; i
< event_class_count
; i
++) {
1471 bt_validation_output_put_types(
1472 &ec_validation_outputs
[i
]);
1477 g_free(ec_validation_outputs
);
1478 bt_validation_output_put_types(&trace_sc_validation_output
);
1479 bt_put(current_parent_trace
);
1480 assert(!packet_header_type
);
1481 assert(!packet_context_type
);
1482 assert(!event_header_type
);
1483 assert(!stream_event_ctx_type
);
1487 int64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
1492 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1497 ret
= (int64_t) trace
->streams
->len
;
1503 struct bt_stream
*bt_trace_get_stream_by_index(
1504 struct bt_trace
*trace
,
1507 struct bt_stream
*stream
= NULL
;
1510 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1514 if (index
>= trace
->streams
->len
) {
1515 BT_LOGW("Invalid parameter: index is out of bounds: "
1516 "addr=%p, name=\"%s\", "
1517 "index=%" PRIu64
", count=%u",
1518 trace
, bt_trace_get_name(trace
),
1519 index
, trace
->streams
->len
);
1523 stream
= bt_get(g_ptr_array_index(trace
->streams
, index
));
1529 int64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
1534 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1539 ret
= (int64_t) trace
->stream_classes
->len
;
1544 struct bt_stream_class
*bt_trace_get_stream_class_by_index(
1545 struct bt_trace
*trace
, uint64_t index
)
1547 struct bt_stream_class
*stream_class
= NULL
;
1550 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1554 if (index
>= trace
->stream_classes
->len
) {
1555 BT_LOGW("Invalid parameter: index is out of bounds: "
1556 "addr=%p, name=\"%s\", "
1557 "index=%" PRIu64
", count=%u",
1558 trace
, bt_trace_get_name(trace
),
1559 index
, trace
->stream_classes
->len
);
1563 stream_class
= g_ptr_array_index(trace
->stream_classes
, index
);
1564 bt_get(stream_class
);
1566 return stream_class
;
1569 struct bt_stream_class
*bt_trace_get_stream_class_by_id(
1570 struct bt_trace
*trace
, uint64_t id_param
)
1573 struct bt_stream_class
*stream_class
= NULL
;
1574 int64_t id
= (int64_t) id_param
;
1577 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1582 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1583 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64
,
1584 trace
, bt_trace_get_name(trace
), id_param
);
1588 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1589 struct bt_stream_class
*stream_class_candidate
;
1591 stream_class_candidate
=
1592 g_ptr_array_index(trace
->stream_classes
, i
);
1594 if (bt_stream_class_get_id(stream_class_candidate
) ==
1596 stream_class
= stream_class_candidate
;
1597 bt_get(stream_class
);
1603 return stream_class
;
1606 struct bt_clock_class
*bt_trace_get_clock_class_by_name(
1607 struct bt_trace
*trace
, const char *name
)
1610 struct bt_clock_class
*clock_class
= NULL
;
1613 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1618 BT_LOGW_STR("Invalid parameter: name is NULL.");
1622 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1623 struct bt_clock_class
*cur_clk
=
1624 g_ptr_array_index(trace
->clocks
, i
);
1625 const char *cur_clk_name
= bt_clock_class_get_name(cur_clk
);
1627 if (!cur_clk_name
) {
1631 if (!strcmp(cur_clk_name
, name
)) {
1632 clock_class
= cur_clk
;
1633 bt_get(clock_class
);
1643 bt_bool
bt_trace_has_clock_class(struct bt_trace
*trace
,
1644 struct bt_clock_class
*clock_class
)
1646 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1649 assert(clock_class
);
1651 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1656 const char *get_byte_order_string(enum bt_byte_order byte_order
)
1660 switch (byte_order
) {
1661 case BT_BYTE_ORDER_LITTLE_ENDIAN
:
1664 case BT_BYTE_ORDER_BIG_ENDIAN
:
1667 case BT_BYTE_ORDER_NATIVE
:
1678 int append_trace_metadata(struct bt_trace
*trace
,
1679 struct metadata_context
*context
)
1681 unsigned char *uuid
= trace
->uuid
;
1684 if (trace
->native_byte_order
== BT_BYTE_ORDER_NATIVE
||
1685 trace
->native_byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
1686 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_BYTE_ORDER_NATIVE or BT_BYTE_ORDER_UNSPECIFIED at this point; "
1687 "set it with bt_trace_set_native_byte_order(): "
1688 "addr=%p, name=\"%s\"",
1689 trace
, bt_trace_get_name(trace
));
1694 g_string_append(context
->string
, "trace {\n");
1695 g_string_append(context
->string
, "\tmajor = 1;\n");
1696 g_string_append(context
->string
, "\tminor = 8;\n");
1697 assert(trace
->native_byte_order
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
1698 trace
->native_byte_order
== BT_BYTE_ORDER_BIG_ENDIAN
||
1699 trace
->native_byte_order
== BT_BYTE_ORDER_NETWORK
);
1701 if (trace
->uuid_set
) {
1702 g_string_append_printf(context
->string
,
1703 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1704 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1705 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1706 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1707 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1710 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1711 get_byte_order_string(trace
->native_byte_order
));
1713 if (trace
->packet_header_type
) {
1714 g_string_append(context
->string
, "\tpacket.header := ");
1715 context
->current_indentation_level
++;
1716 g_string_assign(context
->field_name
, "");
1717 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1718 ret
= bt_field_type_serialize(trace
->packet_header_type
,
1723 context
->current_indentation_level
--;
1726 g_string_append(context
->string
, ";\n};\n\n");
1732 void append_env_metadata(struct bt_trace
*trace
,
1733 struct metadata_context
*context
)
1738 env_size
= bt_attributes_get_count(trace
->environment
);
1739 if (env_size
<= 0) {
1743 g_string_append(context
->string
, "env {\n");
1745 for (i
= 0; i
< env_size
; i
++) {
1746 struct bt_value
*env_field_value_obj
= NULL
;
1747 const char *entry_name
;
1749 entry_name
= bt_attributes_get_field_name(
1750 trace
->environment
, i
);
1751 env_field_value_obj
= bt_attributes_get_field_value(
1752 trace
->environment
, i
);
1755 assert(env_field_value_obj
);
1757 switch (bt_value_get_type(env_field_value_obj
)) {
1758 case BT_VALUE_TYPE_INTEGER
:
1763 ret
= bt_value_integer_get(env_field_value_obj
,
1766 g_string_append_printf(context
->string
,
1767 "\t%s = %" PRId64
";\n", entry_name
,
1771 case BT_VALUE_TYPE_STRING
:
1774 const char *str_value
;
1775 char *escaped_str
= NULL
;
1777 ret
= bt_value_string_get(env_field_value_obj
,
1780 escaped_str
= g_strescape(str_value
, NULL
);
1782 BT_LOGE("Cannot escape string: string=\"%s\"",
1787 g_string_append_printf(context
->string
,
1788 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1797 BT_PUT(env_field_value_obj
);
1800 g_string_append(context
->string
, "};\n\n");
1803 char *bt_trace_get_metadata_string(struct bt_trace
*trace
)
1805 char *metadata
= NULL
;
1806 struct metadata_context
*context
= NULL
;
1811 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1815 context
= g_new0(struct metadata_context
, 1);
1817 BT_LOGE_STR("Failed to allocate one metadata context.");
1821 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1822 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1823 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1824 if (append_trace_metadata(trace
, context
)) {
1825 /* append_trace_metadata() logs errors */
1828 append_env_metadata(trace
, context
);
1829 g_ptr_array_foreach(trace
->clocks
,
1830 (GFunc
)bt_clock_class_serialize
, context
);
1832 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1833 /* bt_stream_class_serialize() logs details */
1834 err
= bt_stream_class_serialize(
1835 trace
->stream_classes
->pdata
[i
], context
);
1837 /* bt_stream_class_serialize() logs errors */
1842 metadata
= context
->string
->str
;
1845 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1846 g_string_free(context
->field_name
, TRUE
);
1853 enum bt_byte_order
bt_trace_get_native_byte_order(
1854 struct bt_trace
*trace
)
1856 enum bt_byte_order ret
= BT_BYTE_ORDER_UNKNOWN
;
1859 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1863 ret
= trace
->native_byte_order
;
1869 int bt_trace_set_native_byte_order(struct bt_trace
*trace
,
1870 enum bt_byte_order byte_order
)
1875 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1880 if (trace
->frozen
) {
1881 BT_LOGW("Invalid parameter: trace is frozen: "
1882 "addr=%p, name=\"%s\"",
1883 trace
, bt_trace_get_name(trace
));
1888 if (trace
->is_created_by_writer
&&
1889 byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
1890 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
1891 "addr=%p, name=\"%s\"",
1892 trace
, bt_trace_get_name(trace
));
1897 if (byte_order
!= BT_BYTE_ORDER_LITTLE_ENDIAN
&&
1898 byte_order
!= BT_BYTE_ORDER_BIG_ENDIAN
&&
1899 byte_order
!= BT_BYTE_ORDER_NETWORK
) {
1900 BT_LOGW("Invalid parameter: invalid byte order: "
1901 "addr=%p, name=\"%s\", bo=%s",
1902 trace
, bt_trace_get_name(trace
),
1903 bt_byte_order_string(byte_order
));
1908 trace
->native_byte_order
= byte_order
;
1909 BT_LOGV("Set trace's native byte order: "
1910 "addr=%p, name=\"%s\", bo=%s",
1911 trace
, bt_trace_get_name(trace
),
1912 bt_byte_order_string(byte_order
));
1918 struct bt_field_type
*bt_trace_get_packet_header_type(
1919 struct bt_trace
*trace
)
1921 struct bt_field_type
*field_type
= NULL
;
1924 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1928 bt_get(trace
->packet_header_type
);
1929 field_type
= trace
->packet_header_type
;
1934 int bt_trace_set_packet_header_type(struct bt_trace
*trace
,
1935 struct bt_field_type
*packet_header_type
)
1940 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1945 if (trace
->frozen
) {
1946 BT_LOGW("Invalid parameter: trace is frozen: "
1947 "addr=%p, name=\"%s\"",
1948 trace
, bt_trace_get_name(trace
));
1953 /* packet_header_type must be a structure. */
1954 if (packet_header_type
&&
1955 !bt_field_type_is_structure(packet_header_type
)) {
1956 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1957 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1958 trace
, bt_trace_get_name(trace
),
1960 bt_field_type_id_string(packet_header_type
->id
));
1965 bt_put(trace
->packet_header_type
);
1966 trace
->packet_header_type
= bt_get(packet_header_type
);
1967 BT_LOGV("Set trace's packet header field type: "
1968 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1969 trace
, bt_trace_get_name(trace
), packet_header_type
);
1975 int64_t get_stream_class_count(void *element
)
1977 return bt_trace_get_stream_class_count(
1978 (struct bt_trace
*) element
);
1982 void *get_stream_class(void *element
, int i
)
1984 return bt_trace_get_stream_class_by_index(
1985 (struct bt_trace
*) element
, i
);
1989 int visit_stream_class(void *object
, bt_visitor visitor
,void *data
)
1991 return bt_stream_class_visit(object
, visitor
, data
);
1994 int bt_trace_visit(struct bt_trace
*trace
,
1995 bt_visitor visitor
, void *data
)
1998 struct bt_visitor_object obj
= {
2000 .type
= BT_VISITOR_OBJECT_TYPE_TRACE
2004 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2010 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
2015 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
2016 trace
, bt_trace_get_name(trace
));
2017 ret
= visitor_helper(&obj
, get_stream_class_count
,
2018 get_stream_class
, visit_stream_class
, visitor
, data
);
2024 int invoke_listener(struct bt_visitor_object
*object
, void *data
)
2026 struct listener_wrapper
*listener_wrapper
= data
;
2028 listener_wrapper
->listener(object
, listener_wrapper
->data
);
2032 // TODO: add logging to this function once we use it internally.
2033 int bt_trace_add_listener(struct bt_trace
*trace
,
2034 bt_listener_cb listener
, void *listener_data
)
2037 struct listener_wrapper
*listener_wrapper
=
2038 g_new0(struct listener_wrapper
, 1);
2040 if (!trace
|| !listener
|| !listener_wrapper
) {
2045 listener_wrapper
->listener
= listener
;
2046 listener_wrapper
->data
= listener_data
;
2048 /* Visit the current schema. */
2049 ret
= bt_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2055 * Add listener to the array of callbacks which will be invoked on
2058 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2061 g_free(listener_wrapper
);
2066 int bt_trace_object_modification(struct bt_visitor_object
*object
,
2070 struct bt_trace
*trace
= trace_ptr
;
2075 if (trace
->listeners
->len
== 0) {
2079 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2080 struct listener_wrapper
*listener
=
2081 g_ptr_array_index(trace
->listeners
, i
);
2083 listener
->listener(object
, listener
->data
);
2090 struct bt_field_type
*get_field_type(enum field_type_alias alias
)
2093 unsigned int alignment
, size
;
2094 struct bt_field_type
*field_type
= NULL
;
2096 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2100 alignment
= field_type_aliases_alignments
[alias
];
2101 size
= field_type_aliases_sizes
[alias
];
2102 field_type
= bt_field_type_integer_create(size
);
2103 ret
= bt_field_type_set_alignment(field_type
, alignment
);
2112 void bt_trace_freeze(struct bt_trace
*trace
)
2116 if (trace
->frozen
) {
2120 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2121 trace
, bt_trace_get_name(trace
));
2122 BT_LOGD_STR("Freezing packet header field type.");
2123 bt_field_type_freeze(trace
->packet_header_type
);
2124 BT_LOGD_STR("Freezing environment attributes.");
2125 bt_attributes_freeze(trace
->environment
);
2127 if (trace
->clocks
->len
> 0) {
2128 BT_LOGD_STR("Freezing clock classes.");
2131 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2132 struct bt_clock_class
*clock_class
=
2133 g_ptr_array_index(trace
->clocks
, i
);
2135 bt_clock_class_freeze(clock_class
);
2141 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
2143 bt_bool is_static
= BT_FALSE
;
2146 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2150 is_static
= trace
->is_static
;
2156 int bt_trace_set_is_static(struct bt_trace
*trace
)
2162 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2167 trace
->is_static
= BT_TRUE
;
2168 bt_trace_freeze(trace
);
2169 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2170 trace
, bt_trace_get_name(trace
));
2172 /* Call all the "trace is static" listeners */
2173 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2174 struct bt_trace_is_static_listener_elem elem
=
2175 g_array_index(trace
->is_static_listeners
,
2176 struct bt_trace_is_static_listener_elem
, i
);
2179 elem
.func(trace
, elem
.data
);
2187 int bt_trace_add_is_static_listener(struct bt_trace
*trace
,
2188 bt_trace_is_static_listener listener
,
2189 bt_trace_listener_removed listener_removed
, void *data
)
2192 struct bt_trace_is_static_listener_elem new_elem
= {
2194 .removed
= listener_removed
,
2199 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2205 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2210 if (trace
->is_static
) {
2211 BT_LOGW("Invalid parameter: trace is already static: "
2212 "addr=%p, name=\"%s\"",
2213 trace
, bt_trace_get_name(trace
));
2218 if (trace
->in_remove_listener
) {
2219 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2220 "addr=%p, name=\"%s\"",
2221 trace
, bt_trace_get_name(trace
));
2226 /* Find the next available spot */
2227 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2228 struct bt_trace_is_static_listener_elem elem
=
2229 g_array_index(trace
->is_static_listeners
,
2230 struct bt_trace_is_static_listener_elem
, i
);
2237 if (i
== trace
->is_static_listeners
->len
) {
2238 g_array_append_val(trace
->is_static_listeners
, new_elem
);
2240 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
2243 BT_LOGV("Added \"trace is static\" listener: "
2244 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2245 "data-addr=%p, listener-id=%d",
2246 trace
, bt_trace_get_name(trace
), listener
, data
, i
);
2252 int bt_trace_remove_is_static_listener(
2253 struct bt_trace
*trace
, int listener_id
)
2256 struct bt_trace_is_static_listener_elem
*elem
;
2259 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2264 if (trace
->in_remove_listener
) {
2265 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2266 "addr=%p, name=\"%s\", listener-id=%d",
2267 trace
, bt_trace_get_name(trace
),
2273 if (listener_id
< 0) {
2274 BT_LOGW("Invalid listener ID: must be zero or positive: "
2275 "listener-id=%d", listener_id
);
2280 if (listener_id
>= trace
->is_static_listeners
->len
) {
2281 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2282 "addr=%p, name=\"%s\", listener-id=%d",
2283 trace
, bt_trace_get_name(trace
),
2289 elem
= &g_array_index(trace
->is_static_listeners
,
2290 struct bt_trace_is_static_listener_elem
,
2293 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2294 "addr=%p, name=\"%s\", listener-id=%d",
2295 trace
, bt_trace_get_name(trace
),
2301 if (elem
->removed
) {
2302 /* Call remove listener */
2303 BT_LOGV("Calling remove listener: "
2304 "trace-addr=%p, trace-name=\"%s\", "
2305 "listener-id=%d", trace
, bt_trace_get_name(trace
),
2307 trace
->in_remove_listener
= BT_TRUE
;
2308 elem
->removed(trace
, elem
->data
);
2309 trace
->in_remove_listener
= BT_FALSE
;
2313 elem
->removed
= NULL
;
2315 BT_LOGV("Removed \"trace is static\" listener: "
2316 "trace-addr=%p, trace-name=\"%s\", "
2317 "listener-id=%d", trace
, bt_trace_get_name(trace
),