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
,
63 CTF_SCOPE_PACKET_HEADER
,
64 CTF_SCOPE_PACKET_CONTEXT
,
65 CTF_SCOPE_EVENT_HEADER
,
66 CTF_SCOPE_EVENT_COMMON_CONTEXT
,
67 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
,
68 CTF_SCOPE_EVENT_PAYLOAD
,
71 struct ctf_clock_class
{
76 int64_t offset_seconds
;
77 uint64_t offset_cycles
;
82 /* Weak, set during translation */
83 bt_clock_class
*ir_cc
;
86 struct ctf_field_class
{
87 enum ctf_field_class_type type
;
88 unsigned int alignment
;
92 /* Weak, set during translation. NULL if `in_ir` is false below. */
93 bt_field_class
*ir_fc
;
96 struct ctf_field_class_bit_array
{
97 struct ctf_field_class base
;
98 enum ctf_byte_order byte_order
;
102 struct ctf_field_class_int
{
103 struct ctf_field_class_bit_array base
;
104 enum ctf_field_class_meaning meaning
;
106 bt_field_class_integer_preferred_display_base disp_base
;
107 enum ctf_encoding encoding
;
108 int64_t storing_index
;
111 struct ctf_clock_class
*mapped_clock_class
;
126 struct ctf_field_class_enum_mapping
{
128 struct ctf_range range
;
131 struct ctf_field_class_enum
{
132 struct ctf_field_class_int base
;
134 /* Array of `struct ctf_field_class_enum_mapping` */
138 struct ctf_field_class_float
{
139 struct ctf_field_class_bit_array base
;
142 struct ctf_field_class_string
{
143 struct ctf_field_class base
;
144 enum ctf_encoding encoding
;
147 struct ctf_named_field_class
{
151 struct ctf_field_class
*fc
;
154 struct ctf_field_class_struct
{
155 struct ctf_field_class base
;
157 /* Array of `struct ctf_named_field_class` */
161 struct ctf_field_path
{
164 /* Array of `int64_t` */
168 struct ctf_field_class_variant_range
{
169 struct ctf_range range
;
170 uint64_t option_index
;
173 struct ctf_field_class_variant
{
174 struct ctf_field_class base
;
176 struct ctf_field_path tag_path
;
177 uint64_t stored_tag_index
;
179 /* Array of `struct ctf_named_field_class` */
182 /* Array of `struct ctf_field_class_variant_range` */
186 struct ctf_field_class_enum
*tag_fc
;
189 struct ctf_field_class_array_base
{
190 struct ctf_field_class base
;
191 struct ctf_field_class
*elem_fc
;
195 struct ctf_field_class_array
{
196 struct ctf_field_class_array_base base
;
197 enum ctf_field_class_meaning meaning
;
201 struct ctf_field_class_sequence
{
202 struct ctf_field_class_array_base base
;
204 struct ctf_field_path length_path
;
205 uint64_t stored_length_index
;
208 struct ctf_field_class_int
*length_fc
;
211 struct ctf_event_class
{
215 bt_event_class_log_level log_level
;
219 struct ctf_field_class
*spec_context_fc
;
222 struct ctf_field_class
*payload_fc
;
224 /* Weak, set during translation */
225 bt_event_class
*ir_ec
;
228 struct ctf_stream_class
{
233 struct ctf_field_class
*packet_context_fc
;
236 struct ctf_field_class
*event_header_fc
;
239 struct ctf_field_class
*event_common_context_fc
;
241 /* Array of `struct ctf_event_class *`, owned by this */
242 GPtrArray
*event_classes
;
245 * Hash table mapping event class IDs to `struct ctf_event_class *`,
248 GHashTable
*event_classes_by_id
;
251 struct ctf_clock_class
*default_clock_class
;
253 /* Weak, set during translation */
254 bt_stream_class
*ir_sc
;
257 enum ctf_trace_class_env_entry_type
{
258 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
259 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
262 struct ctf_trace_class_env_entry
{
263 enum ctf_trace_class_env_entry_type type
;
272 struct ctf_trace_class
{
277 enum ctf_byte_order default_byte_order
;
280 struct ctf_field_class
*packet_header_fc
;
282 uint64_t stored_value_count
;
284 /* Array of `struct ctf_clock_class *` (owned by this) */
285 GPtrArray
*clock_classes
;
287 /* Array of `struct ctf_stream_class *` */
288 GPtrArray
*stream_classes
;
290 /* Array of `struct ctf_trace_class_env_entry` */
295 /* Weak, set during translation */
296 bt_trace_class
*ir_tc
;
300 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
303 void _ctf_field_class_init(struct ctf_field_class
*fc
,
304 enum ctf_field_class_type type
, unsigned int alignment
)
308 fc
->alignment
= alignment
;
313 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
314 enum ctf_field_class_type type
)
316 _ctf_field_class_init((void *) fc
, type
, 1);
320 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
321 enum ctf_field_class_type type
)
323 _ctf_field_class_bit_array_init((void *) fc
, type
);
324 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
325 fc
->storing_index
= -1;
329 void ctf_field_path_init(struct ctf_field_path
*field_path
)
331 BT_ASSERT(field_path
);
332 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
333 BT_ASSERT(field_path
->path
);
337 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
339 BT_ASSERT(field_path
);
341 if (field_path
->path
) {
342 g_array_free(field_path
->path
, TRUE
);
347 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
350 named_fc
->name
= g_string_new(NULL
);
351 BT_ASSERT(named_fc
->name
);
355 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
359 if (named_fc
->name
) {
360 g_string_free(named_fc
->name
, TRUE
);
363 ctf_field_class_destroy(named_fc
->fc
);
367 void _ctf_field_class_enum_mapping_init(
368 struct ctf_field_class_enum_mapping
*mapping
)
371 mapping
->label
= g_string_new(NULL
);
372 BT_ASSERT(mapping
->label
);
376 void _ctf_field_class_enum_mapping_fini(
377 struct ctf_field_class_enum_mapping
*mapping
)
381 if (mapping
->label
) {
382 g_string_free(mapping
->label
, TRUE
);
387 struct ctf_field_class_int
*ctf_field_class_int_create(void)
389 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
392 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
397 struct ctf_field_class_float
*ctf_field_class_float_create(void)
399 struct ctf_field_class_float
*fc
=
400 g_new0(struct ctf_field_class_float
, 1);
403 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
408 struct ctf_field_class_string
*ctf_field_class_string_create(void)
410 struct ctf_field_class_string
*fc
=
411 g_new0(struct ctf_field_class_string
, 1);
414 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
419 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
421 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
424 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
425 fc
->mappings
= g_array_new(FALSE
, TRUE
,
426 sizeof(struct ctf_field_class_enum_mapping
));
427 BT_ASSERT(fc
->mappings
);
432 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
434 struct ctf_field_class_struct
*fc
=
435 g_new0(struct ctf_field_class_struct
, 1);
438 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
439 fc
->members
= g_array_new(FALSE
, TRUE
,
440 sizeof(struct ctf_named_field_class
));
441 BT_ASSERT(fc
->members
);
442 fc
->base
.is_compound
= true;
447 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
449 struct ctf_field_class_variant
*fc
=
450 g_new0(struct ctf_field_class_variant
, 1);
453 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
454 fc
->options
= g_array_new(FALSE
, TRUE
,
455 sizeof(struct ctf_named_field_class
));
456 BT_ASSERT(fc
->options
);
457 fc
->ranges
= g_array_new(FALSE
, TRUE
,
458 sizeof(struct ctf_field_class_variant_range
));
459 BT_ASSERT(fc
->ranges
);
460 fc
->tag_ref
= g_string_new(NULL
);
461 BT_ASSERT(fc
->tag_ref
);
462 ctf_field_path_init(&fc
->tag_path
);
463 fc
->base
.is_compound
= true;
468 struct ctf_field_class_array
*ctf_field_class_array_create(void)
470 struct ctf_field_class_array
*fc
=
471 g_new0(struct ctf_field_class_array
, 1);
474 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
475 fc
->base
.base
.is_compound
= true;
480 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
482 struct ctf_field_class_sequence
*fc
=
483 g_new0(struct ctf_field_class_sequence
, 1);
486 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
487 fc
->length_ref
= g_string_new(NULL
);
488 BT_ASSERT(fc
->length_ref
);
489 ctf_field_path_init(&fc
->length_path
);
490 fc
->base
.base
.is_compound
= true;
495 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
502 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
509 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
510 struct ctf_field_class_enum_mapping
*mapping
=
511 &g_array_index(fc
->mappings
,
512 struct ctf_field_class_enum_mapping
, i
);
514 _ctf_field_class_enum_mapping_fini(mapping
);
517 g_array_free(fc
->mappings
, TRUE
);
524 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
531 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
538 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
545 for (i
= 0; i
< fc
->members
->len
; i
++) {
546 struct ctf_named_field_class
*named_fc
=
547 &g_array_index(fc
->members
,
548 struct ctf_named_field_class
, i
);
550 _ctf_named_field_class_fini(named_fc
);
553 g_array_free(fc
->members
, TRUE
);
560 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
563 ctf_field_class_destroy(fc
->elem_fc
);
567 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
570 _ctf_field_class_array_base_fini((void *) fc
);
575 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
578 _ctf_field_class_array_base_fini((void *) fc
);
580 if (fc
->length_ref
) {
581 g_string_free(fc
->length_ref
, TRUE
);
584 ctf_field_path_fini(&fc
->length_path
);
589 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
596 for (i
= 0; i
< fc
->options
->len
; i
++) {
597 struct ctf_named_field_class
*named_fc
=
598 &g_array_index(fc
->options
,
599 struct ctf_named_field_class
, i
);
601 _ctf_named_field_class_fini(named_fc
);
604 g_array_free(fc
->options
, TRUE
);
608 g_array_free(fc
->ranges
, TRUE
);
612 g_string_free(fc
->tag_ref
, TRUE
);
615 ctf_field_path_fini(&fc
->tag_path
);
620 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
627 case CTF_FIELD_CLASS_TYPE_INT
:
628 _ctf_field_class_int_destroy((void *) fc
);
630 case CTF_FIELD_CLASS_TYPE_ENUM
:
631 _ctf_field_class_enum_destroy((void *) fc
);
633 case CTF_FIELD_CLASS_TYPE_FLOAT
:
634 _ctf_field_class_float_destroy((void *) fc
);
636 case CTF_FIELD_CLASS_TYPE_STRING
:
637 _ctf_field_class_string_destroy((void *) fc
);
639 case CTF_FIELD_CLASS_TYPE_STRUCT
:
640 _ctf_field_class_struct_destroy((void *) fc
);
642 case CTF_FIELD_CLASS_TYPE_ARRAY
:
643 _ctf_field_class_array_destroy((void *) fc
);
645 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
646 _ctf_field_class_sequence_destroy((void *) fc
);
648 case CTF_FIELD_CLASS_TYPE_VARIANT
:
649 _ctf_field_class_variant_destroy((void *) fc
);
657 void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum
*fc
,
658 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
660 struct ctf_field_class_enum_mapping
*mapping
;
664 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
666 mapping
= &g_array_index(fc
->mappings
,
667 struct ctf_field_class_enum_mapping
, fc
->mappings
->len
- 1);
668 _ctf_field_class_enum_mapping_init(mapping
);
669 g_string_assign(mapping
->label
, label
);
670 mapping
->range
.lower
.u
= u_lower
;
671 mapping
->range
.upper
.u
= u_upper
;
675 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
676 struct ctf_field_class_enum
*fc
, uint64_t index
)
679 BT_ASSERT(index
< fc
->mappings
->len
);
680 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
685 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
686 struct ctf_field_class_struct
*fc
, uint64_t index
)
689 BT_ASSERT(index
< fc
->members
->len
);
690 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
695 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
696 struct ctf_field_class_struct
*fc
, const char *name
)
699 struct ctf_named_field_class
*ret_named_fc
= NULL
;
704 for (i
= 0; i
< fc
->members
->len
; i
++) {
705 struct ctf_named_field_class
*named_fc
=
706 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
708 if (strcmp(name
, named_fc
->name
->str
) == 0) {
709 ret_named_fc
= named_fc
;
719 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
720 struct ctf_field_class_struct
*struct_fc
, const char *name
)
722 struct ctf_named_field_class
*named_fc
= NULL
;
723 struct ctf_field_class
*fc
= NULL
;
729 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
741 struct ctf_field_class_int
*
742 ctf_field_class_struct_borrow_member_int_field_class_by_name(
743 struct ctf_field_class_struct
*struct_fc
, const char *name
)
745 struct ctf_field_class_int
*int_fc
= NULL
;
748 ctf_field_class_struct_borrow_member_field_class_by_name(
754 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
755 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
766 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
767 const char *name
, struct ctf_field_class
*member_fc
)
769 struct ctf_named_field_class
*named_fc
;
773 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
775 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
776 fc
->members
->len
- 1);
777 _ctf_named_field_class_init(named_fc
);
778 g_string_assign(named_fc
->name
, name
);
779 named_fc
->fc
= member_fc
;
781 if (member_fc
->alignment
> fc
->base
.alignment
) {
782 fc
->base
.alignment
= member_fc
->alignment
;
787 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
788 struct ctf_field_class_variant
*fc
, uint64_t index
)
791 BT_ASSERT(index
< fc
->options
->len
);
792 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
797 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
798 struct ctf_field_class_variant
*fc
, const char *name
)
801 struct ctf_named_field_class
*ret_named_fc
= NULL
;
806 for (i
= 0; i
< fc
->options
->len
; i
++) {
807 struct ctf_named_field_class
*named_fc
=
808 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
810 if (strcmp(name
, named_fc
->name
->str
) == 0) {
811 ret_named_fc
= named_fc
;
821 struct ctf_field_class_variant_range
*
822 ctf_field_class_variant_borrow_range_by_index(
823 struct ctf_field_class_variant
*fc
, uint64_t index
)
826 BT_ASSERT(index
< fc
->ranges
->len
);
827 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
832 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
833 const char *name
, struct ctf_field_class
*option_fc
)
835 struct ctf_named_field_class
*named_fc
;
839 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
841 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
842 fc
->options
->len
- 1);
843 _ctf_named_field_class_init(named_fc
);
844 g_string_assign(named_fc
->name
, name
);
845 named_fc
->fc
= option_fc
;
849 void ctf_field_class_variant_set_tag_field_class(
850 struct ctf_field_class_variant
*fc
,
851 struct ctf_field_class_enum
*tag_fc
)
859 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
861 struct ctf_named_field_class
*named_fc
=
862 ctf_field_class_variant_borrow_option_by_index(
865 for (mapping_i
= 0; mapping_i
< tag_fc
->mappings
->len
;
867 struct ctf_field_class_enum_mapping
*mapping
=
868 ctf_field_class_enum_borrow_mapping_by_index(
871 if (strcmp(named_fc
->name
->str
,
872 mapping
->label
->str
) == 0) {
873 struct ctf_field_class_variant_range range
;
875 range
.range
= mapping
->range
;
876 range
.option_index
= option_i
;
877 g_array_append_val(fc
->ranges
, range
);
884 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
885 struct ctf_field_class
*comp_fc
, uint64_t index
)
887 struct ctf_field_class
*fc
= NULL
;
889 switch (comp_fc
->type
) {
890 case CTF_FIELD_CLASS_TYPE_STRUCT
:
892 struct ctf_named_field_class
*named_fc
=
893 ctf_field_class_struct_borrow_member_by_index(
894 (void *) comp_fc
, index
);
900 case CTF_FIELD_CLASS_TYPE_VARIANT
:
902 struct ctf_named_field_class
*named_fc
=
903 ctf_field_class_variant_borrow_option_by_index(
904 (void *) comp_fc
, index
);
910 case CTF_FIELD_CLASS_TYPE_ARRAY
:
911 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
913 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
915 fc
= array_fc
->elem_fc
;
926 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
928 uint64_t field_count
;
931 case CTF_FIELD_CLASS_TYPE_STRUCT
:
933 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
935 field_count
= struct_fc
->members
->len
;
938 case CTF_FIELD_CLASS_TYPE_VARIANT
:
940 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
942 field_count
= var_fc
->options
->len
;
945 case CTF_FIELD_CLASS_TYPE_ARRAY
:
946 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
948 * Array and sequence types always contain a single
949 * member (the element type).
961 int64_t ctf_field_class_compound_get_field_class_index_from_name(
962 struct ctf_field_class
*fc
, const char *name
)
964 int64_t ret_index
= -1;
968 case CTF_FIELD_CLASS_TYPE_STRUCT
:
970 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
972 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
973 struct ctf_named_field_class
*named_fc
=
974 ctf_field_class_struct_borrow_member_by_index(
977 if (strcmp(name
, named_fc
->name
->str
) == 0) {
978 ret_index
= (int64_t) i
;
985 case CTF_FIELD_CLASS_TYPE_VARIANT
:
987 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
989 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
990 struct ctf_named_field_class
*named_fc
=
991 ctf_field_class_variant_borrow_option_by_index(
994 if (strcmp(name
, named_fc
->name
->str
) == 0) {
995 ret_index
= (int64_t) i
;
1011 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1014 g_array_append_val(fp
->path
, index
);
1018 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1022 BT_ASSERT(index
< fp
->path
->len
);
1023 return g_array_index(fp
->path
, int64_t, index
);
1027 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1030 g_array_set_size(fp
->path
, 0);
1034 const char *ctf_scope_string(enum ctf_scope scope
)
1037 case CTF_SCOPE_PACKET_HEADER
:
1038 return "CTF_SCOPE_PACKET_HEADER";
1039 case CTF_SCOPE_PACKET_CONTEXT
:
1040 return "CTF_SCOPE_PACKET_CONTEXT";
1041 case CTF_SCOPE_EVENT_HEADER
:
1042 return "CTF_SCOPE_EVENT_HEADER";
1043 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1044 return "CTF_SCOPE_EVENT_COMMON_CONTEXT";
1045 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1046 return "CTF_SCOPE_EVENT_SPECIFIC_CONTEXT";
1047 case CTF_SCOPE_EVENT_PAYLOAD
:
1048 return "CTF_SCOPE_EVENT_PAYLOAD";
1055 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1057 GString
*str
= g_string_new(NULL
);
1066 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1068 for (i
= 0; i
< path
->path
->len
; i
++) {
1069 g_string_append_printf(str
, ", %" PRId64
,
1070 ctf_field_path_borrow_index_by_index(path
, i
));
1073 g_string_append(str
, "]");
1080 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1081 struct ctf_field_path
*field_path
,
1082 struct ctf_trace_class
*tc
,
1083 struct ctf_stream_class
*sc
,
1084 struct ctf_event_class
*ec
)
1087 struct ctf_field_class
*fc
;
1089 switch (field_path
->root
) {
1090 case CTF_SCOPE_PACKET_HEADER
:
1091 fc
= tc
->packet_header_fc
;
1093 case CTF_SCOPE_PACKET_CONTEXT
:
1094 fc
= sc
->packet_context_fc
;
1096 case CTF_SCOPE_EVENT_HEADER
:
1097 fc
= sc
->event_header_fc
;
1099 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1100 fc
= sc
->event_common_context_fc
;
1102 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1103 fc
= ec
->spec_context_fc
;
1105 case CTF_SCOPE_EVENT_PAYLOAD
:
1106 fc
= ec
->payload_fc
;
1114 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1115 int64_t child_index
=
1116 ctf_field_path_borrow_index_by_index(field_path
, i
);
1117 struct ctf_field_class
*child_fc
=
1118 ctf_field_class_compound_borrow_field_class_by_index(
1120 BT_ASSERT(child_fc
);
1129 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1132 void ctf_field_class_bit_array_copy_content(
1133 struct ctf_field_class_bit_array
*dst_fc
,
1134 struct ctf_field_class_bit_array
*src_fc
)
1138 dst_fc
->byte_order
= src_fc
->byte_order
;
1139 dst_fc
->size
= src_fc
->size
;
1143 void ctf_field_class_int_copy_content(
1144 struct ctf_field_class_int
*dst_fc
,
1145 struct ctf_field_class_int
*src_fc
)
1147 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1148 dst_fc
->meaning
= src_fc
->meaning
;
1149 dst_fc
->is_signed
= src_fc
->is_signed
;
1150 dst_fc
->disp_base
= src_fc
->disp_base
;
1151 dst_fc
->encoding
= src_fc
->encoding
;
1152 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1153 dst_fc
->storing_index
= src_fc
->storing_index
;
1157 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1158 struct ctf_field_class_int
*fc
)
1160 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1163 ctf_field_class_int_copy_content(copy_fc
, fc
);
1168 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1169 struct ctf_field_class_enum
*fc
)
1171 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1175 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1177 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1178 struct ctf_field_class_enum_mapping
*mapping
=
1179 &g_array_index(fc
->mappings
,
1180 struct ctf_field_class_enum_mapping
, i
);
1182 ctf_field_class_enum_append_mapping(copy_fc
, mapping
->label
->str
,
1183 mapping
->range
.lower
.u
, mapping
->range
.upper
.u
);
1190 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1191 struct ctf_field_class_float
*fc
)
1193 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1196 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1201 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1202 struct ctf_field_class_string
*fc
)
1204 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1211 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1212 struct ctf_field_class_struct
*fc
)
1214 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1219 for (i
= 0; i
< fc
->members
->len
; i
++) {
1220 struct ctf_named_field_class
*named_fc
=
1221 &g_array_index(fc
->members
,
1222 struct ctf_named_field_class
, i
);
1224 ctf_field_class_struct_append_member(copy_fc
,
1225 named_fc
->name
->str
,
1226 ctf_field_class_copy(named_fc
->fc
));
1233 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1234 struct ctf_field_path
*src_fp
)
1240 dst_fp
->root
= src_fp
->root
;
1241 ctf_field_path_clear(dst_fp
);
1243 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1244 int64_t index
= ctf_field_path_borrow_index_by_index(
1247 ctf_field_path_append_index(dst_fp
, index
);
1252 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1253 struct ctf_field_class_variant
*fc
)
1255 struct ctf_field_class_variant
*copy_fc
=
1256 ctf_field_class_variant_create();
1261 for (i
= 0; i
< fc
->options
->len
; i
++) {
1262 struct ctf_named_field_class
*named_fc
=
1263 &g_array_index(fc
->options
,
1264 struct ctf_named_field_class
, i
);
1266 ctf_field_class_variant_append_option(copy_fc
,
1267 named_fc
->name
->str
,
1268 ctf_field_class_copy(named_fc
->fc
));
1271 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1272 struct ctf_field_class_variant_range
*range
=
1273 &g_array_index(fc
->ranges
,
1274 struct ctf_field_class_variant_range
, i
);
1276 g_array_append_val(copy_fc
->ranges
, *range
);
1279 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1280 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1281 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1286 void ctf_field_class_array_base_copy_content(
1287 struct ctf_field_class_array_base
*dst_fc
,
1288 struct ctf_field_class_array_base
*src_fc
)
1292 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1293 dst_fc
->is_text
= src_fc
->is_text
;
1297 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1298 struct ctf_field_class_array
*fc
)
1300 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1303 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1304 copy_fc
->length
= fc
->length
;
1309 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1310 struct ctf_field_class_sequence
*fc
)
1312 struct ctf_field_class_sequence
*copy_fc
=
1313 ctf_field_class_sequence_create();
1316 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1317 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1318 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1319 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1324 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1326 struct ctf_field_class
*copy_fc
= NULL
;
1333 * Translation should not have happened yet.
1335 BT_ASSERT(!fc
->ir_fc
);
1338 case CTF_FIELD_CLASS_TYPE_INT
:
1339 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1341 case CTF_FIELD_CLASS_TYPE_ENUM
:
1342 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1344 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1345 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1347 case CTF_FIELD_CLASS_TYPE_STRING
:
1348 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1350 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1351 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1353 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1354 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1356 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1357 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1359 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1360 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1366 copy_fc
->type
= fc
->type
;
1367 copy_fc
->alignment
= fc
->alignment
;
1368 copy_fc
->in_ir
= fc
->in_ir
;
1375 struct ctf_event_class
*ctf_event_class_create(void)
1377 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1380 ec
->name
= g_string_new(NULL
);
1381 BT_ASSERT(ec
->name
);
1382 ec
->emf_uri
= g_string_new(NULL
);
1383 BT_ASSERT(ec
->emf_uri
);
1389 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1396 g_string_free(ec
->name
, TRUE
);
1400 g_string_free(ec
->emf_uri
, TRUE
);
1403 ctf_field_class_destroy(ec
->spec_context_fc
);
1404 ctf_field_class_destroy(ec
->payload_fc
);
1409 struct ctf_stream_class
*ctf_stream_class_create(void)
1411 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1414 sc
->event_classes
= g_ptr_array_new_with_free_func(
1415 (GDestroyNotify
) ctf_event_class_destroy
);
1416 BT_ASSERT(sc
->event_classes
);
1417 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1419 BT_ASSERT(sc
->event_classes_by_id
);
1424 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1430 if (sc
->event_classes
) {
1431 g_ptr_array_free(sc
->event_classes
, TRUE
);
1434 if (sc
->event_classes_by_id
) {
1435 g_hash_table_destroy(sc
->event_classes_by_id
);
1438 ctf_field_class_destroy(sc
->packet_context_fc
);
1439 ctf_field_class_destroy(sc
->event_header_fc
);
1440 ctf_field_class_destroy(sc
->event_common_context_fc
);
1445 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1446 struct ctf_event_class
*ec
)
1448 g_ptr_array_add(sc
->event_classes
, ec
);
1449 g_hash_table_insert(sc
->event_classes_by_id
,
1450 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1454 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1455 struct ctf_stream_class
*sc
, uint64_t type
)
1458 return g_hash_table_lookup(sc
->event_classes_by_id
,
1459 GUINT_TO_POINTER((guint
) type
));
1463 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1466 entry
->name
= g_string_new(NULL
);
1467 BT_ASSERT(entry
->name
);
1468 entry
->value
.str
= g_string_new(NULL
);
1469 BT_ASSERT(entry
->value
.str
);
1473 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1478 g_string_free(entry
->name
, TRUE
);
1481 if (entry
->value
.str
) {
1482 g_string_free(entry
->value
.str
, TRUE
);
1487 struct ctf_clock_class
*ctf_clock_class_create(void)
1489 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1492 cc
->name
= g_string_new(NULL
);
1493 BT_ASSERT(cc
->name
);
1494 cc
->description
= g_string_new(NULL
);
1495 BT_ASSERT(cc
->description
);
1500 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1507 g_string_free(cc
->name
, TRUE
);
1510 if (cc
->description
) {
1511 g_string_free(cc
->description
, TRUE
);
1514 bt_clock_class_put_ref(cc
->ir_cc
);
1519 struct ctf_trace_class
*ctf_trace_class_create(void)
1521 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1524 tc
->default_byte_order
= -1;
1525 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1526 (GDestroyNotify
) ctf_clock_class_destroy
);
1527 BT_ASSERT(tc
->clock_classes
);
1528 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1529 (GDestroyNotify
) ctf_stream_class_destroy
);
1530 BT_ASSERT(tc
->stream_classes
);
1531 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1532 sizeof(struct ctf_trace_class_env_entry
));
1537 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1543 ctf_field_class_destroy(tc
->packet_header_fc
);
1545 if (tc
->clock_classes
) {
1546 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1549 if (tc
->stream_classes
) {
1550 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1553 if (tc
->env_entries
) {
1556 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1557 struct ctf_trace_class_env_entry
*entry
=
1558 &g_array_index(tc
->env_entries
,
1559 struct ctf_trace_class_env_entry
, i
);
1561 _ctf_trace_class_env_entry_fini(entry
);
1564 g_array_free(tc
->env_entries
, TRUE
);
1571 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1572 const char *name
, enum ctf_trace_class_env_entry_type type
,
1573 const char *str_value
, int64_t i_value
)
1575 struct ctf_trace_class_env_entry
*entry
;
1579 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1581 entry
= &g_array_index(tc
->env_entries
,
1582 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1584 _ctf_trace_class_env_entry_init(entry
);
1585 g_string_assign(entry
->name
, name
);
1588 g_string_assign(entry
->value
.str
, str_value
);
1591 entry
->value
.i
= i_value
;
1595 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1596 struct ctf_trace_class
*tc
, uint64_t id
)
1599 struct ctf_stream_class
*ret_sc
= NULL
;
1603 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1604 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1617 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1618 struct ctf_trace_class
*tc
, const char *name
)
1621 struct ctf_clock_class
*ret_cc
= NULL
;
1626 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1627 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1629 BT_ASSERT(cc
->name
);
1630 if (strcmp(cc
->name
->str
, name
) == 0) {
1641 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1642 struct ctf_trace_class
*tc
, uint64_t index
)
1645 BT_ASSERT(index
< tc
->env_entries
->len
);
1646 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1651 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1652 struct ctf_trace_class
*tc
, const char *name
)
1654 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1660 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1661 struct ctf_trace_class_env_entry
*env_entry
=
1662 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1664 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1665 ret_entry
= env_entry
;
1674 #endif /* _CTF_META_H */