5 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
18 #include <babeltrace/babeltrace.h>
19 #include <babeltrace/common-internal.h>
20 #include <babeltrace/assert-internal.h>
25 enum ctf_field_class_type
{
26 CTF_FIELD_CLASS_TYPE_INT
,
27 CTF_FIELD_CLASS_TYPE_ENUM
,
28 CTF_FIELD_CLASS_TYPE_FLOAT
,
29 CTF_FIELD_CLASS_TYPE_STRING
,
30 CTF_FIELD_CLASS_TYPE_STRUCT
,
31 CTF_FIELD_CLASS_TYPE_ARRAY
,
32 CTF_FIELD_CLASS_TYPE_SEQUENCE
,
33 CTF_FIELD_CLASS_TYPE_VARIANT
,
36 enum ctf_field_class_meaning
{
37 CTF_FIELD_CLASS_MEANING_NONE
,
38 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
,
39 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
,
40 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
,
41 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
,
42 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
,
43 CTF_FIELD_CLASS_MEANING_MAGIC
,
44 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
,
45 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
,
46 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
,
47 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
,
48 CTF_FIELD_CLASS_MEANING_UUID
,
52 CTF_BYTE_ORDER_DEFAULT
,
53 CTF_BYTE_ORDER_LITTLE
,
62 struct ctf_field_class
{
63 enum ctf_field_class_type type
;
64 unsigned int alignment
;
68 /* Weak, set during translation. NULL if `in_ir` is false below. */
69 struct bt_private_field_class
*ir_fc
;
72 struct ctf_field_class_bit_array
{
73 struct ctf_field_class base
;
74 enum ctf_byte_order byte_order
;
78 struct ctf_field_class_int
{
79 struct ctf_field_class_bit_array base
;
80 enum ctf_field_class_meaning meaning
;
82 enum bt_field_class_integer_preferred_display_base disp_base
;
83 enum ctf_encoding encoding
;
84 int64_t storing_index
;
87 struct bt_private_clock_class
*mapped_clock_class
;
102 struct ctf_field_class_enum_mapping
{
104 struct ctf_range range
;
107 struct ctf_field_class_enum
{
108 struct ctf_field_class_int base
;
110 /* Array of `struct ctf_field_class_enum_mapping` */
114 struct ctf_field_class_float
{
115 struct ctf_field_class_bit_array base
;
118 struct ctf_field_class_string
{
119 struct ctf_field_class base
;
120 enum ctf_encoding encoding
;
123 struct ctf_named_field_class
{
127 struct ctf_field_class
*fc
;
130 struct ctf_field_class_struct
{
131 struct ctf_field_class base
;
133 /* Array of `struct ctf_named_field_class` */
137 struct ctf_field_path
{
140 /* Array of `int64_t` */
144 struct ctf_field_class_variant_range
{
145 struct ctf_range range
;
146 uint64_t option_index
;
149 struct ctf_field_class_variant
{
150 struct ctf_field_class base
;
152 struct ctf_field_path tag_path
;
153 uint64_t stored_tag_index
;
155 /* Array of `struct ctf_named_field_class` */
158 /* Array of `struct ctf_field_class_variant_range` */
162 struct ctf_field_class_enum
*tag_fc
;
165 struct ctf_field_class_array_base
{
166 struct ctf_field_class base
;
167 struct ctf_field_class
*elem_fc
;
171 struct ctf_field_class_array
{
172 struct ctf_field_class_array_base base
;
173 enum ctf_field_class_meaning meaning
;
177 struct ctf_field_class_sequence
{
178 struct ctf_field_class_array_base base
;
180 struct ctf_field_path length_path
;
181 uint64_t stored_length_index
;
184 struct ctf_field_class_int
*length_fc
;
187 struct ctf_event_class
{
191 enum bt_event_class_log_level log_level
;
195 struct ctf_field_class
*spec_context_fc
;
198 struct ctf_field_class
*payload_fc
;
200 /* Weak, set during translation */
201 struct bt_private_event_class
*ir_ec
;
204 struct ctf_stream_class
{
209 struct ctf_field_class
*packet_context_fc
;
212 struct ctf_field_class
*event_header_fc
;
215 struct ctf_field_class
*event_common_context_fc
;
217 /* Array of `struct ctf_event_class *`, owned by this */
218 GPtrArray
*event_classes
;
221 * Hash table mapping event class IDs to `struct ctf_event_class *`,
224 GHashTable
*event_classes_by_id
;
227 struct bt_private_clock_class
*default_clock_class
;
229 /* Weak, set during translation */
230 struct bt_private_stream_class
*ir_sc
;
233 enum ctf_trace_class_env_entry_type
{
234 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
235 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
238 struct ctf_trace_class_env_entry
{
239 enum ctf_trace_class_env_entry_type type
;
248 struct ctf_trace_class
{
254 enum ctf_byte_order default_byte_order
;
257 struct ctf_field_class
*packet_header_fc
;
259 uint64_t stored_value_count
;
261 /* Array of `struct bt_private_clock_class *` (owned by this) */
262 GPtrArray
*clock_classes
;
264 /* Array of `struct ctf_stream_class *` */
265 GPtrArray
*stream_classes
;
267 /* Array of `struct ctf_trace_class_env_entry` */
272 /* Weak, set during translation */
273 struct bt_private_trace
*ir_tc
;
277 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
280 void _ctf_field_class_init(struct ctf_field_class
*fc
,
281 enum ctf_field_class_type type
, unsigned int alignment
)
285 fc
->alignment
= alignment
;
290 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
291 enum ctf_field_class_type type
)
293 _ctf_field_class_init((void *) fc
, type
, 1);
297 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
298 enum ctf_field_class_type type
)
300 _ctf_field_class_bit_array_init((void *) fc
, type
);
301 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
302 fc
->storing_index
= -1;
306 void ctf_field_path_init(struct ctf_field_path
*field_path
)
308 BT_ASSERT(field_path
);
309 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
310 BT_ASSERT(field_path
->path
);
314 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
316 BT_ASSERT(field_path
);
318 if (field_path
->path
) {
319 g_array_free(field_path
->path
, TRUE
);
324 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
327 named_fc
->name
= g_string_new(NULL
);
328 BT_ASSERT(named_fc
->name
);
332 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
336 if (named_fc
->name
) {
337 g_string_free(named_fc
->name
, TRUE
);
340 ctf_field_class_destroy(named_fc
->fc
);
344 void _ctf_field_class_enum_mapping_init(
345 struct ctf_field_class_enum_mapping
*mapping
)
348 mapping
->label
= g_string_new(NULL
);
349 BT_ASSERT(mapping
->label
);
353 void _ctf_field_class_enum_mapping_fini(
354 struct ctf_field_class_enum_mapping
*mapping
)
358 if (mapping
->label
) {
359 g_string_free(mapping
->label
, TRUE
);
364 struct ctf_field_class_int
*ctf_field_class_int_create(void)
366 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
369 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
374 struct ctf_field_class_float
*ctf_field_class_float_create(void)
376 struct ctf_field_class_float
*fc
=
377 g_new0(struct ctf_field_class_float
, 1);
380 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
385 struct ctf_field_class_string
*ctf_field_class_string_create(void)
387 struct ctf_field_class_string
*fc
=
388 g_new0(struct ctf_field_class_string
, 1);
391 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
396 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
398 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
401 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
402 fc
->mappings
= g_array_new(FALSE
, TRUE
,
403 sizeof(struct ctf_field_class_enum_mapping
));
404 BT_ASSERT(fc
->mappings
);
409 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
411 struct ctf_field_class_struct
*fc
=
412 g_new0(struct ctf_field_class_struct
, 1);
415 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
416 fc
->members
= g_array_new(FALSE
, TRUE
,
417 sizeof(struct ctf_named_field_class
));
418 BT_ASSERT(fc
->members
);
419 fc
->base
.is_compound
= true;
424 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
426 struct ctf_field_class_variant
*fc
=
427 g_new0(struct ctf_field_class_variant
, 1);
430 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
431 fc
->options
= g_array_new(FALSE
, TRUE
,
432 sizeof(struct ctf_named_field_class
));
433 BT_ASSERT(fc
->options
);
434 fc
->ranges
= g_array_new(FALSE
, TRUE
,
435 sizeof(struct ctf_field_class_variant_range
));
436 BT_ASSERT(fc
->ranges
);
437 fc
->tag_ref
= g_string_new(NULL
);
438 BT_ASSERT(fc
->tag_ref
);
439 ctf_field_path_init(&fc
->tag_path
);
440 fc
->base
.is_compound
= true;
445 struct ctf_field_class_array
*ctf_field_class_array_create(void)
447 struct ctf_field_class_array
*fc
=
448 g_new0(struct ctf_field_class_array
, 1);
451 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
452 fc
->base
.base
.is_compound
= true;
457 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
459 struct ctf_field_class_sequence
*fc
=
460 g_new0(struct ctf_field_class_sequence
, 1);
463 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
464 fc
->length_ref
= g_string_new(NULL
);
465 BT_ASSERT(fc
->length_ref
);
466 ctf_field_path_init(&fc
->length_path
);
467 fc
->base
.base
.is_compound
= true;
472 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
475 bt_object_put_ref(fc
->mapped_clock_class
);
480 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
483 bt_object_put_ref(fc
->base
.mapped_clock_class
);
488 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
489 struct ctf_field_class_enum_mapping
*mapping
=
490 &g_array_index(fc
->mappings
,
491 struct ctf_field_class_enum_mapping
, i
);
493 _ctf_field_class_enum_mapping_fini(mapping
);
496 g_array_free(fc
->mappings
, TRUE
);
503 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
510 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
517 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
524 for (i
= 0; i
< fc
->members
->len
; i
++) {
525 struct ctf_named_field_class
*named_fc
=
526 &g_array_index(fc
->members
,
527 struct ctf_named_field_class
, i
);
529 _ctf_named_field_class_fini(named_fc
);
532 g_array_free(fc
->members
, TRUE
);
539 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
542 ctf_field_class_destroy(fc
->elem_fc
);
546 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
549 _ctf_field_class_array_base_fini((void *) fc
);
554 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
557 _ctf_field_class_array_base_fini((void *) fc
);
559 if (fc
->length_ref
) {
560 g_string_free(fc
->length_ref
, TRUE
);
563 ctf_field_path_fini(&fc
->length_path
);
568 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
575 for (i
= 0; i
< fc
->options
->len
; i
++) {
576 struct ctf_named_field_class
*named_fc
=
577 &g_array_index(fc
->options
,
578 struct ctf_named_field_class
, i
);
580 _ctf_named_field_class_fini(named_fc
);
583 g_array_free(fc
->options
, TRUE
);
587 g_array_free(fc
->ranges
, TRUE
);
591 g_string_free(fc
->tag_ref
, TRUE
);
594 ctf_field_path_fini(&fc
->tag_path
);
599 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
606 case CTF_FIELD_CLASS_TYPE_INT
:
607 _ctf_field_class_int_destroy((void *) fc
);
609 case CTF_FIELD_CLASS_TYPE_ENUM
:
610 _ctf_field_class_enum_destroy((void *) fc
);
612 case CTF_FIELD_CLASS_TYPE_FLOAT
:
613 _ctf_field_class_float_destroy((void *) fc
);
615 case CTF_FIELD_CLASS_TYPE_STRING
:
616 _ctf_field_class_string_destroy((void *) fc
);
618 case CTF_FIELD_CLASS_TYPE_STRUCT
:
619 _ctf_field_class_struct_destroy((void *) fc
);
621 case CTF_FIELD_CLASS_TYPE_ARRAY
:
622 _ctf_field_class_array_destroy((void *) fc
);
624 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
625 _ctf_field_class_sequence_destroy((void *) fc
);
627 case CTF_FIELD_CLASS_TYPE_VARIANT
:
628 _ctf_field_class_variant_destroy((void *) fc
);
636 void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum
*fc
,
637 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
639 struct ctf_field_class_enum_mapping
*mapping
;
643 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
645 mapping
= &g_array_index(fc
->mappings
,
646 struct ctf_field_class_enum_mapping
, fc
->mappings
->len
- 1);
647 _ctf_field_class_enum_mapping_init(mapping
);
648 g_string_assign(mapping
->label
, label
);
649 mapping
->range
.lower
.u
= u_lower
;
650 mapping
->range
.upper
.u
= u_upper
;
654 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
655 struct ctf_field_class_enum
*fc
, uint64_t index
)
658 BT_ASSERT(index
< fc
->mappings
->len
);
659 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
664 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
665 struct ctf_field_class_struct
*fc
, uint64_t index
)
668 BT_ASSERT(index
< fc
->members
->len
);
669 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
674 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
675 struct ctf_field_class_struct
*fc
, const char *name
)
678 struct ctf_named_field_class
*ret_named_fc
= NULL
;
683 for (i
= 0; i
< fc
->members
->len
; i
++) {
684 struct ctf_named_field_class
*named_fc
=
685 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
687 if (strcmp(name
, named_fc
->name
->str
) == 0) {
688 ret_named_fc
= named_fc
;
698 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
699 struct ctf_field_class_struct
*struct_fc
, const char *name
)
701 struct ctf_named_field_class
*named_fc
= NULL
;
702 struct ctf_field_class
*fc
= NULL
;
708 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
720 struct ctf_field_class_int
*
721 ctf_field_class_struct_borrow_member_int_field_class_by_name(
722 struct ctf_field_class_struct
*struct_fc
, const char *name
)
724 struct ctf_field_class_int
*int_fc
= NULL
;
727 ctf_field_class_struct_borrow_member_field_class_by_name(
733 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
734 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
745 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
746 const char *name
, struct ctf_field_class
*member_fc
)
748 struct ctf_named_field_class
*named_fc
;
752 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
754 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
755 fc
->members
->len
- 1);
756 _ctf_named_field_class_init(named_fc
);
757 g_string_assign(named_fc
->name
, name
);
758 named_fc
->fc
= member_fc
;
760 if (member_fc
->alignment
> fc
->base
.alignment
) {
761 fc
->base
.alignment
= member_fc
->alignment
;
766 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
767 struct ctf_field_class_variant
*fc
, uint64_t index
)
770 BT_ASSERT(index
< fc
->options
->len
);
771 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
776 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
777 struct ctf_field_class_variant
*fc
, const char *name
)
780 struct ctf_named_field_class
*ret_named_fc
= NULL
;
785 for (i
= 0; i
< fc
->options
->len
; i
++) {
786 struct ctf_named_field_class
*named_fc
=
787 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
789 if (strcmp(name
, named_fc
->name
->str
) == 0) {
790 ret_named_fc
= named_fc
;
800 struct ctf_field_class_variant_range
*
801 ctf_field_class_variant_borrow_range_by_index(
802 struct ctf_field_class_variant
*fc
, uint64_t index
)
805 BT_ASSERT(index
< fc
->ranges
->len
);
806 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
811 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
812 const char *name
, struct ctf_field_class
*option_fc
)
814 struct ctf_named_field_class
*named_fc
;
818 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
820 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
821 fc
->options
->len
- 1);
822 _ctf_named_field_class_init(named_fc
);
823 g_string_assign(named_fc
->name
, name
);
824 named_fc
->fc
= option_fc
;
828 void ctf_field_class_variant_set_tag_field_class(
829 struct ctf_field_class_variant
*fc
,
830 struct ctf_field_class_enum
*tag_fc
)
838 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
840 struct ctf_named_field_class
*named_fc
=
841 ctf_field_class_variant_borrow_option_by_index(
844 for (mapping_i
= 0; mapping_i
< tag_fc
->mappings
->len
;
846 struct ctf_field_class_enum_mapping
*mapping
=
847 ctf_field_class_enum_borrow_mapping_by_index(
850 if (strcmp(named_fc
->name
->str
,
851 mapping
->label
->str
) == 0) {
852 struct ctf_field_class_variant_range range
;
854 range
.range
= mapping
->range
;
855 range
.option_index
= option_i
;
856 g_array_append_val(fc
->ranges
, range
);
863 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
864 struct ctf_field_class
*comp_fc
, uint64_t index
)
866 struct ctf_field_class
*fc
= NULL
;
868 switch (comp_fc
->type
) {
869 case CTF_FIELD_CLASS_TYPE_STRUCT
:
871 struct ctf_named_field_class
*named_fc
=
872 ctf_field_class_struct_borrow_member_by_index(
873 (void *) comp_fc
, index
);
879 case CTF_FIELD_CLASS_TYPE_VARIANT
:
881 struct ctf_named_field_class
*named_fc
=
882 ctf_field_class_variant_borrow_option_by_index(
883 (void *) comp_fc
, index
);
889 case CTF_FIELD_CLASS_TYPE_ARRAY
:
890 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
892 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
894 fc
= array_fc
->elem_fc
;
905 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
907 uint64_t field_count
;
910 case CTF_FIELD_CLASS_TYPE_STRUCT
:
912 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
914 field_count
= struct_fc
->members
->len
;
917 case CTF_FIELD_CLASS_TYPE_VARIANT
:
919 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
921 field_count
= var_fc
->options
->len
;
924 case CTF_FIELD_CLASS_TYPE_ARRAY
:
925 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
927 * Array and sequence types always contain a single
928 * member (the element type).
940 int64_t ctf_field_class_compound_get_field_class_index_from_name(
941 struct ctf_field_class
*fc
, const char *name
)
943 int64_t ret_index
= -1;
947 case CTF_FIELD_CLASS_TYPE_STRUCT
:
949 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
951 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
952 struct ctf_named_field_class
*named_fc
=
953 ctf_field_class_struct_borrow_member_by_index(
956 if (strcmp(name
, named_fc
->name
->str
) == 0) {
957 ret_index
= (int64_t) i
;
964 case CTF_FIELD_CLASS_TYPE_VARIANT
:
966 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
968 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
969 struct ctf_named_field_class
*named_fc
=
970 ctf_field_class_variant_borrow_option_by_index(
973 if (strcmp(name
, named_fc
->name
->str
) == 0) {
974 ret_index
= (int64_t) i
;
990 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
993 g_array_append_val(fp
->path
, index
);
997 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1001 BT_ASSERT(index
< fp
->path
->len
);
1002 return g_array_index(fp
->path
, int64_t, index
);
1006 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1009 g_array_set_size(fp
->path
, 0);
1013 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1015 GString
*str
= g_string_new(NULL
);
1024 g_string_append_printf(str
, "[%s", bt_common_scope_string(
1027 for (i
= 0; i
< path
->path
->len
; i
++) {
1028 g_string_append_printf(str
, ", %" PRId64
,
1029 ctf_field_path_borrow_index_by_index(path
, i
));
1032 g_string_append(str
, "]");
1039 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1040 struct ctf_field_path
*field_path
,
1041 struct ctf_trace_class
*tc
,
1042 struct ctf_stream_class
*sc
,
1043 struct ctf_event_class
*ec
)
1046 struct ctf_field_class
*fc
;
1048 switch (field_path
->root
) {
1049 case BT_SCOPE_PACKET_HEADER
:
1050 fc
= tc
->packet_header_fc
;
1052 case BT_SCOPE_PACKET_CONTEXT
:
1053 fc
= sc
->packet_context_fc
;
1055 case BT_SCOPE_EVENT_HEADER
:
1056 fc
= sc
->event_header_fc
;
1058 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
1059 fc
= sc
->event_common_context_fc
;
1061 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1062 fc
= ec
->spec_context_fc
;
1064 case BT_SCOPE_EVENT_PAYLOAD
:
1065 fc
= ec
->payload_fc
;
1073 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1074 int64_t child_index
=
1075 ctf_field_path_borrow_index_by_index(field_path
, i
);
1076 struct ctf_field_class
*child_fc
=
1077 ctf_field_class_compound_borrow_field_class_by_index(
1079 BT_ASSERT(child_fc
);
1088 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1091 void ctf_field_class_bit_array_copy_content(
1092 struct ctf_field_class_bit_array
*dst_fc
,
1093 struct ctf_field_class_bit_array
*src_fc
)
1097 dst_fc
->byte_order
= src_fc
->byte_order
;
1098 dst_fc
->size
= src_fc
->size
;
1102 void ctf_field_class_int_copy_content(
1103 struct ctf_field_class_int
*dst_fc
,
1104 struct ctf_field_class_int
*src_fc
)
1106 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1107 dst_fc
->meaning
= src_fc
->meaning
;
1108 dst_fc
->is_signed
= src_fc
->is_signed
;
1109 dst_fc
->disp_base
= src_fc
->disp_base
;
1110 dst_fc
->encoding
= src_fc
->encoding
;
1111 dst_fc
->mapped_clock_class
= bt_object_get_ref(src_fc
->mapped_clock_class
);
1112 dst_fc
->storing_index
= src_fc
->storing_index
;
1116 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1117 struct ctf_field_class_int
*fc
)
1119 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1122 ctf_field_class_int_copy_content(copy_fc
, fc
);
1127 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1128 struct ctf_field_class_enum
*fc
)
1130 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1134 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1136 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1137 struct ctf_field_class_enum_mapping
*mapping
=
1138 &g_array_index(fc
->mappings
,
1139 struct ctf_field_class_enum_mapping
, i
);
1141 ctf_field_class_enum_append_mapping(copy_fc
, mapping
->label
->str
,
1142 mapping
->range
.lower
.u
, mapping
->range
.upper
.u
);
1149 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1150 struct ctf_field_class_float
*fc
)
1152 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1155 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1160 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1161 struct ctf_field_class_string
*fc
)
1163 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1170 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1171 struct ctf_field_class_struct
*fc
)
1173 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1178 for (i
= 0; i
< fc
->members
->len
; i
++) {
1179 struct ctf_named_field_class
*named_fc
=
1180 &g_array_index(fc
->members
,
1181 struct ctf_named_field_class
, i
);
1183 ctf_field_class_struct_append_member(copy_fc
,
1184 named_fc
->name
->str
,
1185 ctf_field_class_copy(named_fc
->fc
));
1192 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1193 struct ctf_field_path
*src_fp
)
1199 dst_fp
->root
= src_fp
->root
;
1200 ctf_field_path_clear(dst_fp
);
1202 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1203 int64_t index
= ctf_field_path_borrow_index_by_index(
1206 ctf_field_path_append_index(dst_fp
, index
);
1211 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1212 struct ctf_field_class_variant
*fc
)
1214 struct ctf_field_class_variant
*copy_fc
=
1215 ctf_field_class_variant_create();
1220 for (i
= 0; i
< fc
->options
->len
; i
++) {
1221 struct ctf_named_field_class
*named_fc
=
1222 &g_array_index(fc
->options
,
1223 struct ctf_named_field_class
, i
);
1225 ctf_field_class_variant_append_option(copy_fc
,
1226 named_fc
->name
->str
,
1227 ctf_field_class_copy(named_fc
->fc
));
1230 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1231 struct ctf_field_class_variant_range
*range
=
1232 &g_array_index(fc
->ranges
,
1233 struct ctf_field_class_variant_range
, i
);
1235 g_array_append_val(copy_fc
->ranges
, *range
);
1238 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1239 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1240 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1245 void ctf_field_class_array_base_copy_content(
1246 struct ctf_field_class_array_base
*dst_fc
,
1247 struct ctf_field_class_array_base
*src_fc
)
1251 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1252 dst_fc
->is_text
= src_fc
->is_text
;
1256 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1257 struct ctf_field_class_array
*fc
)
1259 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1262 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1263 copy_fc
->length
= fc
->length
;
1268 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1269 struct ctf_field_class_sequence
*fc
)
1271 struct ctf_field_class_sequence
*copy_fc
=
1272 ctf_field_class_sequence_create();
1275 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1276 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1277 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1278 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1283 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1285 struct ctf_field_class
*copy_fc
= NULL
;
1292 * Translation should not have happened yet.
1294 BT_ASSERT(!fc
->ir_fc
);
1297 case CTF_FIELD_CLASS_TYPE_INT
:
1298 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1300 case CTF_FIELD_CLASS_TYPE_ENUM
:
1301 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1303 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1304 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1306 case CTF_FIELD_CLASS_TYPE_STRING
:
1307 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1309 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1310 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1312 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1313 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1315 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1316 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1318 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1319 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1325 copy_fc
->type
= fc
->type
;
1326 copy_fc
->alignment
= fc
->alignment
;
1327 copy_fc
->in_ir
= fc
->in_ir
;
1334 struct ctf_event_class
*ctf_event_class_create(void)
1336 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1339 ec
->name
= g_string_new(NULL
);
1340 BT_ASSERT(ec
->name
);
1341 ec
->emf_uri
= g_string_new(NULL
);
1342 BT_ASSERT(ec
->emf_uri
);
1348 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1355 g_string_free(ec
->name
, TRUE
);
1359 g_string_free(ec
->emf_uri
, TRUE
);
1362 ctf_field_class_destroy(ec
->spec_context_fc
);
1363 ctf_field_class_destroy(ec
->payload_fc
);
1368 struct ctf_stream_class
*ctf_stream_class_create(void)
1370 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1373 sc
->event_classes
= g_ptr_array_new_with_free_func(
1374 (GDestroyNotify
) ctf_event_class_destroy
);
1375 BT_ASSERT(sc
->event_classes
);
1376 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1378 BT_ASSERT(sc
->event_classes_by_id
);
1383 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1389 if (sc
->event_classes
) {
1390 g_ptr_array_free(sc
->event_classes
, TRUE
);
1393 if (sc
->event_classes_by_id
) {
1394 g_hash_table_destroy(sc
->event_classes_by_id
);
1397 ctf_field_class_destroy(sc
->packet_context_fc
);
1398 ctf_field_class_destroy(sc
->event_header_fc
);
1399 ctf_field_class_destroy(sc
->event_common_context_fc
);
1400 bt_object_put_ref(sc
->default_clock_class
);
1405 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1406 struct ctf_event_class
*ec
)
1408 g_ptr_array_add(sc
->event_classes
, ec
);
1409 g_hash_table_insert(sc
->event_classes_by_id
,
1410 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1414 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1415 struct ctf_stream_class
*sc
, uint64_t type
)
1418 return g_hash_table_lookup(sc
->event_classes_by_id
,
1419 GUINT_TO_POINTER((guint
) type
));
1423 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1426 entry
->name
= g_string_new(NULL
);
1427 BT_ASSERT(entry
->name
);
1428 entry
->value
.str
= g_string_new(NULL
);
1429 BT_ASSERT(entry
->value
.str
);
1433 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1438 g_string_free(entry
->name
, TRUE
);
1441 if (entry
->value
.str
) {
1442 g_string_free(entry
->value
.str
, TRUE
);
1447 struct ctf_trace_class
*ctf_trace_class_create(void)
1449 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1452 tc
->name
= g_string_new(NULL
);
1453 tc
->default_byte_order
= -1;
1454 BT_ASSERT(tc
->name
);
1455 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1456 (GDestroyNotify
) bt_object_put_ref
);
1457 BT_ASSERT(tc
->clock_classes
);
1458 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1459 (GDestroyNotify
) ctf_stream_class_destroy
);
1460 BT_ASSERT(tc
->stream_classes
);
1461 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1462 sizeof(struct ctf_trace_class_env_entry
));
1467 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1474 g_string_free(tc
->name
, TRUE
);
1477 ctf_field_class_destroy(tc
->packet_header_fc
);
1479 if (tc
->clock_classes
) {
1480 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1483 if (tc
->stream_classes
) {
1484 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1487 if (tc
->env_entries
) {
1490 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1491 struct ctf_trace_class_env_entry
*entry
=
1492 &g_array_index(tc
->env_entries
,
1493 struct ctf_trace_class_env_entry
, i
);
1495 _ctf_trace_class_env_entry_fini(entry
);
1498 g_array_free(tc
->env_entries
, TRUE
);
1505 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1506 const char *name
, enum ctf_trace_class_env_entry_type type
,
1507 const char *str_value
, int64_t i_value
)
1509 struct ctf_trace_class_env_entry
*entry
;
1513 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1515 entry
= &g_array_index(tc
->env_entries
,
1516 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1518 _ctf_trace_class_env_entry_init(entry
);
1519 g_string_assign(entry
->name
, name
);
1522 g_string_assign(entry
->value
.str
, str_value
);
1525 entry
->value
.i
= i_value
;
1529 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1530 struct ctf_trace_class
*tc
, uint64_t id
)
1533 struct ctf_stream_class
*ret_sc
= NULL
;
1537 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1538 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1551 struct bt_private_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1552 struct ctf_trace_class
*tc
, const char *name
)
1555 struct bt_private_clock_class
*ret_cc
= NULL
;
1560 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1561 struct bt_private_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1562 const char *cc_name
= bt_clock_class_get_name(
1563 bt_private_clock_class_borrow_clock_class(cc
));
1566 if (strcmp(cc_name
, name
) == 0) {
1577 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1578 struct ctf_trace_class
*tc
, uint64_t index
)
1581 BT_ASSERT(index
< tc
->env_entries
->len
);
1582 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1587 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1588 struct ctf_trace_class
*tc
, const char *name
)
1590 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1596 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1597 struct ctf_trace_class_env_entry
*env_entry
=
1598 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1600 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1601 ret_entry
= env_entry
;
1610 #endif /* _CTF_META_H */