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 <babeltrace2/babeltrace.h>
19 #include "common/common.h"
20 #include "common/assert.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
{
231 bool packets_have_ts_begin
;
232 bool packets_have_ts_end
;
233 bool has_discarded_events
;
234 bool has_discarded_packets
;
235 bool discarded_events_have_default_cs
;
236 bool discarded_packets_have_default_cs
;
239 struct ctf_field_class
*packet_context_fc
;
242 struct ctf_field_class
*event_header_fc
;
245 struct ctf_field_class
*event_common_context_fc
;
247 /* Array of `struct ctf_event_class *`, owned by this */
248 GPtrArray
*event_classes
;
251 * Hash table mapping event class IDs to `struct ctf_event_class *`,
254 GHashTable
*event_classes_by_id
;
257 struct ctf_clock_class
*default_clock_class
;
259 /* Weak, set during translation */
260 bt_stream_class
*ir_sc
;
263 enum ctf_trace_class_env_entry_type
{
264 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
265 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
268 struct ctf_trace_class_env_entry
{
269 enum ctf_trace_class_env_entry_type type
;
278 struct ctf_trace_class
{
283 enum ctf_byte_order default_byte_order
;
286 struct ctf_field_class
*packet_header_fc
;
288 uint64_t stored_value_count
;
290 /* Array of `struct ctf_clock_class *` (owned by this) */
291 GPtrArray
*clock_classes
;
293 /* Array of `struct ctf_stream_class *` */
294 GPtrArray
*stream_classes
;
296 /* Array of `struct ctf_trace_class_env_entry` */
301 /* Weak, set during translation */
302 bt_trace_class
*ir_tc
;
306 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
309 void _ctf_field_class_init(struct ctf_field_class
*fc
,
310 enum ctf_field_class_type type
, unsigned int alignment
)
314 fc
->alignment
= alignment
;
319 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
320 enum ctf_field_class_type type
)
322 _ctf_field_class_init((void *) fc
, type
, 1);
326 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
327 enum ctf_field_class_type type
)
329 _ctf_field_class_bit_array_init((void *) fc
, type
);
330 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
331 fc
->storing_index
= -1;
335 void ctf_field_path_init(struct ctf_field_path
*field_path
)
337 BT_ASSERT(field_path
);
338 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
339 BT_ASSERT(field_path
->path
);
343 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
345 BT_ASSERT(field_path
);
347 if (field_path
->path
) {
348 g_array_free(field_path
->path
, TRUE
);
353 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
356 named_fc
->name
= g_string_new(NULL
);
357 BT_ASSERT(named_fc
->name
);
361 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
365 if (named_fc
->name
) {
366 g_string_free(named_fc
->name
, TRUE
);
369 ctf_field_class_destroy(named_fc
->fc
);
373 void _ctf_field_class_enum_mapping_init(
374 struct ctf_field_class_enum_mapping
*mapping
)
377 mapping
->label
= g_string_new(NULL
);
378 BT_ASSERT(mapping
->label
);
382 void _ctf_field_class_enum_mapping_fini(
383 struct ctf_field_class_enum_mapping
*mapping
)
387 if (mapping
->label
) {
388 g_string_free(mapping
->label
, TRUE
);
393 struct ctf_field_class_int
*ctf_field_class_int_create(void)
395 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
398 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
403 struct ctf_field_class_float
*ctf_field_class_float_create(void)
405 struct ctf_field_class_float
*fc
=
406 g_new0(struct ctf_field_class_float
, 1);
409 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
414 struct ctf_field_class_string
*ctf_field_class_string_create(void)
416 struct ctf_field_class_string
*fc
=
417 g_new0(struct ctf_field_class_string
, 1);
420 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
425 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
427 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
430 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
431 fc
->mappings
= g_array_new(FALSE
, TRUE
,
432 sizeof(struct ctf_field_class_enum_mapping
));
433 BT_ASSERT(fc
->mappings
);
438 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
440 struct ctf_field_class_struct
*fc
=
441 g_new0(struct ctf_field_class_struct
, 1);
444 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
445 fc
->members
= g_array_new(FALSE
, TRUE
,
446 sizeof(struct ctf_named_field_class
));
447 BT_ASSERT(fc
->members
);
448 fc
->base
.is_compound
= true;
453 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
455 struct ctf_field_class_variant
*fc
=
456 g_new0(struct ctf_field_class_variant
, 1);
459 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
460 fc
->options
= g_array_new(FALSE
, TRUE
,
461 sizeof(struct ctf_named_field_class
));
462 BT_ASSERT(fc
->options
);
463 fc
->ranges
= g_array_new(FALSE
, TRUE
,
464 sizeof(struct ctf_field_class_variant_range
));
465 BT_ASSERT(fc
->ranges
);
466 fc
->tag_ref
= g_string_new(NULL
);
467 BT_ASSERT(fc
->tag_ref
);
468 ctf_field_path_init(&fc
->tag_path
);
469 fc
->base
.is_compound
= true;
474 struct ctf_field_class_array
*ctf_field_class_array_create(void)
476 struct ctf_field_class_array
*fc
=
477 g_new0(struct ctf_field_class_array
, 1);
480 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
481 fc
->base
.base
.is_compound
= true;
486 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
488 struct ctf_field_class_sequence
*fc
=
489 g_new0(struct ctf_field_class_sequence
, 1);
492 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
493 fc
->length_ref
= g_string_new(NULL
);
494 BT_ASSERT(fc
->length_ref
);
495 ctf_field_path_init(&fc
->length_path
);
496 fc
->base
.base
.is_compound
= true;
501 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
508 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
515 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
516 struct ctf_field_class_enum_mapping
*mapping
=
517 &g_array_index(fc
->mappings
,
518 struct ctf_field_class_enum_mapping
, i
);
520 _ctf_field_class_enum_mapping_fini(mapping
);
523 g_array_free(fc
->mappings
, TRUE
);
530 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
537 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
544 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
551 for (i
= 0; i
< fc
->members
->len
; i
++) {
552 struct ctf_named_field_class
*named_fc
=
553 &g_array_index(fc
->members
,
554 struct ctf_named_field_class
, i
);
556 _ctf_named_field_class_fini(named_fc
);
559 g_array_free(fc
->members
, TRUE
);
566 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
569 ctf_field_class_destroy(fc
->elem_fc
);
573 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
576 _ctf_field_class_array_base_fini((void *) fc
);
581 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
584 _ctf_field_class_array_base_fini((void *) fc
);
586 if (fc
->length_ref
) {
587 g_string_free(fc
->length_ref
, TRUE
);
590 ctf_field_path_fini(&fc
->length_path
);
595 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
602 for (i
= 0; i
< fc
->options
->len
; i
++) {
603 struct ctf_named_field_class
*named_fc
=
604 &g_array_index(fc
->options
,
605 struct ctf_named_field_class
, i
);
607 _ctf_named_field_class_fini(named_fc
);
610 g_array_free(fc
->options
, TRUE
);
614 g_array_free(fc
->ranges
, TRUE
);
618 g_string_free(fc
->tag_ref
, TRUE
);
621 ctf_field_path_fini(&fc
->tag_path
);
626 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
633 case CTF_FIELD_CLASS_TYPE_INT
:
634 _ctf_field_class_int_destroy((void *) fc
);
636 case CTF_FIELD_CLASS_TYPE_ENUM
:
637 _ctf_field_class_enum_destroy((void *) fc
);
639 case CTF_FIELD_CLASS_TYPE_FLOAT
:
640 _ctf_field_class_float_destroy((void *) fc
);
642 case CTF_FIELD_CLASS_TYPE_STRING
:
643 _ctf_field_class_string_destroy((void *) fc
);
645 case CTF_FIELD_CLASS_TYPE_STRUCT
:
646 _ctf_field_class_struct_destroy((void *) fc
);
648 case CTF_FIELD_CLASS_TYPE_ARRAY
:
649 _ctf_field_class_array_destroy((void *) fc
);
651 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
652 _ctf_field_class_sequence_destroy((void *) fc
);
654 case CTF_FIELD_CLASS_TYPE_VARIANT
:
655 _ctf_field_class_variant_destroy((void *) fc
);
663 void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum
*fc
,
664 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
666 struct ctf_field_class_enum_mapping
*mapping
;
670 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
672 mapping
= &g_array_index(fc
->mappings
,
673 struct ctf_field_class_enum_mapping
, fc
->mappings
->len
- 1);
674 _ctf_field_class_enum_mapping_init(mapping
);
675 g_string_assign(mapping
->label
, label
);
676 mapping
->range
.lower
.u
= u_lower
;
677 mapping
->range
.upper
.u
= u_upper
;
681 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
682 struct ctf_field_class_enum
*fc
, uint64_t index
)
685 BT_ASSERT(index
< fc
->mappings
->len
);
686 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
691 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
692 struct ctf_field_class_struct
*fc
, uint64_t index
)
695 BT_ASSERT(index
< fc
->members
->len
);
696 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
701 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
702 struct ctf_field_class_struct
*fc
, const char *name
)
705 struct ctf_named_field_class
*ret_named_fc
= NULL
;
710 for (i
= 0; i
< fc
->members
->len
; i
++) {
711 struct ctf_named_field_class
*named_fc
=
712 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
714 if (strcmp(name
, named_fc
->name
->str
) == 0) {
715 ret_named_fc
= named_fc
;
725 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
726 struct ctf_field_class_struct
*struct_fc
, const char *name
)
728 struct ctf_named_field_class
*named_fc
= NULL
;
729 struct ctf_field_class
*fc
= NULL
;
735 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
747 struct ctf_field_class_int
*
748 ctf_field_class_struct_borrow_member_int_field_class_by_name(
749 struct ctf_field_class_struct
*struct_fc
, const char *name
)
751 struct ctf_field_class_int
*int_fc
= NULL
;
754 ctf_field_class_struct_borrow_member_field_class_by_name(
760 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
761 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
772 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
773 const char *name
, struct ctf_field_class
*member_fc
)
775 struct ctf_named_field_class
*named_fc
;
779 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
781 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
782 fc
->members
->len
- 1);
783 _ctf_named_field_class_init(named_fc
);
784 g_string_assign(named_fc
->name
, name
);
785 named_fc
->fc
= member_fc
;
787 if (member_fc
->alignment
> fc
->base
.alignment
) {
788 fc
->base
.alignment
= member_fc
->alignment
;
793 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
794 struct ctf_field_class_variant
*fc
, uint64_t index
)
797 BT_ASSERT(index
< fc
->options
->len
);
798 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
803 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
804 struct ctf_field_class_variant
*fc
, const char *name
)
807 struct ctf_named_field_class
*ret_named_fc
= NULL
;
812 for (i
= 0; i
< fc
->options
->len
; i
++) {
813 struct ctf_named_field_class
*named_fc
=
814 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
816 if (strcmp(name
, named_fc
->name
->str
) == 0) {
817 ret_named_fc
= named_fc
;
827 struct ctf_field_class_variant_range
*
828 ctf_field_class_variant_borrow_range_by_index(
829 struct ctf_field_class_variant
*fc
, uint64_t index
)
832 BT_ASSERT(index
< fc
->ranges
->len
);
833 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
838 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
839 const char *name
, struct ctf_field_class
*option_fc
)
841 struct ctf_named_field_class
*named_fc
;
845 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
847 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
848 fc
->options
->len
- 1);
849 _ctf_named_field_class_init(named_fc
);
850 g_string_assign(named_fc
->name
, name
);
851 named_fc
->fc
= option_fc
;
855 void ctf_field_class_variant_set_tag_field_class(
856 struct ctf_field_class_variant
*fc
,
857 struct ctf_field_class_enum
*tag_fc
)
865 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
867 struct ctf_named_field_class
*named_fc
=
868 ctf_field_class_variant_borrow_option_by_index(
871 for (mapping_i
= 0; mapping_i
< tag_fc
->mappings
->len
;
873 struct ctf_field_class_enum_mapping
*mapping
=
874 ctf_field_class_enum_borrow_mapping_by_index(
877 if (strcmp(named_fc
->name
->str
,
878 mapping
->label
->str
) == 0) {
879 struct ctf_field_class_variant_range range
;
881 range
.range
= mapping
->range
;
882 range
.option_index
= option_i
;
883 g_array_append_val(fc
->ranges
, range
);
890 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
891 struct ctf_field_class
*comp_fc
, uint64_t index
)
893 struct ctf_field_class
*fc
= NULL
;
895 switch (comp_fc
->type
) {
896 case CTF_FIELD_CLASS_TYPE_STRUCT
:
898 struct ctf_named_field_class
*named_fc
=
899 ctf_field_class_struct_borrow_member_by_index(
900 (void *) comp_fc
, index
);
906 case CTF_FIELD_CLASS_TYPE_VARIANT
:
908 struct ctf_named_field_class
*named_fc
=
909 ctf_field_class_variant_borrow_option_by_index(
910 (void *) comp_fc
, index
);
916 case CTF_FIELD_CLASS_TYPE_ARRAY
:
917 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
919 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
921 fc
= array_fc
->elem_fc
;
932 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
934 uint64_t field_count
;
937 case CTF_FIELD_CLASS_TYPE_STRUCT
:
939 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
941 field_count
= struct_fc
->members
->len
;
944 case CTF_FIELD_CLASS_TYPE_VARIANT
:
946 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
948 field_count
= var_fc
->options
->len
;
951 case CTF_FIELD_CLASS_TYPE_ARRAY
:
952 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
954 * Array and sequence types always contain a single
955 * member (the element type).
967 int64_t ctf_field_class_compound_get_field_class_index_from_name(
968 struct ctf_field_class
*fc
, const char *name
)
970 int64_t ret_index
= -1;
974 case CTF_FIELD_CLASS_TYPE_STRUCT
:
976 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
978 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
979 struct ctf_named_field_class
*named_fc
=
980 ctf_field_class_struct_borrow_member_by_index(
983 if (strcmp(name
, named_fc
->name
->str
) == 0) {
984 ret_index
= (int64_t) i
;
991 case CTF_FIELD_CLASS_TYPE_VARIANT
:
993 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
995 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
996 struct ctf_named_field_class
*named_fc
=
997 ctf_field_class_variant_borrow_option_by_index(
1000 if (strcmp(name
, named_fc
->name
->str
) == 0) {
1001 ret_index
= (int64_t) i
;
1017 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1020 g_array_append_val(fp
->path
, index
);
1024 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1028 BT_ASSERT(index
< fp
->path
->len
);
1029 return g_array_index(fp
->path
, int64_t, index
);
1033 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1036 g_array_set_size(fp
->path
, 0);
1040 const char *ctf_scope_string(enum ctf_scope scope
)
1043 case CTF_SCOPE_PACKET_HEADER
:
1044 return "CTF_SCOPE_PACKET_HEADER";
1045 case CTF_SCOPE_PACKET_CONTEXT
:
1046 return "CTF_SCOPE_PACKET_CONTEXT";
1047 case CTF_SCOPE_EVENT_HEADER
:
1048 return "CTF_SCOPE_EVENT_HEADER";
1049 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1050 return "CTF_SCOPE_EVENT_COMMON_CONTEXT";
1051 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1052 return "CTF_SCOPE_EVENT_SPECIFIC_CONTEXT";
1053 case CTF_SCOPE_EVENT_PAYLOAD
:
1054 return "CTF_SCOPE_EVENT_PAYLOAD";
1061 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1063 GString
*str
= g_string_new(NULL
);
1072 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1074 for (i
= 0; i
< path
->path
->len
; i
++) {
1075 g_string_append_printf(str
, ", %" PRId64
,
1076 ctf_field_path_borrow_index_by_index(path
, i
));
1079 g_string_append(str
, "]");
1086 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1087 struct ctf_field_path
*field_path
,
1088 struct ctf_trace_class
*tc
,
1089 struct ctf_stream_class
*sc
,
1090 struct ctf_event_class
*ec
)
1093 struct ctf_field_class
*fc
;
1095 switch (field_path
->root
) {
1096 case CTF_SCOPE_PACKET_HEADER
:
1097 fc
= tc
->packet_header_fc
;
1099 case CTF_SCOPE_PACKET_CONTEXT
:
1100 fc
= sc
->packet_context_fc
;
1102 case CTF_SCOPE_EVENT_HEADER
:
1103 fc
= sc
->event_header_fc
;
1105 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1106 fc
= sc
->event_common_context_fc
;
1108 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1109 fc
= ec
->spec_context_fc
;
1111 case CTF_SCOPE_EVENT_PAYLOAD
:
1112 fc
= ec
->payload_fc
;
1120 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1121 int64_t child_index
=
1122 ctf_field_path_borrow_index_by_index(field_path
, i
);
1123 struct ctf_field_class
*child_fc
=
1124 ctf_field_class_compound_borrow_field_class_by_index(
1126 BT_ASSERT(child_fc
);
1135 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1138 void ctf_field_class_bit_array_copy_content(
1139 struct ctf_field_class_bit_array
*dst_fc
,
1140 struct ctf_field_class_bit_array
*src_fc
)
1144 dst_fc
->byte_order
= src_fc
->byte_order
;
1145 dst_fc
->size
= src_fc
->size
;
1149 void ctf_field_class_int_copy_content(
1150 struct ctf_field_class_int
*dst_fc
,
1151 struct ctf_field_class_int
*src_fc
)
1153 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1154 dst_fc
->meaning
= src_fc
->meaning
;
1155 dst_fc
->is_signed
= src_fc
->is_signed
;
1156 dst_fc
->disp_base
= src_fc
->disp_base
;
1157 dst_fc
->encoding
= src_fc
->encoding
;
1158 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1159 dst_fc
->storing_index
= src_fc
->storing_index
;
1163 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1164 struct ctf_field_class_int
*fc
)
1166 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1169 ctf_field_class_int_copy_content(copy_fc
, fc
);
1174 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1175 struct ctf_field_class_enum
*fc
)
1177 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1181 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1183 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1184 struct ctf_field_class_enum_mapping
*mapping
=
1185 &g_array_index(fc
->mappings
,
1186 struct ctf_field_class_enum_mapping
, i
);
1188 ctf_field_class_enum_append_mapping(copy_fc
, mapping
->label
->str
,
1189 mapping
->range
.lower
.u
, mapping
->range
.upper
.u
);
1196 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1197 struct ctf_field_class_float
*fc
)
1199 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1202 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1207 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1208 struct ctf_field_class_string
*fc
)
1210 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1217 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1218 struct ctf_field_class_struct
*fc
)
1220 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1225 for (i
= 0; i
< fc
->members
->len
; i
++) {
1226 struct ctf_named_field_class
*named_fc
=
1227 &g_array_index(fc
->members
,
1228 struct ctf_named_field_class
, i
);
1230 ctf_field_class_struct_append_member(copy_fc
,
1231 named_fc
->name
->str
,
1232 ctf_field_class_copy(named_fc
->fc
));
1239 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1240 struct ctf_field_path
*src_fp
)
1246 dst_fp
->root
= src_fp
->root
;
1247 ctf_field_path_clear(dst_fp
);
1249 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1250 int64_t index
= ctf_field_path_borrow_index_by_index(
1253 ctf_field_path_append_index(dst_fp
, index
);
1258 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1259 struct ctf_field_class_variant
*fc
)
1261 struct ctf_field_class_variant
*copy_fc
=
1262 ctf_field_class_variant_create();
1267 for (i
= 0; i
< fc
->options
->len
; i
++) {
1268 struct ctf_named_field_class
*named_fc
=
1269 &g_array_index(fc
->options
,
1270 struct ctf_named_field_class
, i
);
1272 ctf_field_class_variant_append_option(copy_fc
,
1273 named_fc
->name
->str
,
1274 ctf_field_class_copy(named_fc
->fc
));
1277 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1278 struct ctf_field_class_variant_range
*range
=
1279 &g_array_index(fc
->ranges
,
1280 struct ctf_field_class_variant_range
, i
);
1282 g_array_append_val(copy_fc
->ranges
, *range
);
1285 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1286 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1287 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1292 void ctf_field_class_array_base_copy_content(
1293 struct ctf_field_class_array_base
*dst_fc
,
1294 struct ctf_field_class_array_base
*src_fc
)
1298 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1299 dst_fc
->is_text
= src_fc
->is_text
;
1303 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1304 struct ctf_field_class_array
*fc
)
1306 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1309 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1310 copy_fc
->length
= fc
->length
;
1315 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1316 struct ctf_field_class_sequence
*fc
)
1318 struct ctf_field_class_sequence
*copy_fc
=
1319 ctf_field_class_sequence_create();
1322 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1323 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1324 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1325 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1330 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1332 struct ctf_field_class
*copy_fc
= NULL
;
1339 * Translation should not have happened yet.
1341 BT_ASSERT(!fc
->ir_fc
);
1344 case CTF_FIELD_CLASS_TYPE_INT
:
1345 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1347 case CTF_FIELD_CLASS_TYPE_ENUM
:
1348 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1350 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1351 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1353 case CTF_FIELD_CLASS_TYPE_STRING
:
1354 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1356 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1357 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1359 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1360 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1362 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1363 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1365 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1366 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1372 copy_fc
->type
= fc
->type
;
1373 copy_fc
->alignment
= fc
->alignment
;
1374 copy_fc
->in_ir
= fc
->in_ir
;
1381 struct ctf_event_class
*ctf_event_class_create(void)
1383 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1386 ec
->name
= g_string_new(NULL
);
1387 BT_ASSERT(ec
->name
);
1388 ec
->emf_uri
= g_string_new(NULL
);
1389 BT_ASSERT(ec
->emf_uri
);
1395 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1402 g_string_free(ec
->name
, TRUE
);
1406 g_string_free(ec
->emf_uri
, TRUE
);
1409 ctf_field_class_destroy(ec
->spec_context_fc
);
1410 ctf_field_class_destroy(ec
->payload_fc
);
1415 struct ctf_stream_class
*ctf_stream_class_create(void)
1417 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1420 sc
->event_classes
= g_ptr_array_new_with_free_func(
1421 (GDestroyNotify
) ctf_event_class_destroy
);
1422 BT_ASSERT(sc
->event_classes
);
1423 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1425 BT_ASSERT(sc
->event_classes_by_id
);
1430 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1436 if (sc
->event_classes
) {
1437 g_ptr_array_free(sc
->event_classes
, TRUE
);
1440 if (sc
->event_classes_by_id
) {
1441 g_hash_table_destroy(sc
->event_classes_by_id
);
1444 ctf_field_class_destroy(sc
->packet_context_fc
);
1445 ctf_field_class_destroy(sc
->event_header_fc
);
1446 ctf_field_class_destroy(sc
->event_common_context_fc
);
1451 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1452 struct ctf_event_class
*ec
)
1454 g_ptr_array_add(sc
->event_classes
, ec
);
1455 g_hash_table_insert(sc
->event_classes_by_id
,
1456 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1460 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1461 struct ctf_stream_class
*sc
, uint64_t type
)
1464 return g_hash_table_lookup(sc
->event_classes_by_id
,
1465 GUINT_TO_POINTER((guint
) type
));
1469 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1472 entry
->name
= g_string_new(NULL
);
1473 BT_ASSERT(entry
->name
);
1474 entry
->value
.str
= g_string_new(NULL
);
1475 BT_ASSERT(entry
->value
.str
);
1479 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1484 g_string_free(entry
->name
, TRUE
);
1487 if (entry
->value
.str
) {
1488 g_string_free(entry
->value
.str
, TRUE
);
1493 struct ctf_clock_class
*ctf_clock_class_create(void)
1495 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1498 cc
->name
= g_string_new(NULL
);
1499 BT_ASSERT(cc
->name
);
1500 cc
->description
= g_string_new(NULL
);
1501 BT_ASSERT(cc
->description
);
1506 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1513 g_string_free(cc
->name
, TRUE
);
1516 if (cc
->description
) {
1517 g_string_free(cc
->description
, TRUE
);
1520 bt_clock_class_put_ref(cc
->ir_cc
);
1525 struct ctf_trace_class
*ctf_trace_class_create(void)
1527 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1530 tc
->default_byte_order
= -1;
1531 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1532 (GDestroyNotify
) ctf_clock_class_destroy
);
1533 BT_ASSERT(tc
->clock_classes
);
1534 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1535 (GDestroyNotify
) ctf_stream_class_destroy
);
1536 BT_ASSERT(tc
->stream_classes
);
1537 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1538 sizeof(struct ctf_trace_class_env_entry
));
1543 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1549 ctf_field_class_destroy(tc
->packet_header_fc
);
1551 if (tc
->clock_classes
) {
1552 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1555 if (tc
->stream_classes
) {
1556 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1559 if (tc
->env_entries
) {
1562 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1563 struct ctf_trace_class_env_entry
*entry
=
1564 &g_array_index(tc
->env_entries
,
1565 struct ctf_trace_class_env_entry
, i
);
1567 _ctf_trace_class_env_entry_fini(entry
);
1570 g_array_free(tc
->env_entries
, TRUE
);
1577 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1578 const char *name
, enum ctf_trace_class_env_entry_type type
,
1579 const char *str_value
, int64_t i_value
)
1581 struct ctf_trace_class_env_entry
*entry
;
1585 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1587 entry
= &g_array_index(tc
->env_entries
,
1588 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1590 _ctf_trace_class_env_entry_init(entry
);
1591 g_string_assign(entry
->name
, name
);
1594 g_string_assign(entry
->value
.str
, str_value
);
1597 entry
->value
.i
= i_value
;
1601 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1602 struct ctf_trace_class
*tc
, uint64_t id
)
1605 struct ctf_stream_class
*ret_sc
= NULL
;
1609 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1610 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1623 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1624 struct ctf_trace_class
*tc
, const char *name
)
1627 struct ctf_clock_class
*ret_cc
= NULL
;
1632 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1633 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1635 BT_ASSERT(cc
->name
);
1636 if (strcmp(cc
->name
->str
, name
) == 0) {
1647 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1648 struct ctf_trace_class
*tc
, uint64_t index
)
1651 BT_ASSERT(index
< tc
->env_entries
->len
);
1652 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1657 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1658 struct ctf_trace_class
*tc
, const char *name
)
1660 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1666 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1667 struct ctf_trace_class_env_entry
*env_entry
=
1668 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1670 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1671 ret_entry
= env_entry
;
1680 #endif /* _CTF_META_H */