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>
54 #include <babeltrace/assert-pre-internal.h>
60 #define DEFAULT_IDENTIFIER_SIZE 128
61 #define DEFAULT_METADATA_STRING_SIZE 4096
63 struct listener_wrapper
{
64 bt_listener_cb listener
;
68 struct bt_trace_is_static_listener_elem
{
69 bt_trace_is_static_listener func
;
70 bt_trace_listener_removed removed
;
75 void bt_trace_destroy(struct bt_object
*obj
);
77 void bt_trace_freeze(struct bt_trace
*trace
);
80 const unsigned int field_type_aliases_alignments
[] = {
81 [FIELD_TYPE_ALIAS_UINT5_T
] = 1,
82 [FIELD_TYPE_ALIAS_UINT8_T
... FIELD_TYPE_ALIAS_UINT16_T
] = 8,
83 [FIELD_TYPE_ALIAS_UINT27_T
] = 1,
84 [FIELD_TYPE_ALIAS_UINT32_T
... FIELD_TYPE_ALIAS_UINT64_T
] = 8,
88 const unsigned int field_type_aliases_sizes
[] = {
89 [FIELD_TYPE_ALIAS_UINT5_T
] = 5,
90 [FIELD_TYPE_ALIAS_UINT8_T
] = 8,
91 [FIELD_TYPE_ALIAS_UINT16_T
] = 16,
92 [FIELD_TYPE_ALIAS_UINT27_T
] = 27,
93 [FIELD_TYPE_ALIAS_UINT32_T
] = 32,
94 [FIELD_TYPE_ALIAS_UINT64_T
] = 64,
97 struct bt_trace
*bt_trace_create(void)
99 struct bt_trace
*trace
= NULL
;
101 trace
= g_new0(struct bt_trace
, 1);
103 BT_LOGE_STR("Failed to allocate one trace.");
107 BT_LOGD_STR("Creating trace object.");
108 trace
->native_byte_order
= BT_BYTE_ORDER_UNSPECIFIED
;
109 bt_object_init(trace
, bt_trace_destroy
);
110 trace
->clocks
= g_ptr_array_new_with_free_func(
111 (GDestroyNotify
) bt_put
);
112 if (!trace
->clocks
) {
113 BT_LOGE_STR("Failed to allocate one GPtrArray.");
117 trace
->streams
= g_ptr_array_new_with_free_func(
118 (GDestroyNotify
) bt_object_release
);
119 if (!trace
->streams
) {
120 BT_LOGE_STR("Failed to allocate one GPtrArray.");
124 trace
->stream_classes
= g_ptr_array_new_with_free_func(
125 (GDestroyNotify
) bt_object_release
);
126 if (!trace
->stream_classes
) {
127 BT_LOGE_STR("Failed to allocate one GPtrArray.");
131 /* Create the environment array object */
132 trace
->environment
= bt_attributes_create();
133 if (!trace
->environment
) {
134 BT_LOGE_STR("Cannot create empty attributes object.");
138 trace
->listeners
= g_ptr_array_new_with_free_func(
139 (GDestroyNotify
) g_free
);
140 if (!trace
->listeners
) {
141 BT_LOGE_STR("Failed to allocate one GPtrArray.");
145 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
146 sizeof(struct bt_trace_is_static_listener_elem
));
147 if (!trace
->is_static_listeners
) {
148 BT_LOGE_STR("Failed to allocate one GArray.");
152 BT_LOGD("Created trace object: addr=%p", trace
);
160 const char *bt_trace_get_name(struct bt_trace
*trace
)
162 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
163 return trace
->name
? trace
->name
->str
: NULL
;
166 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
171 BT_LOGW_STR("Invalid parameter: trace is NULL.");
177 BT_LOGW_STR("Invalid parameter: name is NULL.");
183 BT_LOGW("Invalid parameter: trace is frozen: "
184 "addr=%p, name=\"%s\"",
185 trace
, bt_trace_get_name(trace
));
190 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
193 BT_LOGE_STR("Failed to allocate one GString.");
198 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
204 const unsigned char *bt_trace_get_uuid(struct bt_trace
*trace
)
206 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
207 return trace
->uuid_set
? trace
->uuid
: NULL
;
210 int bt_trace_set_uuid(struct bt_trace
*trace
, const unsigned char *uuid
)
215 BT_LOGW_STR("Invalid parameter: trace is NULL.");
221 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
227 BT_LOGW("Invalid parameter: trace is frozen: "
228 "addr=%p, name=\"%s\"",
229 trace
, bt_trace_get_name(trace
));
234 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
235 trace
->uuid_set
= BT_TRUE
;
236 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
237 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
238 trace
, bt_trace_get_name(trace
),
239 (unsigned int) uuid
[0],
240 (unsigned int) uuid
[1],
241 (unsigned int) uuid
[2],
242 (unsigned int) uuid
[3],
243 (unsigned int) uuid
[4],
244 (unsigned int) uuid
[5],
245 (unsigned int) uuid
[6],
246 (unsigned int) uuid
[7],
247 (unsigned int) uuid
[8],
248 (unsigned int) uuid
[9],
249 (unsigned int) uuid
[10],
250 (unsigned int) uuid
[11],
251 (unsigned int) uuid
[12],
252 (unsigned int) uuid
[13],
253 (unsigned int) uuid
[14],
254 (unsigned int) uuid
[15]);
260 void bt_trace_destroy(struct bt_object
*obj
)
262 struct bt_trace
*trace
;
264 trace
= container_of(obj
, struct bt_trace
, base
);
266 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
267 trace
, bt_trace_get_name(trace
));
270 * Call remove listeners first so that everything else still
271 * exists in the trace.
273 if (trace
->is_static_listeners
) {
276 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
277 struct bt_trace_is_static_listener_elem elem
=
278 g_array_index(trace
->is_static_listeners
,
279 struct bt_trace_is_static_listener_elem
, i
);
282 elem
.removed(trace
, elem
.data
);
286 g_array_free(trace
->is_static_listeners
, TRUE
);
289 if (trace
->listeners
) {
290 g_ptr_array_free(trace
->listeners
, TRUE
);
293 if (trace
->environment
) {
294 BT_LOGD_STR("Destroying environment attributes.");
295 bt_attributes_destroy(trace
->environment
);
299 g_string_free(trace
->name
, TRUE
);
303 BT_LOGD_STR("Putting clock classes.");
304 g_ptr_array_free(trace
->clocks
, TRUE
);
307 if (trace
->streams
) {
308 BT_LOGD_STR("Destroying streams.");
309 g_ptr_array_free(trace
->streams
, TRUE
);
312 if (trace
->stream_classes
) {
313 BT_LOGD_STR("Destroying stream classes.");
314 g_ptr_array_free(trace
->stream_classes
, TRUE
);
317 BT_LOGD_STR("Putting packet header field type.");
318 bt_put(trace
->packet_header_type
);
322 int bt_trace_set_environment_field(struct bt_trace
*trace
,
323 const char *name
, struct bt_value
*value
)
328 BT_LOGW_STR("Invalid parameter: trace is NULL.");
334 BT_LOGW_STR("Invalid parameter: name is NULL.");
340 BT_LOGW_STR("Invalid parameter: value is NULL.");
345 if (!bt_identifier_is_valid(name
)) {
346 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
347 "trace-addr=%p, trace-name=\"%s\", "
349 trace
, bt_trace_get_name(trace
), name
);
354 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
355 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
356 "trace-addr=%p, trace-name=\"%s\", "
357 "env-name=\"%s\", env-value-type=%s",
358 trace
, bt_trace_get_name(trace
), name
,
359 bt_value_type_string(bt_value_get_type(value
)));
364 if (trace
->is_static
) {
365 BT_LOGW("Invalid parameter: trace is static: "
366 "addr=%p, name=\"%s\"",
367 trace
, bt_trace_get_name(trace
));
374 * New environment fields may be added to a frozen trace,
375 * but existing fields may not be changed.
377 * The object passed is frozen like all other attributes.
379 struct bt_value
*attribute
=
380 bt_attributes_get_field_value_by_name(
381 trace
->environment
, name
);
384 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
385 "trace-addr=%p, trace-name=\"%s\", "
387 trace
, bt_trace_get_name(trace
), name
);
393 bt_value_freeze(value
);
396 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
399 BT_LOGE("Cannot set environment field's value: "
400 "trace-addr=%p, trace-name=\"%s\", "
402 trace
, bt_trace_get_name(trace
), name
);
404 BT_LOGV("Set environment field's value: "
405 "trace-addr=%p, trace-name=\"%s\", "
406 "env-name=\"%s\", value-addr=%p",
407 trace
, bt_trace_get_name(trace
), name
, value
);
414 int bt_trace_set_environment_field_string(struct bt_trace
*trace
,
415 const char *name
, const char *value
)
418 struct bt_value
*env_value_string_obj
= NULL
;
421 BT_LOGW_STR("Invalid parameter: value is NULL.");
426 env_value_string_obj
= bt_value_string_create_init(value
);
427 if (!env_value_string_obj
) {
428 BT_LOGE_STR("Cannot create string value object.");
433 /* bt_trace_set_environment_field() logs errors */
434 ret
= bt_trace_set_environment_field(trace
, name
,
435 env_value_string_obj
);
438 bt_put(env_value_string_obj
);
442 int bt_trace_set_environment_field_integer(struct bt_trace
*trace
,
443 const char *name
, int64_t value
)
446 struct bt_value
*env_value_integer_obj
= NULL
;
448 env_value_integer_obj
= bt_value_integer_create_init(value
);
449 if (!env_value_integer_obj
) {
450 BT_LOGE_STR("Cannot create integer value object.");
455 /* bt_trace_set_environment_field() logs errors */
456 ret
= bt_trace_set_environment_field(trace
, name
,
457 env_value_integer_obj
);
460 bt_put(env_value_integer_obj
);
464 int64_t bt_trace_get_environment_field_count(struct bt_trace
*trace
)
468 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
469 ret
= bt_attributes_get_count(trace
->environment
);
475 bt_trace_get_environment_field_name_by_index(struct bt_trace
*trace
,
478 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
479 return bt_attributes_get_field_name(trace
->environment
, index
);
482 struct bt_value
*bt_trace_get_environment_field_value_by_index(
483 struct bt_trace
*trace
, uint64_t index
)
485 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
486 return bt_attributes_get_field_value(trace
->environment
, index
);
489 struct bt_value
*bt_trace_get_environment_field_value_by_name(
490 struct bt_trace
*trace
, const char *name
)
492 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
493 BT_ASSERT_PRE_NON_NULL(name
, "Name");
494 return bt_attributes_get_field_value_by_name(trace
->environment
,
498 int bt_trace_add_clock_class(struct bt_trace
*trace
,
499 struct bt_clock_class
*clock_class
)
504 BT_LOGW_STR("Invalid parameter: trace is NULL.");
509 if (trace
->is_static
) {
510 BT_LOGW("Invalid parameter: trace is static: "
511 "addr=%p, name=\"%s\"",
512 trace
, bt_trace_get_name(trace
));
517 if (!bt_clock_class_is_valid(clock_class
)) {
518 BT_LOGW("Invalid parameter: clock class is invalid: "
519 "trace-addr=%p, trace-name=\"%s\", "
520 "clock-class-addr=%p, clock-class-name=\"%s\"",
521 trace
, bt_trace_get_name(trace
),
522 clock_class
, bt_clock_class_get_name(clock_class
));
527 /* Check for duplicate clock classes */
528 if (bt_trace_has_clock_class(trace
, clock_class
)) {
529 BT_LOGW("Invalid parameter: clock class already exists in trace: "
530 "trace-addr=%p, trace-name=\"%s\", "
531 "clock-class-addr=%p, clock-class-name=\"%s\"",
532 trace
, bt_trace_get_name(trace
),
533 clock_class
, bt_clock_class_get_name(clock_class
));
539 g_ptr_array_add(trace
->clocks
, clock_class
);
542 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
543 bt_clock_class_freeze(clock_class
);
546 BT_LOGV("Added clock class to trace: "
547 "trace-addr=%p, trace-name=\"%s\", "
548 "clock-class-addr=%p, clock-class-name=\"%s\"",
549 trace
, bt_trace_get_name(trace
),
550 clock_class
, bt_clock_class_get_name(clock_class
));
556 int64_t bt_trace_get_clock_class_count(struct bt_trace
*trace
)
558 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
559 return trace
->clocks
->len
;
562 struct bt_clock_class
*bt_trace_get_clock_class_by_index(
563 struct bt_trace
*trace
, uint64_t index
)
565 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
566 BT_ASSERT_PRE(index
< trace
->clocks
->len
,
567 "Index is out of bounds: index=%" PRIu64
", "
569 index
, trace
->clocks
->len
);
570 return bt_get(g_ptr_array_index(trace
->clocks
, index
));
574 bool packet_header_field_type_is_valid(struct bt_trace
*trace
,
575 struct bt_field_type
*packet_header_type
)
578 bool is_valid
= true;
579 struct bt_field_type
*field_type
= NULL
;
581 if (!packet_header_type
) {
583 * No packet header field type: trace must have only
584 * one stream. At this point the stream class being
585 * added is not part of the trace yet, so we validate
586 * that the trace contains no stream classes yet.
588 if (trace
->stream_classes
->len
>= 1) {
589 BT_LOGW_STR("Invalid packet header field type: "
590 "packet header field type does not exist but there's more than one stream class in the trace.");
594 /* No packet header field type: valid at this point */
598 /* Packet header field type, if it exists, must be a structure */
599 if (!bt_field_type_is_structure(packet_header_type
)) {
600 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
601 "ft-addr=%p, ft-id=%s",
603 bt_field_type_id_string(packet_header_type
->id
));
608 * If there's a `magic` field, it must be a 32-bit unsigned
609 * integer field type. Also it must be the first field of the
610 * packet header field type.
612 field_type
= bt_field_type_structure_get_field_type_by_name(
613 packet_header_type
, "magic");
615 const char *field_name
;
617 if (!bt_field_type_is_integer(field_type
)) {
618 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
619 "magic-ft-addr=%p, magic-ft-id=%s",
621 bt_field_type_id_string(field_type
->id
));
625 if (bt_field_type_integer_is_signed(field_type
)) {
626 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
627 "magic-ft-addr=%p", field_type
);
631 if (bt_field_type_integer_get_size(field_type
) != 32) {
632 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
633 "magic-ft-addr=%p, magic-ft-size=%u",
635 bt_field_type_integer_get_size(field_type
));
639 ret
= bt_field_type_structure_get_field_by_index(
640 packet_header_type
, &field_name
, NULL
, 0);
643 if (strcmp(field_name
, "magic") != 0) {
644 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
645 "magic-ft-addr=%p, first-field-name=\"%s\"",
646 field_type
, field_name
);
654 * If there's a `uuid` field, it must be an array field type of
655 * length 16 with an 8-bit unsigned integer element field type.
657 field_type
= bt_field_type_structure_get_field_type_by_name(
658 packet_header_type
, "uuid");
660 struct bt_field_type
*elem_ft
;
662 if (!bt_field_type_is_array(field_type
)) {
663 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
664 "uuid-ft-addr=%p, uuid-ft-id=%s",
666 bt_field_type_id_string(field_type
->id
));
670 if (bt_field_type_array_get_length(field_type
) != 16) {
671 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
672 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
674 bt_field_type_array_get_length(field_type
));
678 elem_ft
= bt_field_type_array_get_element_type(field_type
);
681 if (!bt_field_type_is_integer(elem_ft
)) {
682 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
683 "elem-ft-addr=%p, elem-ft-id=%s",
685 bt_field_type_id_string(elem_ft
->id
));
690 if (bt_field_type_integer_is_signed(elem_ft
)) {
691 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
692 "elem-ft-addr=%p", elem_ft
);
697 if (bt_field_type_integer_get_size(elem_ft
) != 8) {
698 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
699 "elem-ft-addr=%p, elem-ft-size=%u",
701 bt_field_type_integer_get_size(elem_ft
));
711 * The `stream_id` field must exist if there's more than one
712 * stream classes in the trace.
714 field_type
= bt_field_type_structure_get_field_type_by_name(
715 packet_header_type
, "stream_id");
717 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
718 BT_LOGW_STR("Invalid packet header field type: "
719 "`stream_id` field does not exist but there's more than one stream class in the trace.");
724 * If there's a `stream_id` field, it must be an unsigned
725 * integer field type.
728 if (!bt_field_type_is_integer(field_type
)) {
729 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
730 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
732 bt_field_type_id_string(field_type
->id
));
736 if (bt_field_type_integer_is_signed(field_type
)) {
737 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
738 "stream-id-ft-addr=%p", field_type
);
746 * If there's a `packet_seq_num` field, it must be an unsigned
747 * integer field type.
749 field_type
= bt_field_type_structure_get_field_type_by_name(
750 packet_header_type
, "packet_seq_num");
752 if (!bt_field_type_is_integer(field_type
)) {
753 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
754 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
756 bt_field_type_id_string(field_type
->id
));
760 if (bt_field_type_integer_is_signed(field_type
)) {
761 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
762 "packet-seq-num-ft-addr=%p", field_type
);
780 bool packet_context_field_type_is_valid(struct bt_trace
*trace
,
781 struct bt_stream_class
*stream_class
,
782 struct bt_field_type
*packet_context_type
)
784 bool is_valid
= true;
785 struct bt_field_type
*field_type
= NULL
;
787 if (!packet_context_type
) {
788 /* No packet context field type: valid at this point */
792 /* Packet context field type, if it exists, must be a structure */
793 if (!bt_field_type_is_structure(packet_context_type
)) {
794 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
795 "ft-addr=%p, ft-id=%s",
797 bt_field_type_id_string(packet_context_type
->id
));
802 * If there's a `packet_size` field, it must be an unsigned
803 * integer field type.
805 field_type
= bt_field_type_structure_get_field_type_by_name(
806 packet_context_type
, "packet_size");
808 if (!bt_field_type_is_integer(field_type
)) {
809 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
810 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
812 bt_field_type_id_string(field_type
->id
));
816 if (bt_field_type_integer_is_signed(field_type
)) {
817 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
818 "packet-size-ft-addr=%p", field_type
);
826 * If there's a `content_size` field, it must be an unsigned
827 * integer field type.
829 field_type
= bt_field_type_structure_get_field_type_by_name(
830 packet_context_type
, "content_size");
832 if (!bt_field_type_is_integer(field_type
)) {
833 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
834 "content-size-ft-addr=%p, content-size-ft-id=%s",
836 bt_field_type_id_string(field_type
->id
));
840 if (bt_field_type_integer_is_signed(field_type
)) {
841 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
842 "content-size-ft-addr=%p", field_type
);
850 * If there's a `events_discarded` field, it must be an unsigned
851 * integer field type.
853 field_type
= bt_field_type_structure_get_field_type_by_name(
854 packet_context_type
, "events_discarded");
856 if (!bt_field_type_is_integer(field_type
)) {
857 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
858 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
860 bt_field_type_id_string(field_type
->id
));
864 if (bt_field_type_integer_is_signed(field_type
)) {
865 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
866 "events-discarded-ft-addr=%p", field_type
);
874 * If there's a `timestamp_begin` field, it must be an unsigned
875 * integer field type. Also, if the trace is not a CTF writer's
876 * trace, then we cannot automatically set the mapped clock
877 * class of this field, so it must have a mapped clock class.
879 field_type
= bt_field_type_structure_get_field_type_by_name(
880 packet_context_type
, "timestamp_begin");
882 if (!bt_field_type_is_integer(field_type
)) {
883 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
884 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
886 bt_field_type_id_string(field_type
->id
));
890 if (bt_field_type_integer_is_signed(field_type
)) {
891 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
892 "timestamp-begin-ft-addr=%p", field_type
);
896 if (!trace
->is_created_by_writer
) {
897 struct bt_clock_class
*clock_class
=
898 bt_field_type_integer_get_mapped_clock_class(
903 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
904 "timestamp-begin-ft-addr=%p", field_type
);
913 * If there's a `timestamp_end` field, it must be an unsigned
914 * integer field type. Also, if the trace is not a CTF writer's
915 * trace, then we cannot automatically set the mapped clock
916 * class of this field, so it must have a mapped clock class.
918 field_type
= bt_field_type_structure_get_field_type_by_name(
919 packet_context_type
, "timestamp_end");
921 if (!bt_field_type_is_integer(field_type
)) {
922 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
923 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
925 bt_field_type_id_string(field_type
->id
));
929 if (bt_field_type_integer_is_signed(field_type
)) {
930 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
931 "timestamp-end-ft-addr=%p", field_type
);
935 if (!trace
->is_created_by_writer
) {
936 struct bt_clock_class
*clock_class
=
937 bt_field_type_integer_get_mapped_clock_class(
942 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
943 "timestamp-end-ft-addr=%p", field_type
);
962 bool event_header_field_type_is_valid(struct bt_trace
*trace
,
963 struct bt_stream_class
*stream_class
,
964 struct bt_field_type
*event_header_type
)
966 bool is_valid
= true;
967 struct bt_field_type
*field_type
= NULL
;
970 * We do not validate that the `timestamp` field exists here
971 * because CTF does not require this exact name to be mapped to
975 if (!event_header_type
) {
977 * No event header field type: stream class must have
978 * only one event class.
980 if (bt_stream_class_get_event_class_count(stream_class
) > 1) {
981 BT_LOGW_STR("Invalid event header field type: "
982 "event header field type does not exist but there's more than one event class in the stream class.");
986 /* No event header field type: valid at this point */
990 /* Event header field type, if it exists, must be a structure */
991 if (!bt_field_type_is_structure(event_header_type
)) {
992 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
993 "ft-addr=%p, ft-id=%s",
995 bt_field_type_id_string(event_header_type
->id
));
1000 * If there's an `id` field, it must be an unsigned integer
1001 * field type or an enumeration field type with an unsigned
1002 * integer container field type.
1004 field_type
= bt_field_type_structure_get_field_type_by_name(
1005 event_header_type
, "id");
1007 struct bt_field_type
*int_ft
;
1009 if (bt_field_type_is_integer(field_type
)) {
1010 int_ft
= bt_get(field_type
);
1011 } else if (bt_field_type_is_enumeration(field_type
)) {
1012 int_ft
= bt_field_type_enumeration_get_container_type(
1015 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1016 "id-ft-addr=%p, id-ft-id=%s",
1018 bt_field_type_id_string(field_type
->id
));
1023 if (bt_field_type_integer_is_signed(int_ft
)) {
1024 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1025 "id-ft-addr=%p", int_ft
);
1044 int check_packet_header_type_has_no_clock_class(struct bt_trace
*trace
)
1048 if (trace
->packet_header_type
) {
1049 struct bt_clock_class
*clock_class
= NULL
;
1051 ret
= bt_validate_single_clock_class(trace
->packet_header_type
,
1053 bt_put(clock_class
);
1054 if (ret
|| clock_class
) {
1055 BT_LOGW("Trace's packet header field type cannot "
1056 "contain a field type which is mapped to "
1058 "trace-addr=%p, trace-name=\"%s\", "
1059 "clock-class-name=\"%s\"",
1060 trace
, bt_trace_get_name(trace
),
1062 bt_clock_class_get_name(clock_class
) :
1071 int bt_trace_add_stream_class(struct bt_trace
*trace
,
1072 struct bt_stream_class
*stream_class
)
1077 struct bt_validation_output trace_sc_validation_output
= { 0 };
1078 struct bt_validation_output
*ec_validation_outputs
= NULL
;
1079 const enum bt_validation_flag trace_sc_validation_flags
=
1080 BT_VALIDATION_FLAG_TRACE
|
1081 BT_VALIDATION_FLAG_STREAM
;
1082 const enum bt_validation_flag ec_validation_flags
=
1083 BT_VALIDATION_FLAG_EVENT
;
1084 struct bt_field_type
*packet_header_type
= NULL
;
1085 struct bt_field_type
*packet_context_type
= NULL
;
1086 struct bt_field_type
*event_header_type
= NULL
;
1087 struct bt_field_type
*stream_event_ctx_type
= NULL
;
1088 int64_t event_class_count
;
1089 struct bt_trace
*current_parent_trace
= NULL
;
1090 struct bt_clock_class
*expected_clock_class
= NULL
;
1093 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1098 if (!stream_class
) {
1099 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1104 if (trace
->is_static
) {
1105 BT_LOGW_STR("Invalid parameter: trace is static.");
1110 BT_LOGD("Adding stream class to trace: "
1111 "trace-addr=%p, trace-name=\"%s\", "
1112 "stream-class-addr=%p, stream-class-name=\"%s\", "
1113 "stream-class-id=%" PRId64
,
1114 trace
, bt_trace_get_name(trace
),
1115 stream_class
, bt_stream_class_get_name(stream_class
),
1116 bt_stream_class_get_id(stream_class
));
1118 current_parent_trace
= bt_stream_class_get_trace(stream_class
);
1119 if (current_parent_trace
) {
1120 /* Stream class is already associated to a trace, abort. */
1121 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1122 "stream-class-trace-addr=%p, "
1123 "stream-class-trace-name=\"%s\"",
1124 current_parent_trace
,
1125 bt_trace_get_name(current_parent_trace
));
1131 bt_stream_class_get_event_class_count(stream_class
);
1132 BT_ASSERT(event_class_count
>= 0);
1134 if (stream_class
->clock
) {
1135 struct bt_clock_class
*stream_clock_class
=
1136 stream_class
->clock
->clock_class
;
1138 if (trace
->is_created_by_writer
) {
1140 * Make sure this clock was also added to the
1141 * trace (potentially through its CTF writer
1146 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1147 if (trace
->clocks
->pdata
[i
] ==
1148 stream_clock_class
) {
1154 if (i
== trace
->clocks
->len
) {
1156 BT_LOGW("Stream class's clock's class is not part of the trace: "
1157 "clock-class-addr=%p, clock-class-name=\"%s\"",
1159 bt_clock_class_get_name(stream_clock_class
));
1165 * This trace was NOT created by a CTF writer,
1166 * thus do not allow the stream class to add to
1167 * have a clock at all. Those are two
1168 * independent APIs (non-writer and writer
1169 * APIs), and isolating them simplifies things.
1171 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1172 "clock-class-addr=%p, clock-class-name=\"%s\"",
1174 bt_clock_class_get_name(stream_clock_class
));
1179 if (stream_class
->clock_class
&&
1180 stream_class
->clock_class
!=
1181 stream_class
->clock
->clock_class
) {
1183 * Stream class already has an expected clock
1184 * class, but it does not match its clock's
1187 BT_LOGW("Invalid parameter: stream class's clock's "
1188 "class does not match stream class's "
1189 "expected clock class: "
1190 "stream-class-addr=%p, "
1191 "stream-class-id=%" PRId64
", "
1192 "stream-class-name=\"%s\", "
1193 "expected-clock-class-addr=%p, "
1194 "expected-clock-class-name=\"%s\"",
1196 bt_stream_class_get_id(stream_class
),
1197 bt_stream_class_get_name(stream_class
),
1198 expected_clock_class
,
1199 bt_clock_class_get_name(expected_clock_class
));
1200 } else if (!stream_class
->clock_class
) {
1202 * Set expected clock class to stream class's
1205 expected_clock_class
=
1206 bt_get(stream_class
->clock
->clock_class
);
1210 if (!stream_class
->frozen
) {
1212 * Stream class is not frozen yet. Validate that the
1213 * stream class contains at most a single clock class
1214 * because the previous
1215 * bt_stream_class_add_event_class() calls did not make
1216 * this validation since the stream class's direct field
1217 * types (packet context, event header, event context)
1218 * could change afterwards. This stream class is about
1219 * to be frozen and those field types won't be changed
1220 * if this function succeeds.
1222 * At this point we're also sure that the stream class's
1223 * clock, if any, has the same class as the stream
1224 * class's expected clock class, if any. This is why, if
1225 * bt_stream_class_validate_single_clock_class()
1226 * succeeds below, the call to
1227 * bt_stream_class_map_clock_class() at the end of this
1228 * function is safe because it maps to the same, single
1231 ret
= bt_stream_class_validate_single_clock_class(stream_class
,
1232 &expected_clock_class
);
1234 BT_LOGW("Invalid parameter: stream class or one of its "
1235 "event classes contains a field type which is "
1236 "not recursively mapped to the expected "
1238 "stream-class-addr=%p, "
1239 "stream-class-id=%" PRId64
", "
1240 "stream-class-name=\"%s\", "
1241 "expected-clock-class-addr=%p, "
1242 "expected-clock-class-name=\"%s\"",
1243 stream_class
, bt_stream_class_get_id(stream_class
),
1244 bt_stream_class_get_name(stream_class
),
1245 expected_clock_class
,
1246 expected_clock_class
?
1247 bt_clock_class_get_name(expected_clock_class
) :
1253 ret
= check_packet_header_type_has_no_clock_class(trace
);
1255 /* check_packet_header_type_has_no_clock_class() logs errors */
1260 * We're about to freeze both the trace and the stream class.
1261 * Also, each event class contained in this stream class are
1264 * This trace, this stream class, and all its event classes
1265 * should be valid at this point.
1267 * Validate trace and stream class first, then each event
1268 * class of this stream class can be validated individually.
1270 packet_header_type
=
1271 bt_trace_get_packet_header_type(trace
);
1272 packet_context_type
=
1273 bt_stream_class_get_packet_context_type(stream_class
);
1275 bt_stream_class_get_event_header_type(stream_class
);
1276 stream_event_ctx_type
=
1277 bt_stream_class_get_event_context_type(stream_class
);
1279 BT_LOGD("Validating trace and stream class field types.");
1280 ret
= bt_validate_class_types(trace
->environment
,
1281 packet_header_type
, packet_context_type
, event_header_type
,
1282 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1283 stream_class
->valid
, 1, &trace_sc_validation_output
,
1284 trace_sc_validation_flags
);
1285 BT_PUT(packet_header_type
);
1286 BT_PUT(packet_context_type
);
1287 BT_PUT(event_header_type
);
1288 BT_PUT(stream_event_ctx_type
);
1292 * This means something went wrong during the validation
1293 * process, not that the objects are invalid.
1295 BT_LOGE("Failed to validate trace and stream class field types: "
1300 if ((trace_sc_validation_output
.valid_flags
&
1301 trace_sc_validation_flags
) !=
1302 trace_sc_validation_flags
) {
1303 /* Invalid trace/stream class */
1304 BT_LOGW("Invalid trace or stream class field types: "
1306 trace_sc_validation_output
.valid_flags
);
1311 if (event_class_count
> 0) {
1312 ec_validation_outputs
= g_new0(struct bt_validation_output
,
1314 if (!ec_validation_outputs
) {
1315 BT_LOGE_STR("Failed to allocate one validation output structure.");
1321 /* Validate each event class individually */
1322 for (i
= 0; i
< event_class_count
; i
++) {
1323 struct bt_event_class
*event_class
=
1324 bt_stream_class_get_event_class_by_index(
1326 struct bt_field_type
*event_context_type
= NULL
;
1327 struct bt_field_type
*event_payload_type
= NULL
;
1329 event_context_type
=
1330 bt_event_class_get_context_type(event_class
);
1331 event_payload_type
=
1332 bt_event_class_get_payload_type(event_class
);
1335 * It is important to use the field types returned by
1336 * the previous trace and stream class validation here
1337 * because copies could have been made.
1339 BT_LOGD("Validating event class's field types: "
1340 "addr=%p, name=\"%s\", id=%" PRId64
,
1341 event_class
, bt_event_class_get_name(event_class
),
1342 bt_event_class_get_id(event_class
));
1343 ret
= bt_validate_class_types(trace
->environment
,
1344 trace_sc_validation_output
.packet_header_type
,
1345 trace_sc_validation_output
.packet_context_type
,
1346 trace_sc_validation_output
.event_header_type
,
1347 trace_sc_validation_output
.stream_event_ctx_type
,
1348 event_context_type
, event_payload_type
,
1349 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1350 ec_validation_flags
);
1351 BT_PUT(event_context_type
);
1352 BT_PUT(event_payload_type
);
1353 BT_PUT(event_class
);
1356 BT_LOGE("Failed to validate event class field types: "
1361 if ((ec_validation_outputs
[i
].valid_flags
&
1362 ec_validation_flags
) != ec_validation_flags
) {
1363 /* Invalid event class */
1364 BT_LOGW("Invalid event class field types: "
1366 ec_validation_outputs
[i
].valid_flags
);
1372 stream_id
= bt_stream_class_get_id(stream_class
);
1373 if (stream_id
< 0) {
1374 stream_id
= trace
->next_stream_id
++;
1375 if (stream_id
< 0) {
1376 BT_LOGE_STR("No more stream class IDs available.");
1381 /* Try to assign a new stream id */
1382 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1383 if (stream_id
== bt_stream_class_get_id(
1384 trace
->stream_classes
->pdata
[i
])) {
1385 /* Duplicate stream id found */
1386 BT_LOGW("Duplicate stream class ID: "
1387 "id=%" PRId64
, (int64_t) stream_id
);
1393 if (bt_stream_class_set_id_no_check(stream_class
,
1395 /* TODO Should retry with a different stream id */
1396 BT_LOGE("Cannot set stream class's ID: "
1397 "id=%" PRId64
, (int64_t) stream_id
);
1404 * At this point all the field types in the validation output
1405 * are valid. Validate the semantics of some scopes according to
1406 * the CTF specification.
1408 if (!packet_header_field_type_is_valid(trace
,
1409 trace_sc_validation_output
.packet_header_type
)) {
1410 BT_LOGW_STR("Invalid trace's packet header field type.");
1415 if (!packet_context_field_type_is_valid(trace
,
1417 trace_sc_validation_output
.packet_context_type
)) {
1418 BT_LOGW_STR("Invalid stream class's packet context field type.");
1423 if (!event_header_field_type_is_valid(trace
,
1425 trace_sc_validation_output
.event_header_type
)) {
1426 BT_LOGW_STR("Invalid steam class's event header field type.");
1432 * Now is the time to automatically map specific field types of
1433 * the stream class's packet context and event header field
1434 * types to the stream class's clock's class if they are not
1435 * mapped to a clock class yet. We do it here because we know
1436 * that after this point, everything is frozen so it won't be
1437 * possible for the user to modify the stream class's clock, or
1438 * to map those field types to other clock classes.
1440 if (trace
->is_created_by_writer
) {
1441 if (bt_stream_class_map_clock_class(stream_class
,
1442 trace_sc_validation_output
.packet_context_type
,
1443 trace_sc_validation_output
.event_header_type
)) {
1444 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1450 bt_object_set_parent(stream_class
, trace
);
1451 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1454 * At this point we know that the function will be successful.
1455 * Therefore we can replace the trace and stream class field
1456 * types with what's in their validation output structure and
1457 * mark them as valid. We can also replace the field types of
1458 * all the event classes of the stream class and mark them as
1461 bt_validation_replace_types(trace
, stream_class
, NULL
,
1462 &trace_sc_validation_output
, trace_sc_validation_flags
);
1464 stream_class
->valid
= 1;
1467 * Put what was not moved in bt_validation_replace_types().
1469 bt_validation_output_put_types(&trace_sc_validation_output
);
1471 for (i
= 0; i
< event_class_count
; i
++) {
1472 struct bt_event_class
*event_class
=
1473 bt_stream_class_get_event_class_by_index(
1476 bt_validation_replace_types(NULL
, NULL
, event_class
,
1477 &ec_validation_outputs
[i
], ec_validation_flags
);
1478 event_class
->valid
= 1;
1479 BT_PUT(event_class
);
1482 * Put what was not moved in
1483 * bt_validation_replace_types().
1485 bt_validation_output_put_types(&ec_validation_outputs
[i
]);
1489 * Freeze the trace and the stream class.
1491 bt_stream_class_freeze(stream_class
);
1492 bt_trace_freeze(trace
);
1495 * It is safe to set the stream class's unique clock class
1496 * now because the stream class is frozen.
1498 if (expected_clock_class
) {
1499 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
1502 /* Notifiy listeners of the trace's schema modification. */
1503 bt_stream_class_visit(stream_class
,
1504 bt_trace_object_modification
, trace
);
1505 BT_LOGD("Added stream class to trace: "
1506 "trace-addr=%p, trace-name=\"%s\", "
1507 "stream-class-addr=%p, stream-class-name=\"%s\", "
1508 "stream-class-id=%" PRId64
,
1509 trace
, bt_trace_get_name(trace
),
1510 stream_class
, bt_stream_class_get_name(stream_class
),
1511 bt_stream_class_get_id(stream_class
));
1515 bt_object_set_parent(stream_class
, NULL
);
1517 if (ec_validation_outputs
) {
1518 for (i
= 0; i
< event_class_count
; i
++) {
1519 bt_validation_output_put_types(
1520 &ec_validation_outputs
[i
]);
1525 g_free(ec_validation_outputs
);
1526 bt_validation_output_put_types(&trace_sc_validation_output
);
1527 bt_put(current_parent_trace
);
1528 bt_put(expected_clock_class
);
1529 BT_ASSERT(!packet_header_type
);
1530 BT_ASSERT(!packet_context_type
);
1531 BT_ASSERT(!event_header_type
);
1532 BT_ASSERT(!stream_event_ctx_type
);
1536 int64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
1538 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1539 return (int64_t) trace
->streams
->len
;
1542 struct bt_stream
*bt_trace_get_stream_by_index(
1543 struct bt_trace
*trace
,
1546 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1547 BT_ASSERT_PRE(index
< trace
->streams
->len
,
1548 "Index is out of bounds: index=%" PRIu64
", "
1550 index
, trace
->streams
->len
);
1551 return bt_get(g_ptr_array_index(trace
->streams
, index
));
1554 int64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
1556 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1557 return (int64_t) trace
->stream_classes
->len
;
1560 struct bt_stream_class
*bt_trace_get_stream_class_by_index(
1561 struct bt_trace
*trace
, uint64_t index
)
1563 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1564 BT_ASSERT_PRE(index
< trace
->stream_classes
->len
,
1565 "Index is out of bounds: index=%" PRIu64
", "
1567 index
, trace
->stream_classes
->len
);
1568 return bt_get(g_ptr_array_index(trace
->stream_classes
, index
));
1571 struct bt_stream_class
*bt_trace_get_stream_class_by_id(
1572 struct bt_trace
*trace
, uint64_t id_param
)
1575 struct bt_stream_class
*stream_class
= NULL
;
1576 int64_t id
= (int64_t) id_param
;
1578 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1579 BT_ASSERT_PRE(id
>= 0,
1580 "Invalid stream class ID: %" PRIu64
, id_param
);
1582 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1583 struct bt_stream_class
*stream_class_candidate
;
1585 stream_class_candidate
=
1586 g_ptr_array_index(trace
->stream_classes
, i
);
1588 if (bt_stream_class_get_id(stream_class_candidate
) ==
1590 stream_class
= stream_class_candidate
;
1591 bt_get(stream_class
);
1597 return stream_class
;
1600 struct bt_clock_class
*bt_trace_get_clock_class_by_name(
1601 struct bt_trace
*trace
, const char *name
)
1604 struct bt_clock_class
*clock_class
= NULL
;
1606 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1607 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1609 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
1610 struct bt_clock_class
*cur_clk
=
1611 g_ptr_array_index(trace
->clocks
, i
);
1612 const char *cur_clk_name
= bt_clock_class_get_name(cur_clk
);
1614 if (!cur_clk_name
) {
1618 if (!strcmp(cur_clk_name
, name
)) {
1619 clock_class
= cur_clk
;
1620 bt_get(clock_class
);
1630 bt_bool
bt_trace_has_clock_class(struct bt_trace
*trace
,
1631 struct bt_clock_class
*clock_class
)
1633 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1636 BT_ASSERT(clock_class
);
1638 g_ptr_array_foreach(trace
->clocks
, value_exists
, &query
);
1643 const char *get_byte_order_string(enum bt_byte_order byte_order
)
1647 switch (byte_order
) {
1648 case BT_BYTE_ORDER_LITTLE_ENDIAN
:
1651 case BT_BYTE_ORDER_BIG_ENDIAN
:
1654 case BT_BYTE_ORDER_NATIVE
:
1665 int append_trace_metadata(struct bt_trace
*trace
,
1666 struct metadata_context
*context
)
1668 unsigned char *uuid
= trace
->uuid
;
1671 if (trace
->native_byte_order
== BT_BYTE_ORDER_NATIVE
||
1672 trace
->native_byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
1673 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_BYTE_ORDER_NATIVE or BT_BYTE_ORDER_UNSPECIFIED at this point; "
1674 "set it with bt_trace_set_native_byte_order(): "
1675 "addr=%p, name=\"%s\"",
1676 trace
, bt_trace_get_name(trace
));
1681 g_string_append(context
->string
, "trace {\n");
1682 g_string_append(context
->string
, "\tmajor = 1;\n");
1683 g_string_append(context
->string
, "\tminor = 8;\n");
1684 BT_ASSERT(trace
->native_byte_order
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
1685 trace
->native_byte_order
== BT_BYTE_ORDER_BIG_ENDIAN
||
1686 trace
->native_byte_order
== BT_BYTE_ORDER_NETWORK
);
1688 if (trace
->uuid_set
) {
1689 g_string_append_printf(context
->string
,
1690 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1691 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1692 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1693 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1694 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1697 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
1698 get_byte_order_string(trace
->native_byte_order
));
1700 if (trace
->packet_header_type
) {
1701 g_string_append(context
->string
, "\tpacket.header := ");
1702 context
->current_indentation_level
++;
1703 g_string_assign(context
->field_name
, "");
1704 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1705 ret
= bt_field_type_serialize(trace
->packet_header_type
,
1710 context
->current_indentation_level
--;
1713 g_string_append(context
->string
, ";\n};\n\n");
1719 void append_env_metadata(struct bt_trace
*trace
,
1720 struct metadata_context
*context
)
1725 env_size
= bt_attributes_get_count(trace
->environment
);
1726 if (env_size
<= 0) {
1730 g_string_append(context
->string
, "env {\n");
1732 for (i
= 0; i
< env_size
; i
++) {
1733 struct bt_value
*env_field_value_obj
= NULL
;
1734 const char *entry_name
;
1736 entry_name
= bt_attributes_get_field_name(
1737 trace
->environment
, i
);
1738 env_field_value_obj
= bt_attributes_get_field_value(
1739 trace
->environment
, i
);
1741 BT_ASSERT(entry_name
);
1742 BT_ASSERT(env_field_value_obj
);
1744 switch (bt_value_get_type(env_field_value_obj
)) {
1745 case BT_VALUE_TYPE_INTEGER
:
1750 ret
= bt_value_integer_get(env_field_value_obj
,
1752 BT_ASSERT(ret
== 0);
1753 g_string_append_printf(context
->string
,
1754 "\t%s = %" PRId64
";\n", entry_name
,
1758 case BT_VALUE_TYPE_STRING
:
1761 const char *str_value
;
1762 char *escaped_str
= NULL
;
1764 ret
= bt_value_string_get(env_field_value_obj
,
1766 BT_ASSERT(ret
== 0);
1767 escaped_str
= g_strescape(str_value
, NULL
);
1769 BT_LOGE("Cannot escape string: string=\"%s\"",
1774 g_string_append_printf(context
->string
,
1775 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
1784 BT_PUT(env_field_value_obj
);
1787 g_string_append(context
->string
, "};\n\n");
1790 char *bt_trace_get_metadata_string(struct bt_trace
*trace
)
1792 char *metadata
= NULL
;
1793 struct metadata_context
*context
= NULL
;
1798 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1802 context
= g_new0(struct metadata_context
, 1);
1804 BT_LOGE_STR("Failed to allocate one metadata context.");
1808 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
1809 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
1810 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
1811 if (append_trace_metadata(trace
, context
)) {
1812 /* append_trace_metadata() logs errors */
1815 append_env_metadata(trace
, context
);
1816 g_ptr_array_foreach(trace
->clocks
,
1817 (GFunc
)bt_clock_class_serialize
, context
);
1819 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1820 /* bt_stream_class_serialize() logs details */
1821 err
= bt_stream_class_serialize(
1822 trace
->stream_classes
->pdata
[i
], context
);
1824 /* bt_stream_class_serialize() logs errors */
1829 metadata
= context
->string
->str
;
1832 g_string_free(context
->string
, err
? TRUE
: FALSE
);
1833 g_string_free(context
->field_name
, TRUE
);
1840 enum bt_byte_order
bt_trace_get_native_byte_order(
1841 struct bt_trace
*trace
)
1843 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1844 return trace
->native_byte_order
;
1847 int bt_trace_set_native_byte_order(struct bt_trace
*trace
,
1848 enum bt_byte_order byte_order
)
1853 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1858 if (trace
->frozen
) {
1859 BT_LOGW("Invalid parameter: trace is frozen: "
1860 "addr=%p, name=\"%s\"",
1861 trace
, bt_trace_get_name(trace
));
1866 if (trace
->is_created_by_writer
&&
1867 byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
1868 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
1869 "addr=%p, name=\"%s\"",
1870 trace
, bt_trace_get_name(trace
));
1875 if (byte_order
!= BT_BYTE_ORDER_LITTLE_ENDIAN
&&
1876 byte_order
!= BT_BYTE_ORDER_BIG_ENDIAN
&&
1877 byte_order
!= BT_BYTE_ORDER_NETWORK
) {
1878 BT_LOGW("Invalid parameter: invalid byte order: "
1879 "addr=%p, name=\"%s\", bo=%s",
1880 trace
, bt_trace_get_name(trace
),
1881 bt_byte_order_string(byte_order
));
1886 trace
->native_byte_order
= byte_order
;
1887 BT_LOGV("Set trace's native byte order: "
1888 "addr=%p, name=\"%s\", bo=%s",
1889 trace
, bt_trace_get_name(trace
),
1890 bt_byte_order_string(byte_order
));
1896 struct bt_field_type
*bt_trace_get_packet_header_type(
1897 struct bt_trace
*trace
)
1899 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1900 return bt_get(trace
->packet_header_type
);
1903 int bt_trace_set_packet_header_type(struct bt_trace
*trace
,
1904 struct bt_field_type
*packet_header_type
)
1909 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1914 if (trace
->frozen
) {
1915 BT_LOGW("Invalid parameter: trace is frozen: "
1916 "addr=%p, name=\"%s\"",
1917 trace
, bt_trace_get_name(trace
));
1922 /* packet_header_type must be a structure. */
1923 if (packet_header_type
&&
1924 !bt_field_type_is_structure(packet_header_type
)) {
1925 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1926 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1927 trace
, bt_trace_get_name(trace
),
1929 bt_field_type_id_string(packet_header_type
->id
));
1934 bt_put(trace
->packet_header_type
);
1935 trace
->packet_header_type
= bt_get(packet_header_type
);
1936 BT_LOGV("Set trace's packet header field type: "
1937 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1938 trace
, bt_trace_get_name(trace
), packet_header_type
);
1944 int64_t get_stream_class_count(void *element
)
1946 return bt_trace_get_stream_class_count(
1947 (struct bt_trace
*) element
);
1951 void *get_stream_class(void *element
, int i
)
1953 return bt_trace_get_stream_class_by_index(
1954 (struct bt_trace
*) element
, i
);
1958 int visit_stream_class(void *object
, bt_visitor visitor
,void *data
)
1960 return bt_stream_class_visit(object
, visitor
, data
);
1963 int bt_trace_visit(struct bt_trace
*trace
,
1964 bt_visitor visitor
, void *data
)
1967 struct bt_visitor_object obj
= {
1969 .type
= BT_VISITOR_OBJECT_TYPE_TRACE
1973 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1979 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1984 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1985 trace
, bt_trace_get_name(trace
));
1986 ret
= visitor_helper(&obj
, get_stream_class_count
,
1987 get_stream_class
, visit_stream_class
, visitor
, data
);
1993 int invoke_listener(struct bt_visitor_object
*object
, void *data
)
1995 struct listener_wrapper
*listener_wrapper
= data
;
1997 listener_wrapper
->listener(object
, listener_wrapper
->data
);
2001 // TODO: add logging to this function once we use it internally.
2002 int bt_trace_add_listener(struct bt_trace
*trace
,
2003 bt_listener_cb listener
, void *listener_data
)
2006 struct listener_wrapper
*listener_wrapper
=
2007 g_new0(struct listener_wrapper
, 1);
2009 if (!trace
|| !listener
|| !listener_wrapper
) {
2014 listener_wrapper
->listener
= listener
;
2015 listener_wrapper
->data
= listener_data
;
2017 /* Visit the current schema. */
2018 ret
= bt_trace_visit(trace
, invoke_listener
, listener_wrapper
);
2024 * Add listener to the array of callbacks which will be invoked on
2027 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
2030 g_free(listener_wrapper
);
2035 int bt_trace_object_modification(struct bt_visitor_object
*object
,
2039 struct bt_trace
*trace
= trace_ptr
;
2044 if (trace
->listeners
->len
== 0) {
2048 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
2049 struct listener_wrapper
*listener
=
2050 g_ptr_array_index(trace
->listeners
, i
);
2052 listener
->listener(object
, listener
->data
);
2059 struct bt_field_type
*get_field_type(enum field_type_alias alias
)
2062 unsigned int alignment
, size
;
2063 struct bt_field_type
*field_type
= NULL
;
2065 if (alias
>= NR_FIELD_TYPE_ALIAS
) {
2069 alignment
= field_type_aliases_alignments
[alias
];
2070 size
= field_type_aliases_sizes
[alias
];
2071 field_type
= bt_field_type_integer_create(size
);
2072 ret
= bt_field_type_set_alignment(field_type
, alignment
);
2081 void bt_trace_freeze(struct bt_trace
*trace
)
2085 if (trace
->frozen
) {
2089 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2090 trace
, bt_trace_get_name(trace
));
2091 BT_LOGD_STR("Freezing packet header field type.");
2092 bt_field_type_freeze(trace
->packet_header_type
);
2093 BT_LOGD_STR("Freezing environment attributes.");
2094 bt_attributes_freeze(trace
->environment
);
2096 if (trace
->clocks
->len
> 0) {
2097 BT_LOGD_STR("Freezing clock classes.");
2100 for (i
= 0; i
< trace
->clocks
->len
; i
++) {
2101 struct bt_clock_class
*clock_class
=
2102 g_ptr_array_index(trace
->clocks
, i
);
2104 bt_clock_class_freeze(clock_class
);
2110 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
2112 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
2113 return trace
->is_static
;
2116 int bt_trace_set_is_static(struct bt_trace
*trace
)
2122 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2127 ret
= check_packet_header_type_has_no_clock_class(trace
);
2129 /* check_packet_header_type_has_no_clock_class() logs errors */
2133 trace
->is_static
= BT_TRUE
;
2134 bt_trace_freeze(trace
);
2135 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2136 trace
, bt_trace_get_name(trace
));
2138 /* Call all the "trace is static" listeners */
2139 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2140 struct bt_trace_is_static_listener_elem elem
=
2141 g_array_index(trace
->is_static_listeners
,
2142 struct bt_trace_is_static_listener_elem
, i
);
2145 elem
.func(trace
, elem
.data
);
2153 int bt_trace_add_is_static_listener(struct bt_trace
*trace
,
2154 bt_trace_is_static_listener listener
,
2155 bt_trace_listener_removed listener_removed
, void *data
)
2158 struct bt_trace_is_static_listener_elem new_elem
= {
2160 .removed
= listener_removed
,
2165 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2171 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2176 if (trace
->is_static
) {
2177 BT_LOGW("Invalid parameter: trace is already static: "
2178 "addr=%p, name=\"%s\"",
2179 trace
, bt_trace_get_name(trace
));
2184 if (trace
->in_remove_listener
) {
2185 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2186 "addr=%p, name=\"%s\"",
2187 trace
, bt_trace_get_name(trace
));
2192 /* Find the next available spot */
2193 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
2194 struct bt_trace_is_static_listener_elem elem
=
2195 g_array_index(trace
->is_static_listeners
,
2196 struct bt_trace_is_static_listener_elem
, i
);
2203 if (i
== trace
->is_static_listeners
->len
) {
2204 g_array_append_val(trace
->is_static_listeners
, new_elem
);
2206 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
2209 BT_LOGV("Added \"trace is static\" listener: "
2210 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2211 "data-addr=%p, listener-id=%d",
2212 trace
, bt_trace_get_name(trace
), listener
, data
, i
);
2218 int bt_trace_remove_is_static_listener(
2219 struct bt_trace
*trace
, int listener_id
)
2222 struct bt_trace_is_static_listener_elem
*elem
;
2225 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2230 if (trace
->in_remove_listener
) {
2231 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2232 "addr=%p, name=\"%s\", listener-id=%d",
2233 trace
, bt_trace_get_name(trace
),
2239 if (listener_id
< 0) {
2240 BT_LOGW("Invalid listener ID: must be zero or positive: "
2241 "listener-id=%d", listener_id
);
2246 if (listener_id
>= trace
->is_static_listeners
->len
) {
2247 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2248 "addr=%p, name=\"%s\", listener-id=%d",
2249 trace
, bt_trace_get_name(trace
),
2255 elem
= &g_array_index(trace
->is_static_listeners
,
2256 struct bt_trace_is_static_listener_elem
,
2259 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2260 "addr=%p, name=\"%s\", listener-id=%d",
2261 trace
, bt_trace_get_name(trace
),
2267 if (elem
->removed
) {
2268 /* Call remove listener */
2269 BT_LOGV("Calling remove listener: "
2270 "trace-addr=%p, trace-name=\"%s\", "
2271 "listener-id=%d", trace
, bt_trace_get_name(trace
),
2273 trace
->in_remove_listener
= BT_TRUE
;
2274 elem
->removed(trace
, elem
->data
);
2275 trace
->in_remove_listener
= BT_FALSE
;
2279 elem
->removed
= NULL
;
2281 BT_LOGV("Removed \"trace is static\" listener: "
2282 "trace-addr=%p, trace-name=\"%s\", "
2283 "listener-id=%d", trace
, bt_trace_get_name(trace
),