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 bt_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 bt_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 bt_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 bt_clock_class
*default_clock_class
;
229 /* Weak, set during translation */
230 bt_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
{
253 enum ctf_byte_order default_byte_order
;
256 struct ctf_field_class
*packet_header_fc
;
258 uint64_t stored_value_count
;
260 /* Array of `bt_clock_class *` (owned by this) */
261 GPtrArray
*clock_classes
;
263 /* Array of `struct ctf_stream_class *` */
264 GPtrArray
*stream_classes
;
266 /* Array of `struct ctf_trace_class_env_entry` */
271 /* Weak, set during translation */
272 bt_trace_class
*ir_tc
;
276 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
279 void _ctf_field_class_init(struct ctf_field_class
*fc
,
280 enum ctf_field_class_type type
, unsigned int alignment
)
284 fc
->alignment
= alignment
;
289 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
290 enum ctf_field_class_type type
)
292 _ctf_field_class_init((void *) fc
, type
, 1);
296 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
297 enum ctf_field_class_type type
)
299 _ctf_field_class_bit_array_init((void *) fc
, type
);
300 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
301 fc
->storing_index
= -1;
305 void ctf_field_path_init(struct ctf_field_path
*field_path
)
307 BT_ASSERT(field_path
);
308 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
309 BT_ASSERT(field_path
->path
);
313 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
315 BT_ASSERT(field_path
);
317 if (field_path
->path
) {
318 g_array_free(field_path
->path
, TRUE
);
323 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
326 named_fc
->name
= g_string_new(NULL
);
327 BT_ASSERT(named_fc
->name
);
331 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
335 if (named_fc
->name
) {
336 g_string_free(named_fc
->name
, TRUE
);
339 ctf_field_class_destroy(named_fc
->fc
);
343 void _ctf_field_class_enum_mapping_init(
344 struct ctf_field_class_enum_mapping
*mapping
)
347 mapping
->label
= g_string_new(NULL
);
348 BT_ASSERT(mapping
->label
);
352 void _ctf_field_class_enum_mapping_fini(
353 struct ctf_field_class_enum_mapping
*mapping
)
357 if (mapping
->label
) {
358 g_string_free(mapping
->label
, TRUE
);
363 struct ctf_field_class_int
*ctf_field_class_int_create(void)
365 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
368 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
373 struct ctf_field_class_float
*ctf_field_class_float_create(void)
375 struct ctf_field_class_float
*fc
=
376 g_new0(struct ctf_field_class_float
, 1);
379 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
384 struct ctf_field_class_string
*ctf_field_class_string_create(void)
386 struct ctf_field_class_string
*fc
=
387 g_new0(struct ctf_field_class_string
, 1);
390 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
395 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
397 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
400 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
401 fc
->mappings
= g_array_new(FALSE
, TRUE
,
402 sizeof(struct ctf_field_class_enum_mapping
));
403 BT_ASSERT(fc
->mappings
);
408 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
410 struct ctf_field_class_struct
*fc
=
411 g_new0(struct ctf_field_class_struct
, 1);
414 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
415 fc
->members
= g_array_new(FALSE
, TRUE
,
416 sizeof(struct ctf_named_field_class
));
417 BT_ASSERT(fc
->members
);
418 fc
->base
.is_compound
= true;
423 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
425 struct ctf_field_class_variant
*fc
=
426 g_new0(struct ctf_field_class_variant
, 1);
429 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
430 fc
->options
= g_array_new(FALSE
, TRUE
,
431 sizeof(struct ctf_named_field_class
));
432 BT_ASSERT(fc
->options
);
433 fc
->ranges
= g_array_new(FALSE
, TRUE
,
434 sizeof(struct ctf_field_class_variant_range
));
435 BT_ASSERT(fc
->ranges
);
436 fc
->tag_ref
= g_string_new(NULL
);
437 BT_ASSERT(fc
->tag_ref
);
438 ctf_field_path_init(&fc
->tag_path
);
439 fc
->base
.is_compound
= true;
444 struct ctf_field_class_array
*ctf_field_class_array_create(void)
446 struct ctf_field_class_array
*fc
=
447 g_new0(struct ctf_field_class_array
, 1);
450 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
451 fc
->base
.base
.is_compound
= true;
456 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
458 struct ctf_field_class_sequence
*fc
=
459 g_new0(struct ctf_field_class_sequence
, 1);
462 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
463 fc
->length_ref
= g_string_new(NULL
);
464 BT_ASSERT(fc
->length_ref
);
465 ctf_field_path_init(&fc
->length_path
);
466 fc
->base
.base
.is_compound
= true;
471 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
474 bt_clock_class_put_ref(fc
->mapped_clock_class
);
479 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
482 bt_clock_class_put_ref(fc
->base
.mapped_clock_class
);
487 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
488 struct ctf_field_class_enum_mapping
*mapping
=
489 &g_array_index(fc
->mappings
,
490 struct ctf_field_class_enum_mapping
, i
);
492 _ctf_field_class_enum_mapping_fini(mapping
);
495 g_array_free(fc
->mappings
, TRUE
);
502 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
509 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
516 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
523 for (i
= 0; i
< fc
->members
->len
; i
++) {
524 struct ctf_named_field_class
*named_fc
=
525 &g_array_index(fc
->members
,
526 struct ctf_named_field_class
, i
);
528 _ctf_named_field_class_fini(named_fc
);
531 g_array_free(fc
->members
, TRUE
);
538 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
541 ctf_field_class_destroy(fc
->elem_fc
);
545 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
548 _ctf_field_class_array_base_fini((void *) fc
);
553 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
556 _ctf_field_class_array_base_fini((void *) fc
);
558 if (fc
->length_ref
) {
559 g_string_free(fc
->length_ref
, TRUE
);
562 ctf_field_path_fini(&fc
->length_path
);
567 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
574 for (i
= 0; i
< fc
->options
->len
; i
++) {
575 struct ctf_named_field_class
*named_fc
=
576 &g_array_index(fc
->options
,
577 struct ctf_named_field_class
, i
);
579 _ctf_named_field_class_fini(named_fc
);
582 g_array_free(fc
->options
, TRUE
);
586 g_array_free(fc
->ranges
, TRUE
);
590 g_string_free(fc
->tag_ref
, TRUE
);
593 ctf_field_path_fini(&fc
->tag_path
);
598 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
605 case CTF_FIELD_CLASS_TYPE_INT
:
606 _ctf_field_class_int_destroy((void *) fc
);
608 case CTF_FIELD_CLASS_TYPE_ENUM
:
609 _ctf_field_class_enum_destroy((void *) fc
);
611 case CTF_FIELD_CLASS_TYPE_FLOAT
:
612 _ctf_field_class_float_destroy((void *) fc
);
614 case CTF_FIELD_CLASS_TYPE_STRING
:
615 _ctf_field_class_string_destroy((void *) fc
);
617 case CTF_FIELD_CLASS_TYPE_STRUCT
:
618 _ctf_field_class_struct_destroy((void *) fc
);
620 case CTF_FIELD_CLASS_TYPE_ARRAY
:
621 _ctf_field_class_array_destroy((void *) fc
);
623 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
624 _ctf_field_class_sequence_destroy((void *) fc
);
626 case CTF_FIELD_CLASS_TYPE_VARIANT
:
627 _ctf_field_class_variant_destroy((void *) fc
);
635 void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum
*fc
,
636 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
638 struct ctf_field_class_enum_mapping
*mapping
;
642 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
644 mapping
= &g_array_index(fc
->mappings
,
645 struct ctf_field_class_enum_mapping
, fc
->mappings
->len
- 1);
646 _ctf_field_class_enum_mapping_init(mapping
);
647 g_string_assign(mapping
->label
, label
);
648 mapping
->range
.lower
.u
= u_lower
;
649 mapping
->range
.upper
.u
= u_upper
;
653 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
654 struct ctf_field_class_enum
*fc
, uint64_t index
)
657 BT_ASSERT(index
< fc
->mappings
->len
);
658 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
663 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
664 struct ctf_field_class_struct
*fc
, uint64_t index
)
667 BT_ASSERT(index
< fc
->members
->len
);
668 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
673 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
674 struct ctf_field_class_struct
*fc
, const char *name
)
677 struct ctf_named_field_class
*ret_named_fc
= NULL
;
682 for (i
= 0; i
< fc
->members
->len
; i
++) {
683 struct ctf_named_field_class
*named_fc
=
684 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
686 if (strcmp(name
, named_fc
->name
->str
) == 0) {
687 ret_named_fc
= named_fc
;
697 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
698 struct ctf_field_class_struct
*struct_fc
, const char *name
)
700 struct ctf_named_field_class
*named_fc
= NULL
;
701 struct ctf_field_class
*fc
= NULL
;
707 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
719 struct ctf_field_class_int
*
720 ctf_field_class_struct_borrow_member_int_field_class_by_name(
721 struct ctf_field_class_struct
*struct_fc
, const char *name
)
723 struct ctf_field_class_int
*int_fc
= NULL
;
726 ctf_field_class_struct_borrow_member_field_class_by_name(
732 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
733 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
744 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
745 const char *name
, struct ctf_field_class
*member_fc
)
747 struct ctf_named_field_class
*named_fc
;
751 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
753 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
754 fc
->members
->len
- 1);
755 _ctf_named_field_class_init(named_fc
);
756 g_string_assign(named_fc
->name
, name
);
757 named_fc
->fc
= member_fc
;
759 if (member_fc
->alignment
> fc
->base
.alignment
) {
760 fc
->base
.alignment
= member_fc
->alignment
;
765 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
766 struct ctf_field_class_variant
*fc
, uint64_t index
)
769 BT_ASSERT(index
< fc
->options
->len
);
770 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
775 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
776 struct ctf_field_class_variant
*fc
, const char *name
)
779 struct ctf_named_field_class
*ret_named_fc
= NULL
;
784 for (i
= 0; i
< fc
->options
->len
; i
++) {
785 struct ctf_named_field_class
*named_fc
=
786 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
788 if (strcmp(name
, named_fc
->name
->str
) == 0) {
789 ret_named_fc
= named_fc
;
799 struct ctf_field_class_variant_range
*
800 ctf_field_class_variant_borrow_range_by_index(
801 struct ctf_field_class_variant
*fc
, uint64_t index
)
804 BT_ASSERT(index
< fc
->ranges
->len
);
805 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
810 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
811 const char *name
, struct ctf_field_class
*option_fc
)
813 struct ctf_named_field_class
*named_fc
;
817 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
819 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
820 fc
->options
->len
- 1);
821 _ctf_named_field_class_init(named_fc
);
822 g_string_assign(named_fc
->name
, name
);
823 named_fc
->fc
= option_fc
;
827 void ctf_field_class_variant_set_tag_field_class(
828 struct ctf_field_class_variant
*fc
,
829 struct ctf_field_class_enum
*tag_fc
)
837 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
839 struct ctf_named_field_class
*named_fc
=
840 ctf_field_class_variant_borrow_option_by_index(
843 for (mapping_i
= 0; mapping_i
< tag_fc
->mappings
->len
;
845 struct ctf_field_class_enum_mapping
*mapping
=
846 ctf_field_class_enum_borrow_mapping_by_index(
849 if (strcmp(named_fc
->name
->str
,
850 mapping
->label
->str
) == 0) {
851 struct ctf_field_class_variant_range range
;
853 range
.range
= mapping
->range
;
854 range
.option_index
= option_i
;
855 g_array_append_val(fc
->ranges
, range
);
862 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
863 struct ctf_field_class
*comp_fc
, uint64_t index
)
865 struct ctf_field_class
*fc
= NULL
;
867 switch (comp_fc
->type
) {
868 case CTF_FIELD_CLASS_TYPE_STRUCT
:
870 struct ctf_named_field_class
*named_fc
=
871 ctf_field_class_struct_borrow_member_by_index(
872 (void *) comp_fc
, index
);
878 case CTF_FIELD_CLASS_TYPE_VARIANT
:
880 struct ctf_named_field_class
*named_fc
=
881 ctf_field_class_variant_borrow_option_by_index(
882 (void *) comp_fc
, index
);
888 case CTF_FIELD_CLASS_TYPE_ARRAY
:
889 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
891 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
893 fc
= array_fc
->elem_fc
;
904 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
906 uint64_t field_count
;
909 case CTF_FIELD_CLASS_TYPE_STRUCT
:
911 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
913 field_count
= struct_fc
->members
->len
;
916 case CTF_FIELD_CLASS_TYPE_VARIANT
:
918 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
920 field_count
= var_fc
->options
->len
;
923 case CTF_FIELD_CLASS_TYPE_ARRAY
:
924 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
926 * Array and sequence types always contain a single
927 * member (the element type).
939 int64_t ctf_field_class_compound_get_field_class_index_from_name(
940 struct ctf_field_class
*fc
, const char *name
)
942 int64_t ret_index
= -1;
946 case CTF_FIELD_CLASS_TYPE_STRUCT
:
948 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
950 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
951 struct ctf_named_field_class
*named_fc
=
952 ctf_field_class_struct_borrow_member_by_index(
955 if (strcmp(name
, named_fc
->name
->str
) == 0) {
956 ret_index
= (int64_t) i
;
963 case CTF_FIELD_CLASS_TYPE_VARIANT
:
965 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
967 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
968 struct ctf_named_field_class
*named_fc
=
969 ctf_field_class_variant_borrow_option_by_index(
972 if (strcmp(name
, named_fc
->name
->str
) == 0) {
973 ret_index
= (int64_t) i
;
989 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
992 g_array_append_val(fp
->path
, index
);
996 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1000 BT_ASSERT(index
< fp
->path
->len
);
1001 return g_array_index(fp
->path
, int64_t, index
);
1005 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1008 g_array_set_size(fp
->path
, 0);
1012 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1014 GString
*str
= g_string_new(NULL
);
1023 g_string_append_printf(str
, "[%s", bt_common_scope_string(
1026 for (i
= 0; i
< path
->path
->len
; i
++) {
1027 g_string_append_printf(str
, ", %" PRId64
,
1028 ctf_field_path_borrow_index_by_index(path
, i
));
1031 g_string_append(str
, "]");
1038 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1039 struct ctf_field_path
*field_path
,
1040 struct ctf_trace_class
*tc
,
1041 struct ctf_stream_class
*sc
,
1042 struct ctf_event_class
*ec
)
1045 struct ctf_field_class
*fc
;
1047 switch (field_path
->root
) {
1048 case BT_SCOPE_PACKET_HEADER
:
1049 fc
= tc
->packet_header_fc
;
1051 case BT_SCOPE_PACKET_CONTEXT
:
1052 fc
= sc
->packet_context_fc
;
1054 case BT_SCOPE_EVENT_HEADER
:
1055 fc
= sc
->event_header_fc
;
1057 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
1058 fc
= sc
->event_common_context_fc
;
1060 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1061 fc
= ec
->spec_context_fc
;
1063 case BT_SCOPE_EVENT_PAYLOAD
:
1064 fc
= ec
->payload_fc
;
1072 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1073 int64_t child_index
=
1074 ctf_field_path_borrow_index_by_index(field_path
, i
);
1075 struct ctf_field_class
*child_fc
=
1076 ctf_field_class_compound_borrow_field_class_by_index(
1078 BT_ASSERT(child_fc
);
1087 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1090 void ctf_field_class_bit_array_copy_content(
1091 struct ctf_field_class_bit_array
*dst_fc
,
1092 struct ctf_field_class_bit_array
*src_fc
)
1096 dst_fc
->byte_order
= src_fc
->byte_order
;
1097 dst_fc
->size
= src_fc
->size
;
1101 void ctf_field_class_int_copy_content(
1102 struct ctf_field_class_int
*dst_fc
,
1103 struct ctf_field_class_int
*src_fc
)
1105 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1106 dst_fc
->meaning
= src_fc
->meaning
;
1107 dst_fc
->is_signed
= src_fc
->is_signed
;
1108 dst_fc
->disp_base
= src_fc
->disp_base
;
1109 dst_fc
->encoding
= src_fc
->encoding
;
1110 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1111 bt_clock_class_get_ref(dst_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_clock_class_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
->default_byte_order
= -1;
1453 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1454 (GDestroyNotify
) bt_clock_class_put_ref
);
1455 BT_ASSERT(tc
->clock_classes
);
1456 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1457 (GDestroyNotify
) ctf_stream_class_destroy
);
1458 BT_ASSERT(tc
->stream_classes
);
1459 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1460 sizeof(struct ctf_trace_class_env_entry
));
1465 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1471 ctf_field_class_destroy(tc
->packet_header_fc
);
1473 if (tc
->clock_classes
) {
1474 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1477 if (tc
->stream_classes
) {
1478 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1481 if (tc
->env_entries
) {
1484 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1485 struct ctf_trace_class_env_entry
*entry
=
1486 &g_array_index(tc
->env_entries
,
1487 struct ctf_trace_class_env_entry
, i
);
1489 _ctf_trace_class_env_entry_fini(entry
);
1492 g_array_free(tc
->env_entries
, TRUE
);
1499 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1500 const char *name
, enum ctf_trace_class_env_entry_type type
,
1501 const char *str_value
, int64_t i_value
)
1503 struct ctf_trace_class_env_entry
*entry
;
1507 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1509 entry
= &g_array_index(tc
->env_entries
,
1510 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1512 _ctf_trace_class_env_entry_init(entry
);
1513 g_string_assign(entry
->name
, name
);
1516 g_string_assign(entry
->value
.str
, str_value
);
1519 entry
->value
.i
= i_value
;
1523 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1524 struct ctf_trace_class
*tc
, uint64_t id
)
1527 struct ctf_stream_class
*ret_sc
= NULL
;
1531 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1532 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1545 bt_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1546 struct ctf_trace_class
*tc
, const char *name
)
1549 bt_clock_class
*ret_cc
= NULL
;
1554 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1555 bt_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1556 const char *cc_name
= bt_clock_class_get_name(cc
);
1559 if (strcmp(cc_name
, name
) == 0) {
1570 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1571 struct ctf_trace_class
*tc
, uint64_t index
)
1574 BT_ASSERT(index
< tc
->env_entries
->len
);
1575 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1580 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1581 struct ctf_trace_class
*tc
, const char *name
)
1583 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1589 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1590 struct ctf_trace_class_env_entry
*env_entry
=
1591 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1593 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1594 ret_entry
= env_entry
;
1603 #endif /* _CTF_META_H */