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/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/trace-internal.h>
34 #include <babeltrace/ctf-ir/clock-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-internal.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/event-internal.h>
38 #include <babeltrace/ctf-ir/event-class.h>
39 #include <babeltrace/ctf-ir/event-class-internal.h>
40 #include <babeltrace/ctf-writer/functor-internal.h>
41 #include <babeltrace/ctf-writer/clock-internal.h>
42 #include <babeltrace/ctf-ir/field-wrapper-internal.h>
43 #include <babeltrace/ctf-ir/field-types-internal.h>
44 #include <babeltrace/ctf-ir/attributes-internal.h>
45 #include <babeltrace/ctf-ir/validation-internal.h>
46 #include <babeltrace/ctf-ir/visitor-internal.h>
47 #include <babeltrace/ctf-ir/utils.h>
48 #include <babeltrace/ctf-ir/utils-internal.h>
49 #include <babeltrace/compiler-internal.h>
50 #include <babeltrace/values.h>
51 #include <babeltrace/values-internal.h>
52 #include <babeltrace/ref.h>
53 #include <babeltrace/types.h>
54 #include <babeltrace/endian-internal.h>
55 #include <babeltrace/assert-internal.h>
61 #define DEFAULT_IDENTIFIER_SIZE 128
62 #define DEFAULT_METADATA_STRING_SIZE 4096
64 struct listener_wrapper
{
65 bt_listener_cb listener
;
69 struct bt_trace_is_static_listener_elem
{
70 bt_trace_is_static_listener func
;
71 bt_trace_listener_removed removed
;
76 void bt_trace_destroy(struct bt_object
*obj
)
78 struct bt_trace
*trace
= (void *) obj
;
80 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
81 trace
, bt_trace_get_name(trace
));
84 * Call remove listeners first so that everything else still
85 * exists in the trace.
87 if (trace
->is_static_listeners
) {
90 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
91 struct bt_trace_is_static_listener_elem elem
=
92 g_array_index(trace
->is_static_listeners
,
93 struct bt_trace_is_static_listener_elem
, i
);
96 elem
.removed(trace
, elem
.data
);
100 g_array_free(trace
->is_static_listeners
, TRUE
);
103 if (trace
->listeners
) {
104 g_ptr_array_free(trace
->listeners
, TRUE
);
107 bt_object_pool_finalize(&trace
->packet_header_field_pool
);
108 bt_trace_common_finalize(BT_TO_COMMON(trace
));
113 int bt_trace_common_initialize(struct bt_trace_common
*trace
,
114 bt_object_release_func release_func
)
118 BT_LOGD_STR("Initializing common trace object.");
119 trace
->native_byte_order
= BT_BYTE_ORDER_UNSPECIFIED
;
120 bt_object_init(trace
, release_func
);
121 trace
->clock_classes
= g_ptr_array_new_with_free_func(
122 (GDestroyNotify
) bt_put
);
123 if (!trace
->clock_classes
) {
124 BT_LOGE_STR("Failed to allocate one GPtrArray.");
128 trace
->streams
= g_ptr_array_new_with_free_func(
129 (GDestroyNotify
) bt_object_release
);
130 if (!trace
->streams
) {
131 BT_LOGE_STR("Failed to allocate one GPtrArray.");
135 trace
->stream_classes
= g_ptr_array_new_with_free_func(
136 (GDestroyNotify
) bt_object_release
);
137 if (!trace
->stream_classes
) {
138 BT_LOGE_STR("Failed to allocate one GPtrArray.");
142 /* Create the environment array object */
143 trace
->environment
= bt_attributes_create();
144 if (!trace
->environment
) {
145 BT_LOGE_STR("Cannot create empty attributes object.");
149 BT_LOGD("Initialized common trace object: addr=%p", trace
);
160 void bt_trace_common_finalize(struct bt_trace_common
*trace
)
162 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
163 trace
, bt_trace_common_get_name(trace
));
165 if (trace
->environment
) {
166 BT_LOGD_STR("Destroying environment attributes.");
167 bt_attributes_destroy(trace
->environment
);
171 g_string_free(trace
->name
, TRUE
);
174 if (trace
->clock_classes
) {
175 BT_LOGD_STR("Putting clock classes.");
176 g_ptr_array_free(trace
->clock_classes
, TRUE
);
179 if (trace
->streams
) {
180 BT_LOGD_STR("Destroying streams.");
181 g_ptr_array_free(trace
->streams
, TRUE
);
184 if (trace
->stream_classes
) {
185 BT_LOGD_STR("Destroying stream classes.");
186 g_ptr_array_free(trace
->stream_classes
, TRUE
);
189 BT_LOGD_STR("Putting packet header field type.");
190 bt_put(trace
->packet_header_field_type
);
194 void free_packet_header_field(struct bt_field_wrapper
*field_wrapper
,
195 struct bt_trace
*trace
)
197 bt_field_wrapper_destroy(field_wrapper
);
200 struct bt_trace
*bt_trace_create(void)
202 struct bt_trace
*trace
= NULL
;
205 BT_LOGD_STR("Creating trace object.");
206 trace
= g_new0(struct bt_trace
, 1);
208 BT_LOGE_STR("Failed to allocate one trace.");
212 ret
= bt_trace_common_initialize(BT_TO_COMMON(trace
), bt_trace_destroy
);
214 /* bt_trace_common_initialize() logs errors */
218 trace
->listeners
= g_ptr_array_new_with_free_func(
219 (GDestroyNotify
) g_free
);
220 if (!trace
->listeners
) {
221 BT_LOGE_STR("Failed to allocate one GPtrArray.");
225 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
226 sizeof(struct bt_trace_is_static_listener_elem
));
227 if (!trace
->is_static_listeners
) {
228 BT_LOGE_STR("Failed to allocate one GArray.");
232 ret
= bt_object_pool_initialize(&trace
->packet_header_field_pool
,
233 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
234 (bt_object_pool_destroy_object_func
) free_packet_header_field
,
237 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
242 BT_LOGD("Created trace object: addr=%p", trace
);
250 const char *bt_trace_get_name(struct bt_trace
*trace
)
252 return bt_trace_common_get_name(BT_TO_COMMON(trace
));
256 int bt_trace_common_set_name(struct bt_trace_common
*trace
, const char *name
)
261 BT_LOGW_STR("Invalid parameter: trace is NULL.");
267 BT_LOGW_STR("Invalid parameter: name is NULL.");
273 BT_LOGW("Invalid parameter: trace is frozen: "
274 "addr=%p, name=\"%s\"",
275 trace
, bt_trace_common_get_name(trace
));
280 trace
->name
= trace
->name
? g_string_assign(trace
->name
, name
) :
283 BT_LOGE_STR("Failed to allocate one GString.");
288 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace
, name
);
294 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
296 return bt_trace_common_set_name(BT_TO_COMMON(trace
), name
);
299 const unsigned char *bt_trace_get_uuid(struct bt_trace
*trace
)
301 return bt_trace_common_get_uuid(BT_TO_COMMON(trace
));
305 int bt_trace_common_set_uuid(struct bt_trace_common
*trace
,
306 const unsigned char *uuid
)
311 BT_LOGW_STR("Invalid parameter: trace is NULL.");
317 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
323 BT_LOGW("Invalid parameter: trace is frozen: "
324 "addr=%p, name=\"%s\"",
325 trace
, bt_trace_common_get_name(trace
));
330 memcpy(trace
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
331 trace
->uuid_set
= BT_TRUE
;
332 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
333 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
334 trace
, bt_trace_common_get_name(trace
),
335 (unsigned int) uuid
[0],
336 (unsigned int) uuid
[1],
337 (unsigned int) uuid
[2],
338 (unsigned int) uuid
[3],
339 (unsigned int) uuid
[4],
340 (unsigned int) uuid
[5],
341 (unsigned int) uuid
[6],
342 (unsigned int) uuid
[7],
343 (unsigned int) uuid
[8],
344 (unsigned int) uuid
[9],
345 (unsigned int) uuid
[10],
346 (unsigned int) uuid
[11],
347 (unsigned int) uuid
[12],
348 (unsigned int) uuid
[13],
349 (unsigned int) uuid
[14],
350 (unsigned int) uuid
[15]);
356 int bt_trace_set_uuid(struct bt_trace
*trace
,
357 const unsigned char *uuid
)
359 return bt_trace_common_set_uuid(BT_TO_COMMON(trace
), uuid
);
363 int bt_trace_common_set_environment_field(struct bt_trace_common
*trace
,
364 const char *name
, struct bt_value
*value
)
369 BT_LOGW_STR("Invalid parameter: trace is NULL.");
375 BT_LOGW_STR("Invalid parameter: name is NULL.");
381 BT_LOGW_STR("Invalid parameter: value is NULL.");
386 if (!bt_identifier_is_valid(name
)) {
387 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
388 "trace-addr=%p, trace-name=\"%s\", "
390 trace
, bt_trace_common_get_name(trace
), name
);
395 if (!bt_value_is_integer(value
) && !bt_value_is_string(value
)) {
396 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
397 "trace-addr=%p, trace-name=\"%s\", "
398 "env-name=\"%s\", env-value-type=%s",
399 trace
, bt_trace_common_get_name(trace
), name
,
400 bt_value_type_string(bt_value_get_type(value
)));
407 * New environment fields may be added to a frozen trace,
408 * but existing fields may not be changed.
410 * The object passed is frozen like all other attributes.
412 struct bt_value
*attribute
=
413 bt_attributes_borrow_field_value_by_name(
414 trace
->environment
, name
);
417 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
418 "trace-addr=%p, trace-name=\"%s\", "
420 trace
, bt_trace_common_get_name(trace
), name
);
425 bt_value_freeze(value
);
428 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
431 BT_LOGE("Cannot set environment field's value: "
432 "trace-addr=%p, trace-name=\"%s\", "
434 trace
, bt_trace_common_get_name(trace
), name
);
436 BT_LOGV("Set environment field's value: "
437 "trace-addr=%p, trace-name=\"%s\", "
438 "env-name=\"%s\", value-addr=%p",
439 trace
, bt_trace_common_get_name(trace
), name
, value
);
446 int bt_trace_set_environment_field(struct bt_trace
*trace
,
447 const char *name
, struct bt_value
*value
)
451 if (trace
->is_static
) {
452 BT_LOGW("Invalid parameter: trace is static: "
453 "addr=%p, name=\"%s\"",
454 trace
, bt_trace_get_name(trace
));
459 ret
= bt_trace_common_set_environment_field(BT_TO_COMMON(trace
),
467 int bt_trace_common_set_environment_field_string(struct bt_trace_common
*trace
,
468 const char *name
, const char *value
)
471 struct bt_value
*env_value_string_obj
= NULL
;
474 BT_LOGW_STR("Invalid parameter: value is NULL.");
479 env_value_string_obj
= bt_value_string_create_init(value
);
480 if (!env_value_string_obj
) {
481 BT_LOGE_STR("Cannot create string value object.");
486 /* bt_trace_common_set_environment_field() logs errors */
487 ret
= bt_trace_common_set_environment_field(trace
, name
,
488 env_value_string_obj
);
491 bt_put(env_value_string_obj
);
495 int bt_trace_set_environment_field_string(struct bt_trace
*trace
,
496 const char *name
, const char *value
)
498 return bt_trace_common_set_environment_field_string(BT_TO_COMMON(trace
),
503 int bt_trace_common_set_environment_field_integer(
504 struct bt_trace_common
*trace
, const char *name
, int64_t value
)
507 struct bt_value
*env_value_integer_obj
= NULL
;
509 env_value_integer_obj
= bt_value_integer_create_init(value
);
510 if (!env_value_integer_obj
) {
511 BT_LOGE_STR("Cannot create integer value object.");
516 /* bt_trace_common_set_environment_field() logs errors */
517 ret
= bt_trace_common_set_environment_field(trace
, name
,
518 env_value_integer_obj
);
521 bt_put(env_value_integer_obj
);
525 int bt_trace_set_environment_field_integer(
526 struct bt_trace
*trace
, const char *name
, int64_t value
)
528 return bt_trace_common_set_environment_field_integer(
529 BT_TO_COMMON(trace
), name
, value
);
532 int64_t bt_trace_get_environment_field_count(struct bt_trace
*trace
)
534 return bt_trace_common_get_environment_field_count(BT_TO_COMMON(trace
));
538 bt_trace_get_environment_field_name_by_index(struct bt_trace
*trace
,
541 return bt_trace_common_get_environment_field_name_by_index(
542 BT_TO_COMMON(trace
), index
);
545 struct bt_value
*bt_trace_borrow_environment_field_value_by_index(
546 struct bt_trace
*trace
, uint64_t index
)
548 return bt_trace_common_borrow_environment_field_value_by_index(
549 BT_TO_COMMON(trace
), index
);
552 struct bt_value
*bt_trace_borrow_environment_field_value_by_name(
553 struct bt_trace
*trace
, const char *name
)
555 return bt_trace_common_borrow_environment_field_value_by_name(
556 BT_TO_COMMON(trace
), name
);
560 int bt_trace_common_add_clock_class(struct bt_trace_common
*trace
,
561 struct bt_clock_class
*clock_class
)
566 BT_LOGW_STR("Invalid parameter: trace is NULL.");
571 if (!bt_clock_class_is_valid(clock_class
)) {
572 BT_LOGW("Invalid parameter: clock class is invalid: "
573 "trace-addr=%p, trace-name=\"%s\", "
574 "clock-class-addr=%p, clock-class-name=\"%s\"",
575 trace
, bt_trace_common_get_name(trace
),
576 clock_class
, bt_clock_class_get_name(clock_class
));
581 /* Check for duplicate clock classes */
582 if (bt_trace_common_has_clock_class(trace
, clock_class
)) {
583 BT_LOGW("Invalid parameter: clock class already exists in trace: "
584 "trace-addr=%p, trace-name=\"%s\", "
585 "clock-class-addr=%p, clock-class-name=\"%s\"",
586 trace
, bt_trace_common_get_name(trace
),
587 clock_class
, bt_clock_class_get_name(clock_class
));
593 g_ptr_array_add(trace
->clock_classes
, clock_class
);
596 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
597 bt_clock_class_freeze(clock_class
);
600 BT_LOGV("Added clock class to trace: "
601 "trace-addr=%p, trace-name=\"%s\", "
602 "clock-class-addr=%p, clock-class-name=\"%s\"",
603 trace
, bt_trace_common_get_name(trace
),
604 clock_class
, bt_clock_class_get_name(clock_class
));
610 int bt_trace_add_clock_class(struct bt_trace
*trace
,
611 struct bt_clock_class
*clock_class
)
615 if (trace
->is_static
) {
616 BT_LOGW("Invalid parameter: trace is static: "
617 "addr=%p, name=\"%s\"",
618 trace
, bt_trace_get_name(trace
));
623 ret
= bt_trace_common_add_clock_class(BT_TO_COMMON(trace
),
630 int64_t bt_trace_get_clock_class_count(struct bt_trace
*trace
)
632 return bt_trace_common_get_clock_class_count(BT_TO_COMMON(trace
));
635 struct bt_clock_class
*bt_trace_borrow_clock_class_by_index(
636 struct bt_trace
*trace
, uint64_t index
)
638 return bt_trace_common_borrow_clock_class_by_index(
639 BT_TO_COMMON(trace
), index
);
643 bool packet_header_field_type_is_valid(struct bt_trace_common
*trace
,
644 struct bt_field_type_common
*packet_header_type
)
647 bool is_valid
= true;
648 struct bt_field_type_common
*field_type
= NULL
;
650 if (!packet_header_type
) {
652 * No packet header field type: trace must have only
653 * one stream. At this point the stream class being
654 * added is not part of the trace yet, so we validate
655 * that the trace contains no stream classes yet.
657 if (trace
->stream_classes
->len
>= 1) {
658 BT_LOGW_STR("Invalid packet header field type: "
659 "packet header field type does not exist but there's more than one stream class in the trace.");
663 /* No packet header field type: valid at this point */
667 /* Packet header field type, if it exists, must be a structure */
668 if (packet_header_type
->id
!= BT_FIELD_TYPE_ID_STRUCT
) {
669 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
670 "ft-addr=%p, ft-id=%s",
672 bt_common_field_type_id_string(packet_header_type
->id
));
677 * If there's a `magic` field, it must be a 32-bit unsigned
678 * integer field type. Also it must be the first field of the
679 * packet header field type.
681 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
682 packet_header_type
, "magic");
684 const char *field_name
;
686 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
687 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
688 "magic-ft-addr=%p, magic-ft-id=%s",
690 bt_common_field_type_id_string(field_type
->id
));
694 if (bt_field_type_common_integer_is_signed(field_type
)) {
695 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
696 "magic-ft-addr=%p", field_type
);
700 if (bt_field_type_common_integer_get_size(field_type
) != 32) {
701 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
702 "magic-ft-addr=%p, magic-ft-size=%u",
704 bt_field_type_common_integer_get_size(field_type
));
708 ret
= bt_field_type_common_structure_borrow_field_by_index(
709 packet_header_type
, &field_name
, NULL
, 0);
712 if (strcmp(field_name
, "magic") != 0) {
713 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
714 "magic-ft-addr=%p, first-field-name=\"%s\"",
715 field_type
, field_name
);
721 * If there's a `uuid` field, it must be an array field type of
722 * length 16 with an 8-bit unsigned integer element field type.
724 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
725 packet_header_type
, "uuid");
727 struct bt_field_type_common
*elem_ft
;
729 if (field_type
->id
!= BT_FIELD_TYPE_ID_ARRAY
) {
730 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
731 "uuid-ft-addr=%p, uuid-ft-id=%s",
733 bt_common_field_type_id_string(field_type
->id
));
737 if (bt_field_type_common_array_get_length(field_type
) != 16) {
738 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
739 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64
,
741 bt_field_type_common_array_get_length(field_type
));
745 elem_ft
= bt_field_type_common_array_borrow_element_field_type(field_type
);
748 if (elem_ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
749 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
750 "elem-ft-addr=%p, elem-ft-id=%s",
752 bt_common_field_type_id_string(elem_ft
->id
));
756 if (bt_field_type_common_integer_is_signed(elem_ft
)) {
757 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
758 "elem-ft-addr=%p", elem_ft
);
762 if (bt_field_type_common_integer_get_size(elem_ft
) != 8) {
763 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
764 "elem-ft-addr=%p, elem-ft-size=%u",
766 bt_field_type_common_integer_get_size(elem_ft
));
772 * The `stream_id` field must exist if there's more than one
773 * stream classes in the trace.
775 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
776 packet_header_type
, "stream_id");
778 if (!field_type
&& trace
->stream_classes
->len
>= 1) {
779 BT_LOGW_STR("Invalid packet header field type: "
780 "`stream_id` field does not exist but there's more than one stream class in the trace.");
785 * If there's a `stream_id` field, it must be an unsigned
786 * integer field type.
789 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
790 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
791 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
793 bt_common_field_type_id_string(field_type
->id
));
797 if (bt_field_type_common_integer_is_signed(field_type
)) {
798 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
799 "stream-id-ft-addr=%p", field_type
);
805 * If there's a `packet_seq_num` field, it must be an unsigned
806 * integer field type.
808 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
809 packet_header_type
, "packet_seq_num");
811 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
812 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
813 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
815 bt_common_field_type_id_string(field_type
->id
));
819 if (bt_field_type_common_integer_is_signed(field_type
)) {
820 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
821 "packet-seq-num-ft-addr=%p", field_type
);
836 bool packet_context_field_type_is_valid(struct bt_trace_common
*trace
,
837 struct bt_stream_class_common
*stream_class
,
838 struct bt_field_type_common
*packet_context_type
,
839 bool check_ts_begin_end_mapped
)
841 bool is_valid
= true;
842 struct bt_field_type_common
*field_type
= NULL
;
844 if (!packet_context_type
) {
845 /* No packet context field type: valid at this point */
849 /* Packet context field type, if it exists, must be a structure */
850 if (packet_context_type
->id
!= BT_FIELD_TYPE_ID_STRUCT
) {
851 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
852 "ft-addr=%p, ft-id=%s",
854 bt_common_field_type_id_string(packet_context_type
->id
));
859 * If there's a `packet_size` field, it must be an unsigned
860 * integer field type.
862 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
863 packet_context_type
, "packet_size");
865 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
866 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
867 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
869 bt_common_field_type_id_string(field_type
->id
));
873 if (bt_field_type_common_integer_is_signed(field_type
)) {
874 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
875 "packet-size-ft-addr=%p", field_type
);
881 * If there's a `content_size` field, it must be an unsigned
882 * integer field type.
884 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
885 packet_context_type
, "content_size");
887 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
888 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
889 "content-size-ft-addr=%p, content-size-ft-id=%s",
891 bt_common_field_type_id_string(field_type
->id
));
895 if (bt_field_type_common_integer_is_signed(field_type
)) {
896 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
897 "content-size-ft-addr=%p", field_type
);
903 * If there's a `events_discarded` field, it must be an unsigned
904 * integer field type.
906 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
907 packet_context_type
, "events_discarded");
909 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
910 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
911 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
913 bt_common_field_type_id_string(field_type
->id
));
917 if (bt_field_type_common_integer_is_signed(field_type
)) {
918 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
919 "events-discarded-ft-addr=%p", field_type
);
925 * If there's a `timestamp_begin` field, it must be an unsigned
926 * integer field type. Also, if the trace is not a CTF writer's
927 * trace, then we cannot automatically set the mapped clock
928 * class of this field, so it must have a mapped clock class.
930 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
931 packet_context_type
, "timestamp_begin");
933 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
934 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
935 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
937 bt_common_field_type_id_string(field_type
->id
));
941 if (bt_field_type_common_integer_is_signed(field_type
)) {
942 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
943 "timestamp-begin-ft-addr=%p", field_type
);
947 if (check_ts_begin_end_mapped
) {
948 struct bt_clock_class
*clock_class
=
949 bt_field_type_common_integer_borrow_mapped_clock_class(
953 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
954 "timestamp-begin-ft-addr=%p", field_type
);
961 * If there's a `timestamp_end` field, it must be an unsigned
962 * integer field type. Also, if the trace is not a CTF writer's
963 * trace, then we cannot automatically set the mapped clock
964 * class of this field, so it must have a mapped clock class.
966 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
967 packet_context_type
, "timestamp_end");
969 if (field_type
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
970 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
971 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
973 bt_common_field_type_id_string(field_type
->id
));
977 if (bt_field_type_common_integer_is_signed(field_type
)) {
978 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
979 "timestamp-end-ft-addr=%p", field_type
);
983 if (check_ts_begin_end_mapped
) {
984 struct bt_clock_class
*clock_class
=
985 bt_field_type_common_integer_borrow_mapped_clock_class(
989 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
990 "timestamp-end-ft-addr=%p", field_type
);
1006 bool event_header_field_type_is_valid(struct bt_trace_common
*trace
,
1007 struct bt_stream_class_common
*stream_class
,
1008 struct bt_field_type_common
*event_header_type
)
1010 bool is_valid
= true;
1011 struct bt_field_type_common
*field_type
= NULL
;
1014 * We do not validate that the `timestamp` field exists here
1015 * because CTF does not require this exact name to be mapped to
1019 if (!event_header_type
) {
1021 * No event header field type: stream class must have
1022 * only one event class.
1024 if (bt_stream_class_common_get_event_class_count(stream_class
) > 1) {
1025 BT_LOGW_STR("Invalid event header field type: "
1026 "event header field type does not exist but there's more than one event class in the stream class.");
1030 /* No event header field type: valid at this point */
1034 /* Event header field type, if it exists, must be a structure */
1035 if (event_header_type
->id
!= BT_FIELD_TYPE_ID_STRUCT
) {
1036 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1037 "ft-addr=%p, ft-id=%s",
1039 bt_common_field_type_id_string(event_header_type
->id
));
1044 * If there's an `id` field, it must be an unsigned integer
1045 * field type or an enumeration field type with an unsigned
1046 * integer container field type.
1048 field_type
= bt_field_type_common_structure_borrow_field_type_by_name(
1049 event_header_type
, "id");
1051 struct bt_field_type_common
*int_ft
;
1053 if (field_type
->id
== BT_FIELD_TYPE_ID_INTEGER
) {
1054 int_ft
= field_type
;
1055 } else if (field_type
->id
== BT_FIELD_TYPE_ID_ENUM
) {
1056 int_ft
= bt_field_type_common_enumeration_borrow_container_field_type(
1059 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1060 "id-ft-addr=%p, id-ft-id=%s",
1062 bt_common_field_type_id_string(field_type
->id
));
1067 if (bt_field_type_common_integer_is_signed(int_ft
)) {
1068 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1069 "id-ft-addr=%p", int_ft
);
1084 int check_packet_header_type_has_no_clock_class(struct bt_trace_common
*trace
)
1088 if (trace
->packet_header_field_type
) {
1089 struct bt_clock_class
*clock_class
= NULL
;
1091 ret
= bt_field_type_common_validate_single_clock_class(
1092 trace
->packet_header_field_type
,
1094 bt_put(clock_class
);
1095 if (ret
|| clock_class
) {
1096 BT_LOGW("Trace's packet header field type cannot "
1097 "contain a field type which is mapped to "
1099 "trace-addr=%p, trace-name=\"%s\", "
1100 "clock-class-name=\"%s\"",
1101 trace
, bt_trace_common_get_name(trace
),
1103 bt_clock_class_get_name(clock_class
) :
1113 int bt_trace_common_add_stream_class(struct bt_trace_common
*trace
,
1114 struct bt_stream_class_common
*stream_class
,
1115 bt_validation_flag_copy_field_type_func copy_field_type_func
,
1116 struct bt_clock_class
*init_expected_clock_class
,
1117 int (*map_clock_classes_func
)(struct bt_stream_class_common
*stream_class
,
1118 struct bt_field_type_common
*packet_context_field_type
,
1119 struct bt_field_type_common
*event_header_field_type
),
1120 bool check_ts_begin_end_mapped
)
1125 struct bt_validation_output trace_sc_validation_output
= { 0 };
1126 struct bt_validation_output
*ec_validation_outputs
= NULL
;
1127 const enum bt_validation_flag trace_sc_validation_flags
=
1128 BT_VALIDATION_FLAG_TRACE
|
1129 BT_VALIDATION_FLAG_STREAM
;
1130 const enum bt_validation_flag ec_validation_flags
=
1131 BT_VALIDATION_FLAG_EVENT
;
1132 struct bt_field_type_common
*packet_header_type
= NULL
;
1133 struct bt_field_type_common
*packet_context_type
= NULL
;
1134 struct bt_field_type_common
*event_header_type
= NULL
;
1135 struct bt_field_type_common
*stream_event_ctx_type
= NULL
;
1136 int64_t event_class_count
;
1137 struct bt_trace_common
*current_parent_trace
= NULL
;
1138 struct bt_clock_class
*expected_clock_class
=
1139 bt_get(init_expected_clock_class
);
1141 BT_ASSERT(copy_field_type_func
);
1144 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1149 if (!stream_class
) {
1150 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1155 BT_LOGD("Adding stream class to trace: "
1156 "trace-addr=%p, trace-name=\"%s\", "
1157 "stream-class-addr=%p, stream-class-name=\"%s\", "
1158 "stream-class-id=%" PRId64
,
1159 trace
, bt_trace_common_get_name(trace
),
1160 stream_class
, bt_stream_class_common_get_name(stream_class
),
1161 bt_stream_class_common_get_id(stream_class
));
1163 current_parent_trace
= bt_stream_class_common_borrow_trace(stream_class
);
1164 if (current_parent_trace
) {
1165 /* Stream class is already associated to a trace, abort. */
1166 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1167 "stream-class-trace-addr=%p, "
1168 "stream-class-trace-name=\"%s\"",
1169 current_parent_trace
,
1170 bt_trace_common_get_name(current_parent_trace
));
1176 bt_stream_class_common_get_event_class_count(stream_class
);
1177 BT_ASSERT(event_class_count
>= 0);
1179 if (!stream_class
->frozen
) {
1181 * Stream class is not frozen yet. Validate that the
1182 * stream class contains at most a single clock class
1183 * because the previous
1184 * bt_stream_class_common_add_event_class() calls did
1185 * not make this validation since the stream class's
1186 * direct field types (packet context, event header,
1187 * event context) could change afterwards. This stream
1188 * class is about to be frozen and those field types
1189 * won't be changed if this function succeeds.
1191 * At this point we're also sure that the stream class's
1192 * clock, if any, has the same class as the stream
1193 * class's expected clock class, if any. This is why, if
1194 * bt_stream_class_common_validate_single_clock_class()
1195 * succeeds below, the call to
1196 * bt_stream_class_map_clock_class() at the end of this
1197 * function is safe because it maps to the same, single
1200 ret
= bt_stream_class_common_validate_single_clock_class(
1201 stream_class
, &expected_clock_class
);
1203 BT_LOGW("Invalid parameter: stream class or one of its "
1204 "event classes contains a field type which is "
1205 "not recursively mapped to the expected "
1207 "stream-class-addr=%p, "
1208 "stream-class-id=%" PRId64
", "
1209 "stream-class-name=\"%s\", "
1210 "expected-clock-class-addr=%p, "
1211 "expected-clock-class-name=\"%s\"",
1212 stream_class
, bt_stream_class_common_get_id(stream_class
),
1213 bt_stream_class_common_get_name(stream_class
),
1214 expected_clock_class
,
1215 expected_clock_class
?
1216 bt_clock_class_get_name(expected_clock_class
) :
1222 ret
= check_packet_header_type_has_no_clock_class(trace
);
1224 /* check_packet_header_type_has_no_clock_class() logs errors */
1229 * We're about to freeze both the trace and the stream class.
1230 * Also, each event class contained in this stream class are
1233 * This trace, this stream class, and all its event classes
1234 * should be valid at this point.
1236 * Validate trace and stream class first, then each event
1237 * class of this stream class can be validated individually.
1239 packet_header_type
=
1240 bt_trace_common_borrow_packet_header_field_type(trace
);
1241 packet_context_type
=
1242 bt_stream_class_common_borrow_packet_context_field_type(stream_class
);
1244 bt_stream_class_common_borrow_event_header_field_type(stream_class
);
1245 stream_event_ctx_type
=
1246 bt_stream_class_common_borrow_event_context_field_type(stream_class
);
1248 BT_LOGD("Validating trace and stream class field types.");
1249 ret
= bt_validate_class_types(trace
->environment
,
1250 packet_header_type
, packet_context_type
, event_header_type
,
1251 stream_event_ctx_type
, NULL
, NULL
, trace
->valid
,
1252 stream_class
->valid
, 1, &trace_sc_validation_output
,
1253 trace_sc_validation_flags
, copy_field_type_func
);
1257 * This means something went wrong during the validation
1258 * process, not that the objects are invalid.
1260 BT_LOGE("Failed to validate trace and stream class field types: "
1265 if ((trace_sc_validation_output
.valid_flags
&
1266 trace_sc_validation_flags
) !=
1267 trace_sc_validation_flags
) {
1268 /* Invalid trace/stream class */
1269 BT_LOGW("Invalid trace or stream class field types: "
1271 trace_sc_validation_output
.valid_flags
);
1276 if (event_class_count
> 0) {
1277 ec_validation_outputs
= g_new0(struct bt_validation_output
,
1279 if (!ec_validation_outputs
) {
1280 BT_LOGE_STR("Failed to allocate one validation output structure.");
1286 /* Validate each event class individually */
1287 for (i
= 0; i
< event_class_count
; i
++) {
1288 struct bt_event_class_common
*event_class
=
1289 bt_stream_class_common_borrow_event_class_by_index(
1291 struct bt_field_type_common
*event_context_type
= NULL
;
1292 struct bt_field_type_common
*event_payload_type
= NULL
;
1294 event_context_type
=
1295 bt_event_class_common_borrow_context_field_type(
1297 event_payload_type
=
1298 bt_event_class_common_borrow_payload_field_type(
1302 * It is important to use the field types returned by
1303 * the previous trace and stream class validation here
1304 * because copies could have been made.
1306 BT_LOGD("Validating event class's field types: "
1307 "addr=%p, name=\"%s\", id=%" PRId64
,
1308 event_class
, bt_event_class_common_get_name(event_class
),
1309 bt_event_class_common_get_id(event_class
));
1310 ret
= bt_validate_class_types(trace
->environment
,
1311 trace_sc_validation_output
.packet_header_type
,
1312 trace_sc_validation_output
.packet_context_type
,
1313 trace_sc_validation_output
.event_header_type
,
1314 trace_sc_validation_output
.stream_event_ctx_type
,
1315 event_context_type
, event_payload_type
,
1316 1, 1, event_class
->valid
, &ec_validation_outputs
[i
],
1317 ec_validation_flags
, copy_field_type_func
);
1320 BT_LOGE("Failed to validate event class field types: "
1325 if ((ec_validation_outputs
[i
].valid_flags
&
1326 ec_validation_flags
) != ec_validation_flags
) {
1327 /* Invalid event class */
1328 BT_LOGW("Invalid event class field types: "
1330 ec_validation_outputs
[i
].valid_flags
);
1336 stream_id
= bt_stream_class_common_get_id(stream_class
);
1337 if (stream_id
< 0) {
1338 stream_id
= trace
->next_stream_id
++;
1339 if (stream_id
< 0) {
1340 BT_LOGE_STR("No more stream class IDs available.");
1345 /* Try to assign a new stream id */
1346 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1347 if (stream_id
== bt_stream_class_common_get_id(
1348 trace
->stream_classes
->pdata
[i
])) {
1349 /* Duplicate stream id found */
1350 BT_LOGW("Duplicate stream class ID: "
1351 "id=%" PRId64
, (int64_t) stream_id
);
1357 if (bt_stream_class_common_set_id_no_check(stream_class
,
1359 /* TODO Should retry with a different stream id */
1360 BT_LOGE("Cannot set stream class's ID: "
1361 "id=%" PRId64
, (int64_t) stream_id
);
1368 * At this point all the field types in the validation output
1369 * are valid. Validate the semantics of some scopes according to
1370 * the CTF specification.
1372 if (!packet_header_field_type_is_valid(trace
,
1373 trace_sc_validation_output
.packet_header_type
)) {
1374 BT_LOGW_STR("Invalid trace's packet header field type.");
1379 if (!packet_context_field_type_is_valid(trace
,
1381 trace_sc_validation_output
.packet_context_type
,
1382 check_ts_begin_end_mapped
)) {
1383 BT_LOGW_STR("Invalid stream class's packet context field type.");
1388 if (!event_header_field_type_is_valid(trace
,
1390 trace_sc_validation_output
.event_header_type
)) {
1391 BT_LOGW_STR("Invalid steam class's event header field type.");
1397 * Now is the time to automatically map specific field types of
1398 * the stream class's packet context and event header field
1399 * types to the stream class's clock's class if they are not
1400 * mapped to a clock class yet. We do it here because we know
1401 * that after this point, everything is frozen so it won't be
1402 * possible for the user to modify the stream class's clock, or
1403 * to map those field types to other clock classes.
1405 if (map_clock_classes_func
) {
1406 if (map_clock_classes_func(stream_class
,
1407 trace_sc_validation_output
.packet_context_type
,
1408 trace_sc_validation_output
.event_header_type
)) {
1409 /* map_clock_classes_func() logs errors */
1415 bt_object_set_parent(stream_class
, trace
);
1416 g_ptr_array_add(trace
->stream_classes
, stream_class
);
1419 * At this point we know that the function will be successful.
1420 * Therefore we can replace the trace and stream class field
1421 * types with what's in their validation output structure and
1422 * mark them as valid. We can also replace the field types of
1423 * all the event classes of the stream class and mark them as
1426 bt_validation_replace_types(trace
, stream_class
, NULL
,
1427 &trace_sc_validation_output
, trace_sc_validation_flags
);
1429 stream_class
->valid
= 1;
1432 * Put what was not moved in bt_validation_replace_types().
1434 bt_validation_output_put_types(&trace_sc_validation_output
);
1436 for (i
= 0; i
< event_class_count
; i
++) {
1437 struct bt_event_class_common
*event_class
=
1438 bt_stream_class_common_borrow_event_class_by_index(
1441 bt_validation_replace_types(NULL
, NULL
, event_class
,
1442 &ec_validation_outputs
[i
], ec_validation_flags
);
1443 event_class
->valid
= 1;
1446 * Put what was not moved in
1447 * bt_validation_replace_types().
1449 bt_validation_output_put_types(&ec_validation_outputs
[i
]);
1453 * Freeze the trace and the stream class.
1455 bt_stream_class_common_freeze(stream_class
);
1456 bt_trace_common_freeze(trace
);
1459 * It is safe to set the stream class's unique clock class
1460 * now because the stream class is frozen.
1462 if (expected_clock_class
) {
1463 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
1466 BT_LOGD("Added stream class to trace: "
1467 "trace-addr=%p, trace-name=\"%s\", "
1468 "stream-class-addr=%p, stream-class-name=\"%s\", "
1469 "stream-class-id=%" PRId64
,
1470 trace
, bt_trace_common_get_name(trace
),
1471 stream_class
, bt_stream_class_common_get_name(stream_class
),
1472 bt_stream_class_common_get_id(stream_class
));
1476 bt_object_set_parent(stream_class
, NULL
);
1478 if (ec_validation_outputs
) {
1479 for (i
= 0; i
< event_class_count
; i
++) {
1480 bt_validation_output_put_types(
1481 &ec_validation_outputs
[i
]);
1486 g_free(ec_validation_outputs
);
1487 bt_validation_output_put_types(&trace_sc_validation_output
);
1488 bt_put(expected_clock_class
);
1492 int bt_trace_add_stream_class(struct bt_trace
*trace
,
1493 struct bt_stream_class
*stream_class
)
1498 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1503 if (trace
->is_static
) {
1504 BT_LOGW_STR("Invalid parameter: trace is static.");
1509 ret
= bt_trace_common_add_stream_class(BT_TO_COMMON(trace
),
1510 BT_TO_COMMON(stream_class
),
1511 (bt_validation_flag_copy_field_type_func
) bt_field_type_copy
,
1517 /* Notifiy listeners of the trace's schema modification. */
1518 bt_stream_class_common_visit(BT_TO_COMMON(stream_class
),
1519 bt_trace_object_modification
, trace
);
1525 int64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
1527 return bt_trace_common_get_stream_count(BT_TO_COMMON(trace
));
1530 struct bt_stream
*bt_trace_borrow_stream_by_index(
1531 struct bt_trace
*trace
, uint64_t index
)
1533 return BT_FROM_COMMON(bt_trace_common_borrow_stream_by_index(
1534 BT_TO_COMMON(trace
), index
));
1537 int64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
1539 return bt_trace_common_get_stream_class_count(BT_TO_COMMON(trace
));
1542 struct bt_stream_class
*bt_trace_borrow_stream_class_by_index(
1543 struct bt_trace
*trace
, uint64_t index
)
1545 return BT_FROM_COMMON(bt_trace_common_borrow_stream_class_by_index(
1546 BT_TO_COMMON(trace
), index
));
1549 struct bt_stream_class
*bt_trace_borrow_stream_class_by_id(
1550 struct bt_trace
*trace
, uint64_t id
)
1552 return BT_FROM_COMMON(
1553 bt_trace_common_borrow_stream_class_by_id(
1554 BT_TO_COMMON(trace
), id
));
1557 struct bt_clock_class
*bt_trace_borrow_clock_class_by_name(
1558 struct bt_trace
*trace
, const char *name
)
1560 return bt_trace_common_borrow_clock_class_by_name(BT_TO_COMMON(trace
),
1565 bt_bool
bt_trace_common_has_clock_class(struct bt_trace_common
*trace
,
1566 struct bt_clock_class
*clock_class
)
1568 struct search_query query
= { .value
= clock_class
, .found
= 0 };
1571 BT_ASSERT(clock_class
);
1573 g_ptr_array_foreach(trace
->clock_classes
, value_exists
, &query
);
1577 enum bt_byte_order
bt_trace_get_native_byte_order(
1578 struct bt_trace
*trace
)
1580 return bt_trace_common_get_native_byte_order(BT_TO_COMMON(trace
));
1584 int bt_trace_common_set_native_byte_order(struct bt_trace_common
*trace
,
1585 enum bt_byte_order byte_order
, bool allow_unspecified
)
1590 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1595 if (trace
->frozen
) {
1596 BT_LOGW("Invalid parameter: trace is frozen: "
1597 "addr=%p, name=\"%s\"",
1598 trace
, bt_trace_common_get_name(trace
));
1603 if (byte_order
== BT_BYTE_ORDER_UNSPECIFIED
&& !allow_unspecified
) {
1604 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1605 "addr=%p, name=\"%s\"",
1606 trace
, bt_trace_common_get_name(trace
));
1611 if (byte_order
!= BT_BYTE_ORDER_LITTLE_ENDIAN
&&
1612 byte_order
!= BT_BYTE_ORDER_BIG_ENDIAN
&&
1613 byte_order
!= BT_BYTE_ORDER_NETWORK
) {
1614 BT_LOGW("Invalid parameter: invalid byte order: "
1615 "addr=%p, name=\"%s\", bo=%s",
1616 trace
, bt_trace_common_get_name(trace
),
1617 bt_common_byte_order_string(byte_order
));
1622 trace
->native_byte_order
= byte_order
;
1623 BT_LOGV("Set trace's native byte order: "
1624 "addr=%p, name=\"%s\", bo=%s",
1625 trace
, bt_trace_common_get_name(trace
),
1626 bt_common_byte_order_string(byte_order
));
1632 int bt_trace_set_native_byte_order(struct bt_trace
*trace
,
1633 enum bt_byte_order byte_order
)
1635 return bt_trace_common_set_native_byte_order(BT_TO_COMMON(trace
),
1639 struct bt_field_type
*bt_trace_borrow_packet_header_field_type(
1640 struct bt_trace
*trace
)
1642 return BT_FROM_COMMON(bt_trace_common_borrow_packet_header_field_type(
1643 BT_TO_COMMON(trace
)));
1647 int bt_trace_common_set_packet_header_field_type(struct bt_trace_common
*trace
,
1648 struct bt_field_type_common
*packet_header_type
)
1653 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1658 if (trace
->frozen
) {
1659 BT_LOGW("Invalid parameter: trace is frozen: "
1660 "addr=%p, name=\"%s\"",
1661 trace
, bt_trace_common_get_name(trace
));
1666 /* packet_header_type must be a structure. */
1667 if (packet_header_type
&&
1668 packet_header_type
->id
!= BT_FIELD_TYPE_ID_STRUCT
) {
1669 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1670 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1671 trace
, bt_trace_common_get_name(trace
),
1673 bt_common_field_type_id_string(packet_header_type
->id
));
1678 bt_put(trace
->packet_header_field_type
);
1679 trace
->packet_header_field_type
= bt_get(packet_header_type
);
1680 BT_LOGV("Set trace's packet header field type: "
1681 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1682 trace
, bt_trace_common_get_name(trace
), packet_header_type
);
1687 int bt_trace_set_packet_header_field_type(struct bt_trace
*trace
,
1688 struct bt_field_type
*packet_header_type
)
1690 return bt_trace_common_set_packet_header_field_type(BT_TO_COMMON(trace
),
1691 (void *) packet_header_type
);
1695 int64_t get_stream_class_count(void *element
)
1697 return bt_trace_get_stream_class_count(
1698 (struct bt_trace
*) element
);
1702 void *get_stream_class(void *element
, int i
)
1704 return bt_trace_get_stream_class_by_index(
1705 (struct bt_trace
*) element
, i
);
1709 int visit_stream_class(void *object
, bt_visitor visitor
,void *data
)
1711 return bt_stream_class_visit(object
, visitor
, data
);
1714 int bt_trace_visit(struct bt_trace
*trace
,
1715 bt_visitor visitor
, void *data
)
1718 struct bt_visitor_object obj
= {
1720 .type
= BT_VISITOR_OBJECT_TYPE_TRACE
1724 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1730 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1735 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1736 trace
, bt_trace_get_name(trace
));
1737 ret
= visitor_helper(&obj
, get_stream_class_count
,
1738 get_stream_class
, visit_stream_class
, visitor
, data
);
1744 int invoke_listener(struct bt_visitor_object
*object
, void *data
)
1746 struct listener_wrapper
*listener_wrapper
= data
;
1748 listener_wrapper
->listener(object
, listener_wrapper
->data
);
1752 // TODO: add logging to this function once we use it internally.
1753 int bt_trace_add_listener(struct bt_trace
*trace
,
1754 bt_listener_cb listener
, void *listener_data
)
1757 struct listener_wrapper
*listener_wrapper
=
1758 g_new0(struct listener_wrapper
, 1);
1760 if (!trace
|| !listener
|| !listener_wrapper
) {
1765 listener_wrapper
->listener
= listener
;
1766 listener_wrapper
->data
= listener_data
;
1768 /* Visit the current schema. */
1769 ret
= bt_trace_visit(trace
, invoke_listener
, listener_wrapper
);
1775 * Add listener to the array of callbacks which will be invoked on
1778 g_ptr_array_add(trace
->listeners
, listener_wrapper
);
1781 g_free(listener_wrapper
);
1786 int bt_trace_object_modification(struct bt_visitor_object
*object
,
1790 struct bt_trace
*trace
= trace_ptr
;
1795 if (trace
->listeners
->len
== 0) {
1799 for (i
= 0; i
< trace
->listeners
->len
; i
++) {
1800 struct listener_wrapper
*listener
=
1801 g_ptr_array_index(trace
->listeners
, i
);
1803 listener
->listener(object
, listener
->data
);
1809 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
1811 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1812 return trace
->is_static
;
1815 int bt_trace_set_is_static(struct bt_trace
*trace
)
1821 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1826 ret
= check_packet_header_type_has_no_clock_class(BT_TO_COMMON(trace
));
1828 /* check_packet_header_type_has_no_clock_class() logs errors */
1832 trace
->is_static
= BT_TRUE
;
1833 bt_trace_common_freeze(BT_TO_COMMON(trace
));
1834 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
1835 trace
, bt_trace_get_name(trace
));
1837 /* Call all the "trace is static" listeners */
1838 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
1839 struct bt_trace_is_static_listener_elem elem
=
1840 g_array_index(trace
->is_static_listeners
,
1841 struct bt_trace_is_static_listener_elem
, i
);
1844 elem
.func(trace
, elem
.data
);
1852 int bt_trace_add_is_static_listener(struct bt_trace
*trace
,
1853 bt_trace_is_static_listener listener
,
1854 bt_trace_listener_removed listener_removed
, void *data
)
1857 struct bt_trace_is_static_listener_elem new_elem
= {
1859 .removed
= listener_removed
,
1864 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1870 BT_LOGW_STR("Invalid parameter: listener is NULL.");
1875 if (trace
->is_static
) {
1876 BT_LOGW("Invalid parameter: trace is already static: "
1877 "addr=%p, name=\"%s\"",
1878 trace
, bt_trace_get_name(trace
));
1883 if (trace
->in_remove_listener
) {
1884 BT_LOGW("Cannot call this function during the execution of a remove listener: "
1885 "addr=%p, name=\"%s\"",
1886 trace
, bt_trace_get_name(trace
));
1891 /* Find the next available spot */
1892 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
1893 struct bt_trace_is_static_listener_elem elem
=
1894 g_array_index(trace
->is_static_listeners
,
1895 struct bt_trace_is_static_listener_elem
, i
);
1902 if (i
== trace
->is_static_listeners
->len
) {
1903 g_array_append_val(trace
->is_static_listeners
, new_elem
);
1905 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
1908 BT_LOGV("Added \"trace is static\" listener: "
1909 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
1910 "data-addr=%p, listener-id=%d",
1911 trace
, bt_trace_get_name(trace
), listener
, data
, i
);
1917 int bt_trace_remove_is_static_listener(
1918 struct bt_trace
*trace
, int listener_id
)
1921 struct bt_trace_is_static_listener_elem
*elem
;
1924 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1929 if (trace
->in_remove_listener
) {
1930 BT_LOGW("Cannot call this function during the execution of a remove listener: "
1931 "addr=%p, name=\"%s\", listener-id=%d",
1932 trace
, bt_trace_get_name(trace
),
1938 if (listener_id
< 0) {
1939 BT_LOGW("Invalid listener ID: must be zero or positive: "
1940 "listener-id=%d", listener_id
);
1945 if (listener_id
>= trace
->is_static_listeners
->len
) {
1946 BT_LOGW("Invalid parameter: no listener with this listener ID: "
1947 "addr=%p, name=\"%s\", listener-id=%d",
1948 trace
, bt_trace_get_name(trace
),
1954 elem
= &g_array_index(trace
->is_static_listeners
,
1955 struct bt_trace_is_static_listener_elem
,
1958 BT_LOGW("Invalid parameter: no listener with this listener ID: "
1959 "addr=%p, name=\"%s\", listener-id=%d",
1960 trace
, bt_trace_get_name(trace
),
1966 if (elem
->removed
) {
1967 /* Call remove listener */
1968 BT_LOGV("Calling remove listener: "
1969 "trace-addr=%p, trace-name=\"%s\", "
1970 "listener-id=%d", trace
, bt_trace_get_name(trace
),
1972 trace
->in_remove_listener
= BT_TRUE
;
1973 elem
->removed(trace
, elem
->data
);
1974 trace
->in_remove_listener
= BT_FALSE
;
1978 elem
->removed
= NULL
;
1980 BT_LOGV("Removed \"trace is static\" listener: "
1981 "trace-addr=%p, trace-name=\"%s\", "
1982 "listener-id=%d", trace
, bt_trace_get_name(trace
),
1989 struct bt_packet_header_field
*bt_trace_create_packet_header_field(
1990 struct bt_trace
*trace
)
1992 struct bt_field_wrapper
*field_wrapper
;
1994 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
1995 BT_ASSERT_PRE(trace
->common
.packet_header_field_type
,
1996 "Trace has no packet header field type: %!+t",
1998 field_wrapper
= bt_field_wrapper_create(
1999 &trace
->packet_header_field_pool
,
2000 (void *) trace
->common
.packet_header_field_type
);
2001 if (!field_wrapper
) {
2002 BT_LIB_LOGE("Cannot allocate one packet header field from trace: "
2003 "%![trace-]+t", trace
);
2007 BT_ASSERT(field_wrapper
->field
);
2008 bt_trace_common_freeze(BT_TO_COMMON(trace
));
2012 if (field_wrapper
) {
2013 bt_field_wrapper_destroy(field_wrapper
);
2014 field_wrapper
= NULL
;
2018 return (void *) field_wrapper
;