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_clock_class
{
67 int64_t offset_seconds
;
68 uint64_t offset_cycles
;
73 /* Weak, set during translation */
74 bt_clock_class
*ir_cc
;
77 struct ctf_field_class
{
78 enum ctf_field_class_type type
;
79 unsigned int alignment
;
83 /* Weak, set during translation. NULL if `in_ir` is false below. */
84 bt_field_class
*ir_fc
;
87 struct ctf_field_class_bit_array
{
88 struct ctf_field_class base
;
89 enum ctf_byte_order byte_order
;
93 struct ctf_field_class_int
{
94 struct ctf_field_class_bit_array base
;
95 enum ctf_field_class_meaning meaning
;
97 bt_field_class_integer_preferred_display_base disp_base
;
98 enum ctf_encoding encoding
;
99 int64_t storing_index
;
102 struct ctf_clock_class
*mapped_clock_class
;
117 struct ctf_field_class_enum_mapping
{
119 struct ctf_range range
;
122 struct ctf_field_class_enum
{
123 struct ctf_field_class_int base
;
125 /* Array of `struct ctf_field_class_enum_mapping` */
129 struct ctf_field_class_float
{
130 struct ctf_field_class_bit_array base
;
133 struct ctf_field_class_string
{
134 struct ctf_field_class base
;
135 enum ctf_encoding encoding
;
138 struct ctf_named_field_class
{
142 struct ctf_field_class
*fc
;
145 struct ctf_field_class_struct
{
146 struct ctf_field_class base
;
148 /* Array of `struct ctf_named_field_class` */
152 struct ctf_field_path
{
155 /* Array of `int64_t` */
159 struct ctf_field_class_variant_range
{
160 struct ctf_range range
;
161 uint64_t option_index
;
164 struct ctf_field_class_variant
{
165 struct ctf_field_class base
;
167 struct ctf_field_path tag_path
;
168 uint64_t stored_tag_index
;
170 /* Array of `struct ctf_named_field_class` */
173 /* Array of `struct ctf_field_class_variant_range` */
177 struct ctf_field_class_enum
*tag_fc
;
180 struct ctf_field_class_array_base
{
181 struct ctf_field_class base
;
182 struct ctf_field_class
*elem_fc
;
186 struct ctf_field_class_array
{
187 struct ctf_field_class_array_base base
;
188 enum ctf_field_class_meaning meaning
;
192 struct ctf_field_class_sequence
{
193 struct ctf_field_class_array_base base
;
195 struct ctf_field_path length_path
;
196 uint64_t stored_length_index
;
199 struct ctf_field_class_int
*length_fc
;
202 struct ctf_event_class
{
206 bt_event_class_log_level log_level
;
210 struct ctf_field_class
*spec_context_fc
;
213 struct ctf_field_class
*payload_fc
;
215 /* Weak, set during translation */
216 bt_event_class
*ir_ec
;
219 struct ctf_stream_class
{
224 struct ctf_field_class
*packet_context_fc
;
227 struct ctf_field_class
*event_header_fc
;
230 struct ctf_field_class
*event_common_context_fc
;
232 /* Array of `struct ctf_event_class *`, owned by this */
233 GPtrArray
*event_classes
;
236 * Hash table mapping event class IDs to `struct ctf_event_class *`,
239 GHashTable
*event_classes_by_id
;
242 struct ctf_clock_class
*default_clock_class
;
244 /* Weak, set during translation */
245 bt_stream_class
*ir_sc
;
248 enum ctf_trace_class_env_entry_type
{
249 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
250 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
253 struct ctf_trace_class_env_entry
{
254 enum ctf_trace_class_env_entry_type type
;
263 struct ctf_trace_class
{
268 enum ctf_byte_order default_byte_order
;
271 struct ctf_field_class
*packet_header_fc
;
273 uint64_t stored_value_count
;
275 /* Array of `struct ctf_clock_class *` (owned by this) */
276 GPtrArray
*clock_classes
;
278 /* Array of `struct ctf_stream_class *` */
279 GPtrArray
*stream_classes
;
281 /* Array of `struct ctf_trace_class_env_entry` */
286 /* Weak, set during translation */
287 bt_trace_class
*ir_tc
;
291 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
294 void _ctf_field_class_init(struct ctf_field_class
*fc
,
295 enum ctf_field_class_type type
, unsigned int alignment
)
299 fc
->alignment
= alignment
;
304 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
305 enum ctf_field_class_type type
)
307 _ctf_field_class_init((void *) fc
, type
, 1);
311 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
312 enum ctf_field_class_type type
)
314 _ctf_field_class_bit_array_init((void *) fc
, type
);
315 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
316 fc
->storing_index
= -1;
320 void ctf_field_path_init(struct ctf_field_path
*field_path
)
322 BT_ASSERT(field_path
);
323 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
324 BT_ASSERT(field_path
->path
);
328 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
330 BT_ASSERT(field_path
);
332 if (field_path
->path
) {
333 g_array_free(field_path
->path
, TRUE
);
338 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
341 named_fc
->name
= g_string_new(NULL
);
342 BT_ASSERT(named_fc
->name
);
346 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
350 if (named_fc
->name
) {
351 g_string_free(named_fc
->name
, TRUE
);
354 ctf_field_class_destroy(named_fc
->fc
);
358 void _ctf_field_class_enum_mapping_init(
359 struct ctf_field_class_enum_mapping
*mapping
)
362 mapping
->label
= g_string_new(NULL
);
363 BT_ASSERT(mapping
->label
);
367 void _ctf_field_class_enum_mapping_fini(
368 struct ctf_field_class_enum_mapping
*mapping
)
372 if (mapping
->label
) {
373 g_string_free(mapping
->label
, TRUE
);
378 struct ctf_field_class_int
*ctf_field_class_int_create(void)
380 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
383 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
388 struct ctf_field_class_float
*ctf_field_class_float_create(void)
390 struct ctf_field_class_float
*fc
=
391 g_new0(struct ctf_field_class_float
, 1);
394 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
399 struct ctf_field_class_string
*ctf_field_class_string_create(void)
401 struct ctf_field_class_string
*fc
=
402 g_new0(struct ctf_field_class_string
, 1);
405 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
410 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
412 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
415 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
416 fc
->mappings
= g_array_new(FALSE
, TRUE
,
417 sizeof(struct ctf_field_class_enum_mapping
));
418 BT_ASSERT(fc
->mappings
);
423 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
425 struct ctf_field_class_struct
*fc
=
426 g_new0(struct ctf_field_class_struct
, 1);
429 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
430 fc
->members
= g_array_new(FALSE
, TRUE
,
431 sizeof(struct ctf_named_field_class
));
432 BT_ASSERT(fc
->members
);
433 fc
->base
.is_compound
= true;
438 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
440 struct ctf_field_class_variant
*fc
=
441 g_new0(struct ctf_field_class_variant
, 1);
444 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
445 fc
->options
= g_array_new(FALSE
, TRUE
,
446 sizeof(struct ctf_named_field_class
));
447 BT_ASSERT(fc
->options
);
448 fc
->ranges
= g_array_new(FALSE
, TRUE
,
449 sizeof(struct ctf_field_class_variant_range
));
450 BT_ASSERT(fc
->ranges
);
451 fc
->tag_ref
= g_string_new(NULL
);
452 BT_ASSERT(fc
->tag_ref
);
453 ctf_field_path_init(&fc
->tag_path
);
454 fc
->base
.is_compound
= true;
459 struct ctf_field_class_array
*ctf_field_class_array_create(void)
461 struct ctf_field_class_array
*fc
=
462 g_new0(struct ctf_field_class_array
, 1);
465 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
466 fc
->base
.base
.is_compound
= true;
471 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
473 struct ctf_field_class_sequence
*fc
=
474 g_new0(struct ctf_field_class_sequence
, 1);
477 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
478 fc
->length_ref
= g_string_new(NULL
);
479 BT_ASSERT(fc
->length_ref
);
480 ctf_field_path_init(&fc
->length_path
);
481 fc
->base
.base
.is_compound
= true;
486 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
493 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
500 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
501 struct ctf_field_class_enum_mapping
*mapping
=
502 &g_array_index(fc
->mappings
,
503 struct ctf_field_class_enum_mapping
, i
);
505 _ctf_field_class_enum_mapping_fini(mapping
);
508 g_array_free(fc
->mappings
, TRUE
);
515 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
522 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
529 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
536 for (i
= 0; i
< fc
->members
->len
; i
++) {
537 struct ctf_named_field_class
*named_fc
=
538 &g_array_index(fc
->members
,
539 struct ctf_named_field_class
, i
);
541 _ctf_named_field_class_fini(named_fc
);
544 g_array_free(fc
->members
, TRUE
);
551 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
554 ctf_field_class_destroy(fc
->elem_fc
);
558 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
561 _ctf_field_class_array_base_fini((void *) fc
);
566 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
569 _ctf_field_class_array_base_fini((void *) fc
);
571 if (fc
->length_ref
) {
572 g_string_free(fc
->length_ref
, TRUE
);
575 ctf_field_path_fini(&fc
->length_path
);
580 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
587 for (i
= 0; i
< fc
->options
->len
; i
++) {
588 struct ctf_named_field_class
*named_fc
=
589 &g_array_index(fc
->options
,
590 struct ctf_named_field_class
, i
);
592 _ctf_named_field_class_fini(named_fc
);
595 g_array_free(fc
->options
, TRUE
);
599 g_array_free(fc
->ranges
, TRUE
);
603 g_string_free(fc
->tag_ref
, TRUE
);
606 ctf_field_path_fini(&fc
->tag_path
);
611 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
618 case CTF_FIELD_CLASS_TYPE_INT
:
619 _ctf_field_class_int_destroy((void *) fc
);
621 case CTF_FIELD_CLASS_TYPE_ENUM
:
622 _ctf_field_class_enum_destroy((void *) fc
);
624 case CTF_FIELD_CLASS_TYPE_FLOAT
:
625 _ctf_field_class_float_destroy((void *) fc
);
627 case CTF_FIELD_CLASS_TYPE_STRING
:
628 _ctf_field_class_string_destroy((void *) fc
);
630 case CTF_FIELD_CLASS_TYPE_STRUCT
:
631 _ctf_field_class_struct_destroy((void *) fc
);
633 case CTF_FIELD_CLASS_TYPE_ARRAY
:
634 _ctf_field_class_array_destroy((void *) fc
);
636 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
637 _ctf_field_class_sequence_destroy((void *) fc
);
639 case CTF_FIELD_CLASS_TYPE_VARIANT
:
640 _ctf_field_class_variant_destroy((void *) fc
);
648 void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum
*fc
,
649 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
651 struct ctf_field_class_enum_mapping
*mapping
;
655 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
657 mapping
= &g_array_index(fc
->mappings
,
658 struct ctf_field_class_enum_mapping
, fc
->mappings
->len
- 1);
659 _ctf_field_class_enum_mapping_init(mapping
);
660 g_string_assign(mapping
->label
, label
);
661 mapping
->range
.lower
.u
= u_lower
;
662 mapping
->range
.upper
.u
= u_upper
;
666 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
667 struct ctf_field_class_enum
*fc
, uint64_t index
)
670 BT_ASSERT(index
< fc
->mappings
->len
);
671 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
676 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
677 struct ctf_field_class_struct
*fc
, uint64_t index
)
680 BT_ASSERT(index
< fc
->members
->len
);
681 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
686 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
687 struct ctf_field_class_struct
*fc
, const char *name
)
690 struct ctf_named_field_class
*ret_named_fc
= NULL
;
695 for (i
= 0; i
< fc
->members
->len
; i
++) {
696 struct ctf_named_field_class
*named_fc
=
697 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
699 if (strcmp(name
, named_fc
->name
->str
) == 0) {
700 ret_named_fc
= named_fc
;
710 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
711 struct ctf_field_class_struct
*struct_fc
, const char *name
)
713 struct ctf_named_field_class
*named_fc
= NULL
;
714 struct ctf_field_class
*fc
= NULL
;
720 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
732 struct ctf_field_class_int
*
733 ctf_field_class_struct_borrow_member_int_field_class_by_name(
734 struct ctf_field_class_struct
*struct_fc
, const char *name
)
736 struct ctf_field_class_int
*int_fc
= NULL
;
739 ctf_field_class_struct_borrow_member_field_class_by_name(
745 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
746 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
757 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
758 const char *name
, struct ctf_field_class
*member_fc
)
760 struct ctf_named_field_class
*named_fc
;
764 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
766 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
767 fc
->members
->len
- 1);
768 _ctf_named_field_class_init(named_fc
);
769 g_string_assign(named_fc
->name
, name
);
770 named_fc
->fc
= member_fc
;
772 if (member_fc
->alignment
> fc
->base
.alignment
) {
773 fc
->base
.alignment
= member_fc
->alignment
;
778 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
779 struct ctf_field_class_variant
*fc
, uint64_t index
)
782 BT_ASSERT(index
< fc
->options
->len
);
783 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
788 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
789 struct ctf_field_class_variant
*fc
, const char *name
)
792 struct ctf_named_field_class
*ret_named_fc
= NULL
;
797 for (i
= 0; i
< fc
->options
->len
; i
++) {
798 struct ctf_named_field_class
*named_fc
=
799 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
801 if (strcmp(name
, named_fc
->name
->str
) == 0) {
802 ret_named_fc
= named_fc
;
812 struct ctf_field_class_variant_range
*
813 ctf_field_class_variant_borrow_range_by_index(
814 struct ctf_field_class_variant
*fc
, uint64_t index
)
817 BT_ASSERT(index
< fc
->ranges
->len
);
818 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
823 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
824 const char *name
, struct ctf_field_class
*option_fc
)
826 struct ctf_named_field_class
*named_fc
;
830 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
832 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
833 fc
->options
->len
- 1);
834 _ctf_named_field_class_init(named_fc
);
835 g_string_assign(named_fc
->name
, name
);
836 named_fc
->fc
= option_fc
;
840 void ctf_field_class_variant_set_tag_field_class(
841 struct ctf_field_class_variant
*fc
,
842 struct ctf_field_class_enum
*tag_fc
)
850 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
852 struct ctf_named_field_class
*named_fc
=
853 ctf_field_class_variant_borrow_option_by_index(
856 for (mapping_i
= 0; mapping_i
< tag_fc
->mappings
->len
;
858 struct ctf_field_class_enum_mapping
*mapping
=
859 ctf_field_class_enum_borrow_mapping_by_index(
862 if (strcmp(named_fc
->name
->str
,
863 mapping
->label
->str
) == 0) {
864 struct ctf_field_class_variant_range range
;
866 range
.range
= mapping
->range
;
867 range
.option_index
= option_i
;
868 g_array_append_val(fc
->ranges
, range
);
875 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
876 struct ctf_field_class
*comp_fc
, uint64_t index
)
878 struct ctf_field_class
*fc
= NULL
;
880 switch (comp_fc
->type
) {
881 case CTF_FIELD_CLASS_TYPE_STRUCT
:
883 struct ctf_named_field_class
*named_fc
=
884 ctf_field_class_struct_borrow_member_by_index(
885 (void *) comp_fc
, index
);
891 case CTF_FIELD_CLASS_TYPE_VARIANT
:
893 struct ctf_named_field_class
*named_fc
=
894 ctf_field_class_variant_borrow_option_by_index(
895 (void *) comp_fc
, index
);
901 case CTF_FIELD_CLASS_TYPE_ARRAY
:
902 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
904 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
906 fc
= array_fc
->elem_fc
;
917 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
919 uint64_t field_count
;
922 case CTF_FIELD_CLASS_TYPE_STRUCT
:
924 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
926 field_count
= struct_fc
->members
->len
;
929 case CTF_FIELD_CLASS_TYPE_VARIANT
:
931 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
933 field_count
= var_fc
->options
->len
;
936 case CTF_FIELD_CLASS_TYPE_ARRAY
:
937 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
939 * Array and sequence types always contain a single
940 * member (the element type).
952 int64_t ctf_field_class_compound_get_field_class_index_from_name(
953 struct ctf_field_class
*fc
, const char *name
)
955 int64_t ret_index
= -1;
959 case CTF_FIELD_CLASS_TYPE_STRUCT
:
961 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
963 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
964 struct ctf_named_field_class
*named_fc
=
965 ctf_field_class_struct_borrow_member_by_index(
968 if (strcmp(name
, named_fc
->name
->str
) == 0) {
969 ret_index
= (int64_t) i
;
976 case CTF_FIELD_CLASS_TYPE_VARIANT
:
978 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
980 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
981 struct ctf_named_field_class
*named_fc
=
982 ctf_field_class_variant_borrow_option_by_index(
985 if (strcmp(name
, named_fc
->name
->str
) == 0) {
986 ret_index
= (int64_t) i
;
1002 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1005 g_array_append_val(fp
->path
, index
);
1009 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1013 BT_ASSERT(index
< fp
->path
->len
);
1014 return g_array_index(fp
->path
, int64_t, index
);
1018 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1021 g_array_set_size(fp
->path
, 0);
1025 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1027 GString
*str
= g_string_new(NULL
);
1036 g_string_append_printf(str
, "[%s", bt_common_scope_string(
1039 for (i
= 0; i
< path
->path
->len
; i
++) {
1040 g_string_append_printf(str
, ", %" PRId64
,
1041 ctf_field_path_borrow_index_by_index(path
, i
));
1044 g_string_append(str
, "]");
1051 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1052 struct ctf_field_path
*field_path
,
1053 struct ctf_trace_class
*tc
,
1054 struct ctf_stream_class
*sc
,
1055 struct ctf_event_class
*ec
)
1058 struct ctf_field_class
*fc
;
1060 switch (field_path
->root
) {
1061 case BT_SCOPE_PACKET_HEADER
:
1062 fc
= tc
->packet_header_fc
;
1064 case BT_SCOPE_PACKET_CONTEXT
:
1065 fc
= sc
->packet_context_fc
;
1067 case BT_SCOPE_EVENT_HEADER
:
1068 fc
= sc
->event_header_fc
;
1070 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
1071 fc
= sc
->event_common_context_fc
;
1073 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1074 fc
= ec
->spec_context_fc
;
1076 case BT_SCOPE_EVENT_PAYLOAD
:
1077 fc
= ec
->payload_fc
;
1085 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1086 int64_t child_index
=
1087 ctf_field_path_borrow_index_by_index(field_path
, i
);
1088 struct ctf_field_class
*child_fc
=
1089 ctf_field_class_compound_borrow_field_class_by_index(
1091 BT_ASSERT(child_fc
);
1100 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1103 void ctf_field_class_bit_array_copy_content(
1104 struct ctf_field_class_bit_array
*dst_fc
,
1105 struct ctf_field_class_bit_array
*src_fc
)
1109 dst_fc
->byte_order
= src_fc
->byte_order
;
1110 dst_fc
->size
= src_fc
->size
;
1114 void ctf_field_class_int_copy_content(
1115 struct ctf_field_class_int
*dst_fc
,
1116 struct ctf_field_class_int
*src_fc
)
1118 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1119 dst_fc
->meaning
= src_fc
->meaning
;
1120 dst_fc
->is_signed
= src_fc
->is_signed
;
1121 dst_fc
->disp_base
= src_fc
->disp_base
;
1122 dst_fc
->encoding
= src_fc
->encoding
;
1123 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1124 dst_fc
->storing_index
= src_fc
->storing_index
;
1128 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1129 struct ctf_field_class_int
*fc
)
1131 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1134 ctf_field_class_int_copy_content(copy_fc
, fc
);
1139 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1140 struct ctf_field_class_enum
*fc
)
1142 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1146 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1148 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1149 struct ctf_field_class_enum_mapping
*mapping
=
1150 &g_array_index(fc
->mappings
,
1151 struct ctf_field_class_enum_mapping
, i
);
1153 ctf_field_class_enum_append_mapping(copy_fc
, mapping
->label
->str
,
1154 mapping
->range
.lower
.u
, mapping
->range
.upper
.u
);
1161 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1162 struct ctf_field_class_float
*fc
)
1164 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1167 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1172 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1173 struct ctf_field_class_string
*fc
)
1175 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1182 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1183 struct ctf_field_class_struct
*fc
)
1185 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1190 for (i
= 0; i
< fc
->members
->len
; i
++) {
1191 struct ctf_named_field_class
*named_fc
=
1192 &g_array_index(fc
->members
,
1193 struct ctf_named_field_class
, i
);
1195 ctf_field_class_struct_append_member(copy_fc
,
1196 named_fc
->name
->str
,
1197 ctf_field_class_copy(named_fc
->fc
));
1204 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1205 struct ctf_field_path
*src_fp
)
1211 dst_fp
->root
= src_fp
->root
;
1212 ctf_field_path_clear(dst_fp
);
1214 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1215 int64_t index
= ctf_field_path_borrow_index_by_index(
1218 ctf_field_path_append_index(dst_fp
, index
);
1223 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1224 struct ctf_field_class_variant
*fc
)
1226 struct ctf_field_class_variant
*copy_fc
=
1227 ctf_field_class_variant_create();
1232 for (i
= 0; i
< fc
->options
->len
; i
++) {
1233 struct ctf_named_field_class
*named_fc
=
1234 &g_array_index(fc
->options
,
1235 struct ctf_named_field_class
, i
);
1237 ctf_field_class_variant_append_option(copy_fc
,
1238 named_fc
->name
->str
,
1239 ctf_field_class_copy(named_fc
->fc
));
1242 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1243 struct ctf_field_class_variant_range
*range
=
1244 &g_array_index(fc
->ranges
,
1245 struct ctf_field_class_variant_range
, i
);
1247 g_array_append_val(copy_fc
->ranges
, *range
);
1250 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1251 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1252 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1257 void ctf_field_class_array_base_copy_content(
1258 struct ctf_field_class_array_base
*dst_fc
,
1259 struct ctf_field_class_array_base
*src_fc
)
1263 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1264 dst_fc
->is_text
= src_fc
->is_text
;
1268 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1269 struct ctf_field_class_array
*fc
)
1271 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1274 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1275 copy_fc
->length
= fc
->length
;
1280 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1281 struct ctf_field_class_sequence
*fc
)
1283 struct ctf_field_class_sequence
*copy_fc
=
1284 ctf_field_class_sequence_create();
1287 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1288 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1289 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1290 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1295 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1297 struct ctf_field_class
*copy_fc
= NULL
;
1304 * Translation should not have happened yet.
1306 BT_ASSERT(!fc
->ir_fc
);
1309 case CTF_FIELD_CLASS_TYPE_INT
:
1310 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1312 case CTF_FIELD_CLASS_TYPE_ENUM
:
1313 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1315 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1316 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1318 case CTF_FIELD_CLASS_TYPE_STRING
:
1319 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1321 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1322 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1324 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1325 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1327 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1328 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1330 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1331 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1337 copy_fc
->type
= fc
->type
;
1338 copy_fc
->alignment
= fc
->alignment
;
1339 copy_fc
->in_ir
= fc
->in_ir
;
1346 struct ctf_event_class
*ctf_event_class_create(void)
1348 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1351 ec
->name
= g_string_new(NULL
);
1352 BT_ASSERT(ec
->name
);
1353 ec
->emf_uri
= g_string_new(NULL
);
1354 BT_ASSERT(ec
->emf_uri
);
1360 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1367 g_string_free(ec
->name
, TRUE
);
1371 g_string_free(ec
->emf_uri
, TRUE
);
1374 ctf_field_class_destroy(ec
->spec_context_fc
);
1375 ctf_field_class_destroy(ec
->payload_fc
);
1380 struct ctf_stream_class
*ctf_stream_class_create(void)
1382 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1385 sc
->event_classes
= g_ptr_array_new_with_free_func(
1386 (GDestroyNotify
) ctf_event_class_destroy
);
1387 BT_ASSERT(sc
->event_classes
);
1388 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1390 BT_ASSERT(sc
->event_classes_by_id
);
1395 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1401 if (sc
->event_classes
) {
1402 g_ptr_array_free(sc
->event_classes
, TRUE
);
1405 if (sc
->event_classes_by_id
) {
1406 g_hash_table_destroy(sc
->event_classes_by_id
);
1409 ctf_field_class_destroy(sc
->packet_context_fc
);
1410 ctf_field_class_destroy(sc
->event_header_fc
);
1411 ctf_field_class_destroy(sc
->event_common_context_fc
);
1416 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1417 struct ctf_event_class
*ec
)
1419 g_ptr_array_add(sc
->event_classes
, ec
);
1420 g_hash_table_insert(sc
->event_classes_by_id
,
1421 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1425 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1426 struct ctf_stream_class
*sc
, uint64_t type
)
1429 return g_hash_table_lookup(sc
->event_classes_by_id
,
1430 GUINT_TO_POINTER((guint
) type
));
1434 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1437 entry
->name
= g_string_new(NULL
);
1438 BT_ASSERT(entry
->name
);
1439 entry
->value
.str
= g_string_new(NULL
);
1440 BT_ASSERT(entry
->value
.str
);
1444 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1449 g_string_free(entry
->name
, TRUE
);
1452 if (entry
->value
.str
) {
1453 g_string_free(entry
->value
.str
, TRUE
);
1458 struct ctf_clock_class
*ctf_clock_class_create(void)
1460 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1463 cc
->name
= g_string_new(NULL
);
1464 BT_ASSERT(cc
->name
);
1465 cc
->description
= g_string_new(NULL
);
1466 BT_ASSERT(cc
->description
);
1471 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1478 g_string_free(cc
->name
, TRUE
);
1481 if (cc
->description
) {
1482 g_string_free(cc
->description
, TRUE
);
1485 bt_clock_class_put_ref(cc
->ir_cc
);
1490 struct ctf_trace_class
*ctf_trace_class_create(void)
1492 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1495 tc
->default_byte_order
= -1;
1496 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1497 (GDestroyNotify
) ctf_clock_class_destroy
);
1498 BT_ASSERT(tc
->clock_classes
);
1499 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1500 (GDestroyNotify
) ctf_stream_class_destroy
);
1501 BT_ASSERT(tc
->stream_classes
);
1502 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1503 sizeof(struct ctf_trace_class_env_entry
));
1508 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1514 ctf_field_class_destroy(tc
->packet_header_fc
);
1516 if (tc
->clock_classes
) {
1517 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1520 if (tc
->stream_classes
) {
1521 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1524 if (tc
->env_entries
) {
1527 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1528 struct ctf_trace_class_env_entry
*entry
=
1529 &g_array_index(tc
->env_entries
,
1530 struct ctf_trace_class_env_entry
, i
);
1532 _ctf_trace_class_env_entry_fini(entry
);
1535 g_array_free(tc
->env_entries
, TRUE
);
1542 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1543 const char *name
, enum ctf_trace_class_env_entry_type type
,
1544 const char *str_value
, int64_t i_value
)
1546 struct ctf_trace_class_env_entry
*entry
;
1550 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1552 entry
= &g_array_index(tc
->env_entries
,
1553 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1555 _ctf_trace_class_env_entry_init(entry
);
1556 g_string_assign(entry
->name
, name
);
1559 g_string_assign(entry
->value
.str
, str_value
);
1562 entry
->value
.i
= i_value
;
1566 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1567 struct ctf_trace_class
*tc
, uint64_t id
)
1570 struct ctf_stream_class
*ret_sc
= NULL
;
1574 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1575 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1588 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1589 struct ctf_trace_class
*tc
, const char *name
)
1592 struct ctf_clock_class
*ret_cc
= NULL
;
1597 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1598 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1600 BT_ASSERT(cc
->name
);
1601 if (strcmp(cc
->name
->str
, name
) == 0) {
1612 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1613 struct ctf_trace_class
*tc
, uint64_t index
)
1616 BT_ASSERT(index
< tc
->env_entries
->len
);
1617 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1622 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1623 struct ctf_trace_class
*tc
, const char *name
)
1625 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1631 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1632 struct ctf_trace_class_env_entry
*env_entry
=
1633 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1635 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1636 ret_entry
= env_entry
;
1645 #endif /* _CTF_META_H */