2 * SPDX-License-Identifier: MIT
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
14 #include <babeltrace2/babeltrace.h>
16 #include "common/assert.h"
17 #include "common/common.h"
18 #include "common/uuid.h"
20 enum ctf_field_class_type
22 CTF_FIELD_CLASS_TYPE_INT,
23 CTF_FIELD_CLASS_TYPE_ENUM,
24 CTF_FIELD_CLASS_TYPE_FLOAT,
25 CTF_FIELD_CLASS_TYPE_STRING,
26 CTF_FIELD_CLASS_TYPE_STRUCT,
27 CTF_FIELD_CLASS_TYPE_ARRAY,
28 CTF_FIELD_CLASS_TYPE_SEQUENCE,
29 CTF_FIELD_CLASS_TYPE_VARIANT,
32 enum ctf_field_class_meaning
34 CTF_FIELD_CLASS_MEANING_NONE,
35 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME,
36 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME,
37 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID,
38 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID,
39 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID,
40 CTF_FIELD_CLASS_MEANING_MAGIC,
41 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT,
42 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT,
43 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE,
44 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE,
45 CTF_FIELD_CLASS_MEANING_UUID,
50 CTF_BYTE_ORDER_UNKNOWN,
51 CTF_BYTE_ORDER_DEFAULT,
52 CTF_BYTE_ORDER_LITTLE,
64 CTF_SCOPE_PACKET_UNKNOWN = -1,
65 CTF_SCOPE_PACKET_HEADER = 0,
66 CTF_SCOPE_PACKET_CONTEXT,
67 CTF_SCOPE_EVENT_HEADER,
68 CTF_SCOPE_EVENT_COMMON_CONTEXT,
69 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT,
70 CTF_SCOPE_EVENT_PAYLOAD,
73 struct ctf_clock_class
79 int64_t offset_seconds;
80 uint64_t offset_cycles;
85 /* Weak, set during translation */
86 bt_clock_class *ir_cc;
89 struct ctf_field_class
91 enum ctf_field_class_type type;
92 unsigned int alignment;
96 /* Weak, set during translation. NULL if `in_ir` is false below. */
97 bt_field_class *ir_fc;
100 struct ctf_field_class_bit_array
102 struct ctf_field_class base;
103 enum ctf_byte_order byte_order;
107 struct ctf_field_class_int
109 struct ctf_field_class_bit_array base;
110 enum ctf_field_class_meaning meaning;
112 bt_field_class_integer_preferred_display_base disp_base;
113 enum ctf_encoding encoding;
114 int64_t storing_index;
117 struct ctf_clock_class *mapped_clock_class;
135 struct ctf_field_class_enum_mapping
139 /* Array of `struct ctf_range` */
143 struct ctf_field_class_enum
145 struct ctf_field_class_int base;
147 /* Array of `struct ctf_field_class_enum_mapping` */
151 struct ctf_field_class_float
153 struct ctf_field_class_bit_array base;
156 struct ctf_field_class_string
158 struct ctf_field_class base;
159 enum ctf_encoding encoding;
162 struct ctf_named_field_class
164 /* Original name which can include a leading `_` */
167 /* Name as translated to trace IR (leading `_` removed) */
171 struct ctf_field_class *fc;
174 struct ctf_field_class_struct
176 struct ctf_field_class base;
178 /* Array of `struct ctf_named_field_class` */
182 struct ctf_field_path
186 /* Array of `int64_t` */
190 struct ctf_field_class_variant_range
192 struct ctf_range range;
193 uint64_t option_index;
196 struct ctf_field_class_variant
198 struct ctf_field_class base;
200 struct ctf_field_path tag_path;
201 uint64_t stored_tag_index;
203 /* Array of `struct ctf_named_field_class` */
206 /* Array of `struct ctf_field_class_variant_range` */
210 struct ctf_field_class_enum *tag_fc;
213 struct ctf_field_class_array_base
215 struct ctf_field_class base;
216 struct ctf_field_class *elem_fc;
220 struct ctf_field_class_array
222 struct ctf_field_class_array_base base;
223 enum ctf_field_class_meaning meaning;
227 struct ctf_field_class_sequence
229 struct ctf_field_class_array_base base;
231 struct ctf_field_path length_path;
232 uint64_t stored_length_index;
235 struct ctf_field_class_int *length_fc;
238 struct ctf_event_class
243 bt_event_class_log_level log_level;
245 bool is_log_level_set;
248 struct ctf_field_class *spec_context_fc;
251 struct ctf_field_class *payload_fc;
253 /* Weak, set during translation */
254 bt_event_class *ir_ec;
257 struct ctf_stream_class
261 bool packets_have_ts_begin;
262 bool packets_have_ts_end;
263 bool has_discarded_events;
264 bool has_discarded_packets;
265 bool discarded_events_have_default_cs;
266 bool discarded_packets_have_default_cs;
269 struct ctf_field_class *packet_context_fc;
272 struct ctf_field_class *event_header_fc;
275 struct ctf_field_class *event_common_context_fc;
277 /* Array of `struct ctf_event_class *`, owned by this */
278 GPtrArray *event_classes;
281 * Hash table mapping event class IDs to `struct ctf_event_class *`,
284 GHashTable *event_classes_by_id;
287 struct ctf_clock_class *default_clock_class;
289 /* Weak, set during translation */
290 bt_stream_class *ir_sc;
293 enum ctf_trace_class_env_entry_type
295 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
296 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
299 struct ctf_trace_class_env_entry
301 enum ctf_trace_class_env_entry_type type;
311 struct ctf_trace_class
317 enum ctf_byte_order default_byte_order;
320 struct ctf_field_class *packet_header_fc;
322 uint64_t stored_value_count;
324 /* Array of `struct ctf_clock_class *` (owned by this) */
325 GPtrArray *clock_classes;
327 /* Array of `struct ctf_stream_class *` */
328 GPtrArray *stream_classes;
330 /* Array of `struct ctf_trace_class_env_entry` */
335 /* Weak, set during translation */
336 bt_trace_class *ir_tc;
341 bool lttng_event_after_packet;
342 bool barectf_event_before_packet;
346 static inline ctf_field_class_bit_array *ctf_field_class_as_bit_array(ctf_field_class *fc)
349 (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM ||
350 fc->type == CTF_FIELD_CLASS_TYPE_FLOAT));
351 return (ctf_field_class_bit_array *) fc;
354 static inline ctf_field_class_int *ctf_field_class_as_int(ctf_field_class *fc)
357 (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM));
358 return (ctf_field_class_int *) fc;
361 static inline ctf_field_class_enum *ctf_field_class_as_enum(ctf_field_class *fc)
363 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ENUM);
364 return (ctf_field_class_enum *) fc;
367 static inline ctf_field_class_float *ctf_field_class_as_float(ctf_field_class *fc)
369 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_FLOAT);
370 return (ctf_field_class_float *) fc;
373 static inline ctf_field_class_string *ctf_field_class_as_string(ctf_field_class *fc)
375 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRING);
376 return (ctf_field_class_string *) fc;
379 static inline ctf_field_class_struct *ctf_field_class_as_struct(ctf_field_class *fc)
381 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRUCT);
382 return (ctf_field_class_struct *) fc;
385 static inline ctf_field_class_array_base *ctf_field_class_as_array_base(ctf_field_class *fc)
387 BT_ASSERT_DBG(!fc || (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
388 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE));
389 return (ctf_field_class_array_base *) fc;
392 static inline ctf_field_class_array *ctf_field_class_as_array(ctf_field_class *fc)
394 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ARRAY);
395 return (ctf_field_class_array *) fc;
398 static inline ctf_field_class_sequence *ctf_field_class_as_sequence(ctf_field_class *fc)
400 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE);
401 return (ctf_field_class_sequence *) fc;
404 static inline ctf_field_class_variant *ctf_field_class_as_variant(ctf_field_class *fc)
406 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_VARIANT);
407 return (ctf_field_class_variant *) fc;
410 static inline void ctf_field_class_destroy(struct ctf_field_class *fc);
412 static inline void _ctf_field_class_init(struct ctf_field_class *fc, enum ctf_field_class_type type,
413 unsigned int alignment)
417 fc->alignment = alignment;
421 static inline void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
422 enum ctf_field_class_type type)
424 _ctf_field_class_init(&fc->base, type, 1);
427 static inline void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
428 enum ctf_field_class_type type)
430 _ctf_field_class_bit_array_init(&fc->base, type);
431 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
432 fc->storing_index = -1;
435 static inline void ctf_field_path_init(struct ctf_field_path *field_path)
437 BT_ASSERT(field_path);
438 field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t));
439 BT_ASSERT(field_path->path);
442 static inline void ctf_field_path_fini(struct ctf_field_path *field_path)
444 BT_ASSERT(field_path);
446 if (field_path->path) {
447 g_array_free(field_path->path, TRUE);
451 static inline void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
454 named_fc->name = g_string_new(NULL);
455 BT_ASSERT(named_fc->name);
456 named_fc->orig_name = g_string_new(NULL);
457 BT_ASSERT(named_fc->orig_name);
460 static inline void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
464 if (named_fc->name) {
465 g_string_free(named_fc->name, TRUE);
468 if (named_fc->orig_name) {
469 g_string_free(named_fc->orig_name, TRUE);
472 ctf_field_class_destroy(named_fc->fc);
475 static inline void _ctf_field_class_enum_mapping_init(struct ctf_field_class_enum_mapping *mapping)
478 mapping->label = g_string_new(NULL);
479 BT_ASSERT(mapping->label);
480 mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range));
481 BT_ASSERT(mapping->ranges);
484 static inline void _ctf_field_class_enum_mapping_fini(struct ctf_field_class_enum_mapping *mapping)
488 if (mapping->label) {
489 g_string_free(mapping->label, TRUE);
492 if (mapping->ranges) {
493 g_array_free(mapping->ranges, TRUE);
497 static inline struct ctf_field_class_int *ctf_field_class_int_create(void)
499 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
502 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
506 static inline struct ctf_field_class_float *ctf_field_class_float_create(void)
508 struct ctf_field_class_float *fc = g_new0(struct ctf_field_class_float, 1);
511 _ctf_field_class_bit_array_init(&fc->base, CTF_FIELD_CLASS_TYPE_FLOAT);
515 static inline struct ctf_field_class_string *ctf_field_class_string_create(void)
517 struct ctf_field_class_string *fc = g_new0(struct ctf_field_class_string, 1);
520 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRING, 8);
524 static inline struct ctf_field_class_enum *ctf_field_class_enum_create(void)
526 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
529 _ctf_field_class_int_init(&fc->base, CTF_FIELD_CLASS_TYPE_ENUM);
530 fc->mappings = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_enum_mapping));
531 BT_ASSERT(fc->mappings);
535 static inline struct ctf_field_class_struct *ctf_field_class_struct_create(void)
537 struct ctf_field_class_struct *fc = g_new0(struct ctf_field_class_struct, 1);
540 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
541 fc->members = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class));
542 BT_ASSERT(fc->members);
543 fc->base.is_compound = true;
547 static inline struct ctf_field_class_variant *ctf_field_class_variant_create(void)
549 struct ctf_field_class_variant *fc = g_new0(struct ctf_field_class_variant, 1);
552 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
553 fc->options = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class));
554 BT_ASSERT(fc->options);
555 fc->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_variant_range));
556 BT_ASSERT(fc->ranges);
557 fc->tag_ref = g_string_new(NULL);
558 BT_ASSERT(fc->tag_ref);
559 ctf_field_path_init(&fc->tag_path);
560 fc->base.is_compound = true;
564 static inline struct ctf_field_class_array *ctf_field_class_array_create(void)
566 struct ctf_field_class_array *fc = g_new0(struct ctf_field_class_array, 1);
569 _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
570 fc->base.base.is_compound = true;
574 static inline struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
576 struct ctf_field_class_sequence *fc = g_new0(struct ctf_field_class_sequence, 1);
579 _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
580 fc->length_ref = g_string_new(NULL);
581 BT_ASSERT(fc->length_ref);
582 ctf_field_path_init(&fc->length_path);
583 fc->base.base.is_compound = true;
587 static inline void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
593 static inline void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
600 for (i = 0; i < fc->mappings->len; i++) {
601 struct ctf_field_class_enum_mapping *mapping =
602 &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i);
604 _ctf_field_class_enum_mapping_fini(mapping);
607 g_array_free(fc->mappings, TRUE);
613 static inline void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
619 static inline void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
625 static inline void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
632 for (i = 0; i < fc->members->len; i++) {
633 struct ctf_named_field_class *named_fc =
634 &bt_g_array_index(fc->members, struct ctf_named_field_class, i);
636 _ctf_named_field_class_fini(named_fc);
639 g_array_free(fc->members, TRUE);
645 static inline void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
648 ctf_field_class_destroy(fc->elem_fc);
651 static inline void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
654 _ctf_field_class_array_base_fini(&fc->base);
658 static inline void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
661 _ctf_field_class_array_base_fini(&fc->base);
663 if (fc->length_ref) {
664 g_string_free(fc->length_ref, TRUE);
667 ctf_field_path_fini(&fc->length_path);
671 static inline void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
678 for (i = 0; i < fc->options->len; i++) {
679 struct ctf_named_field_class *named_fc =
680 &bt_g_array_index(fc->options, struct ctf_named_field_class, i);
682 _ctf_named_field_class_fini(named_fc);
685 g_array_free(fc->options, TRUE);
689 g_array_free(fc->ranges, TRUE);
693 g_string_free(fc->tag_ref, TRUE);
696 ctf_field_path_fini(&fc->tag_path);
700 static inline void ctf_field_class_destroy(struct ctf_field_class *fc)
707 case CTF_FIELD_CLASS_TYPE_INT:
708 _ctf_field_class_int_destroy(ctf_field_class_as_int(fc));
710 case CTF_FIELD_CLASS_TYPE_ENUM:
711 _ctf_field_class_enum_destroy(ctf_field_class_as_enum(fc));
713 case CTF_FIELD_CLASS_TYPE_FLOAT:
714 _ctf_field_class_float_destroy(ctf_field_class_as_float(fc));
716 case CTF_FIELD_CLASS_TYPE_STRING:
717 _ctf_field_class_string_destroy(ctf_field_class_as_string(fc));
719 case CTF_FIELD_CLASS_TYPE_STRUCT:
720 _ctf_field_class_struct_destroy(ctf_field_class_as_struct(fc));
722 case CTF_FIELD_CLASS_TYPE_ARRAY:
723 _ctf_field_class_array_destroy(ctf_field_class_as_array(fc));
725 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
726 _ctf_field_class_sequence_destroy(ctf_field_class_as_sequence(fc));
728 case CTF_FIELD_CLASS_TYPE_VARIANT:
729 _ctf_field_class_variant_destroy(ctf_field_class_as_variant(fc));
736 static inline struct ctf_range *
737 ctf_field_class_enum_mapping_borrow_range_by_index(struct ctf_field_class_enum_mapping *mapping,
740 BT_ASSERT_DBG(mapping);
741 BT_ASSERT_DBG(index < mapping->ranges->len);
742 return &bt_g_array_index(mapping->ranges, struct ctf_range, index);
745 static inline struct ctf_field_class_enum_mapping *
746 ctf_field_class_enum_borrow_mapping_by_index(struct ctf_field_class_enum *fc, uint64_t index)
749 BT_ASSERT_DBG(index < fc->mappings->len);
750 return &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, index);
753 static inline struct ctf_field_class_enum_mapping *
754 ctf_field_class_enum_borrow_mapping_by_label(struct ctf_field_class_enum *fc, const char *label)
756 struct ctf_field_class_enum_mapping *ret_mapping = NULL;
760 BT_ASSERT_DBG(label);
762 for (i = 0; i < fc->mappings->len; i++) {
763 struct ctf_field_class_enum_mapping *mapping =
764 ctf_field_class_enum_borrow_mapping_by_index(fc, i);
766 if (strcmp(mapping->label->str, label) == 0) {
767 ret_mapping = mapping;
776 static inline void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc,
777 const char *label, uint64_t u_lower,
780 struct ctf_field_class_enum_mapping *mapping = NULL;
781 struct ctf_range range = {
796 for (i = 0; i < fc->mappings->len; i++) {
797 mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, i);
799 if (strcmp(mapping->label->str, label) == 0) {
804 if (i == fc->mappings->len) {
809 g_array_set_size(fc->mappings, fc->mappings->len + 1);
810 mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, fc->mappings->len - 1);
811 _ctf_field_class_enum_mapping_init(mapping);
812 g_string_assign(mapping->label, label);
815 g_array_append_val(mapping->ranges, range);
818 static inline struct ctf_named_field_class *
819 ctf_field_class_struct_borrow_member_by_index(struct ctf_field_class_struct *fc, uint64_t index)
822 BT_ASSERT_DBG(index < fc->members->len);
823 return &bt_g_array_index(fc->members, struct ctf_named_field_class, index);
826 static inline struct ctf_named_field_class *
827 ctf_field_class_struct_borrow_member_by_name(struct ctf_field_class_struct *fc, const char *name)
830 struct ctf_named_field_class *ret_named_fc = NULL;
835 for (i = 0; i < fc->members->len; i++) {
836 struct ctf_named_field_class *named_fc =
837 ctf_field_class_struct_borrow_member_by_index(fc, i);
839 if (strcmp(name, named_fc->name->str) == 0) {
840 ret_named_fc = named_fc;
849 static inline struct ctf_field_class *
850 ctf_field_class_struct_borrow_member_field_class_by_name(struct ctf_field_class_struct *struct_fc,
853 struct ctf_named_field_class *named_fc = NULL;
854 struct ctf_field_class *fc = NULL;
860 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
871 static inline struct ctf_field_class_int *
872 ctf_field_class_struct_borrow_member_int_field_class_by_name(
873 struct ctf_field_class_struct *struct_fc, const char *name)
875 ctf_field_class *member_fc =
876 ctf_field_class_struct_borrow_member_field_class_by_name(struct_fc, name);
882 if (member_fc->type != CTF_FIELD_CLASS_TYPE_INT &&
883 member_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
887 return ctf_field_class_as_int(member_fc);
890 static inline void _ctf_named_field_class_unescape_orig_name(struct ctf_named_field_class *named_fc)
892 const char *name = named_fc->orig_name->str;
894 if (name[0] == '_') {
898 g_string_assign(named_fc->name, name);
901 static inline void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
902 const char *orig_name,
903 struct ctf_field_class *member_fc)
905 struct ctf_named_field_class *named_fc;
908 BT_ASSERT(orig_name);
909 g_array_set_size(fc->members, fc->members->len + 1);
911 named_fc = &bt_g_array_index(fc->members, struct ctf_named_field_class, fc->members->len - 1);
912 _ctf_named_field_class_init(named_fc);
913 g_string_assign(named_fc->orig_name, orig_name);
914 _ctf_named_field_class_unescape_orig_name(named_fc);
915 named_fc->fc = member_fc;
917 if (member_fc->alignment > fc->base.alignment) {
918 fc->base.alignment = member_fc->alignment;
922 static inline struct ctf_named_field_class *
923 ctf_field_class_variant_borrow_option_by_index(struct ctf_field_class_variant *fc, uint64_t index)
926 BT_ASSERT_DBG(index < fc->options->len);
927 return &bt_g_array_index(fc->options, struct ctf_named_field_class, index);
930 static inline struct ctf_named_field_class *
931 ctf_field_class_variant_borrow_option_by_name(struct ctf_field_class_variant *fc, const char *name)
934 struct ctf_named_field_class *ret_named_fc = NULL;
939 for (i = 0; i < fc->options->len; i++) {
940 struct ctf_named_field_class *named_fc =
941 ctf_field_class_variant_borrow_option_by_index(fc, i);
943 if (strcmp(name, named_fc->name->str) == 0) {
944 ret_named_fc = named_fc;
953 static inline struct ctf_field_class_variant_range *
954 ctf_field_class_variant_borrow_range_by_index(struct ctf_field_class_variant *fc, uint64_t index)
957 BT_ASSERT_DBG(index < fc->ranges->len);
958 return &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, index);
961 static inline void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
962 const char *orig_name,
963 struct ctf_field_class *option_fc)
965 struct ctf_named_field_class *named_fc;
968 BT_ASSERT(orig_name);
969 g_array_set_size(fc->options, fc->options->len + 1);
971 named_fc = &bt_g_array_index(fc->options, struct ctf_named_field_class, fc->options->len - 1);
972 _ctf_named_field_class_init(named_fc);
973 g_string_assign(named_fc->orig_name, orig_name);
974 _ctf_named_field_class_unescape_orig_name(named_fc);
975 named_fc->fc = option_fc;
978 static inline void ctf_field_class_variant_set_tag_field_class(struct ctf_field_class_variant *fc,
979 struct ctf_field_class_enum *tag_fc)
987 for (option_i = 0; option_i < fc->options->len; option_i++) {
989 struct ctf_named_field_class *named_fc =
990 ctf_field_class_variant_borrow_option_by_index(fc, option_i);
991 struct ctf_field_class_enum_mapping *mapping;
993 mapping = ctf_field_class_enum_borrow_mapping_by_label(tag_fc, named_fc->orig_name->str);
998 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
999 struct ctf_range *range =
1000 ctf_field_class_enum_mapping_borrow_range_by_index(mapping, range_i);
1001 struct ctf_field_class_variant_range var_range;
1003 var_range.range = *range;
1004 var_range.option_index = option_i;
1005 g_array_append_val(fc->ranges, var_range);
1010 static inline struct ctf_field_class *
1011 ctf_field_class_compound_borrow_field_class_by_index(struct ctf_field_class *comp_fc,
1014 struct ctf_field_class *fc = NULL;
1016 switch (comp_fc->type) {
1017 case CTF_FIELD_CLASS_TYPE_STRUCT:
1019 struct ctf_named_field_class *named_fc = ctf_field_class_struct_borrow_member_by_index(
1020 (struct ctf_field_class_struct *) comp_fc, index);
1022 BT_ASSERT_DBG(named_fc);
1026 case CTF_FIELD_CLASS_TYPE_VARIANT:
1028 struct ctf_named_field_class *named_fc = ctf_field_class_variant_borrow_option_by_index(
1029 (struct ctf_field_class_variant *) comp_fc, index);
1031 BT_ASSERT_DBG(named_fc);
1035 case CTF_FIELD_CLASS_TYPE_ARRAY:
1036 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1038 struct ctf_field_class_array_base *array_fc = (struct ctf_field_class_array_base *) comp_fc;
1040 fc = array_fc->elem_fc;
1050 static inline uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
1052 uint64_t field_count;
1055 case CTF_FIELD_CLASS_TYPE_STRUCT:
1057 struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc;
1059 field_count = struct_fc->members->len;
1062 case CTF_FIELD_CLASS_TYPE_VARIANT:
1064 struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc;
1066 field_count = var_fc->options->len;
1069 case CTF_FIELD_CLASS_TYPE_ARRAY:
1070 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1072 * Array and sequence types always contain a single
1073 * member (the element type).
1084 static inline int64_t
1085 ctf_field_class_compound_get_field_class_index_from_orig_name(struct ctf_field_class *fc,
1086 const char *orig_name)
1088 int64_t ret_index = -1;
1092 case CTF_FIELD_CLASS_TYPE_STRUCT:
1094 struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc;
1096 for (i = 0; i < struct_fc->members->len; i++) {
1097 struct ctf_named_field_class *named_fc =
1098 ctf_field_class_struct_borrow_member_by_index(struct_fc, i);
1100 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1101 ret_index = (int64_t) i;
1108 case CTF_FIELD_CLASS_TYPE_VARIANT:
1110 struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc;
1112 for (i = 0; i < var_fc->options->len; i++) {
1113 struct ctf_named_field_class *named_fc =
1114 ctf_field_class_variant_borrow_option_by_index(var_fc, i);
1116 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1117 ret_index = (int64_t) i;
1132 static inline void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index)
1135 g_array_append_val(fp->path, index);
1138 static inline int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path *fp,
1142 BT_ASSERT_DBG(index < fp->path->len);
1143 return bt_g_array_index(fp->path, int64_t, index);
1146 static inline void ctf_field_path_clear(struct ctf_field_path *fp)
1149 g_array_set_size(fp->path, 0);
1152 static inline const char *ctf_scope_string(enum ctf_scope scope)
1155 case CTF_SCOPE_PACKET_HEADER:
1156 return "PACKET_HEADER";
1157 case CTF_SCOPE_PACKET_CONTEXT:
1158 return "PACKET_CONTEXT";
1159 case CTF_SCOPE_EVENT_HEADER:
1160 return "EVENT_HEADER";
1161 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1162 return "EVENT_COMMON_CONTEXT";
1163 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1164 return "EVENT_SPECIFIC_CONTEXT";
1165 case CTF_SCOPE_EVENT_PAYLOAD:
1166 return "EVENT_PAYLOAD";
1172 static inline GString *ctf_field_path_string(struct ctf_field_path *path)
1174 GString *str = g_string_new(NULL);
1183 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
1185 for (i = 0; i < path->path->len; i++) {
1186 g_string_append_printf(str, ", %" PRId64, ctf_field_path_borrow_index_by_index(path, i));
1189 g_string_append(str, "]");
1195 static inline struct ctf_field_class *
1196 ctf_field_path_borrow_field_class(struct ctf_field_path *field_path, struct ctf_trace_class *tc,
1197 struct ctf_stream_class *sc, struct ctf_event_class *ec)
1200 struct ctf_field_class *fc;
1202 switch (field_path->root) {
1203 case CTF_SCOPE_PACKET_HEADER:
1204 fc = tc->packet_header_fc;
1206 case CTF_SCOPE_PACKET_CONTEXT:
1207 fc = sc->packet_context_fc;
1209 case CTF_SCOPE_EVENT_HEADER:
1210 fc = sc->event_header_fc;
1212 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1213 fc = sc->event_common_context_fc;
1215 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1216 fc = ec->spec_context_fc;
1218 case CTF_SCOPE_EVENT_PAYLOAD:
1219 fc = ec->payload_fc;
1227 for (i = 0; i < field_path->path->len; i++) {
1228 int64_t child_index = ctf_field_path_borrow_index_by_index(field_path, i);
1229 struct ctf_field_class *child_fc =
1230 ctf_field_class_compound_borrow_field_class_by_index(fc, child_index);
1231 BT_ASSERT_DBG(child_fc);
1239 static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
1241 static inline void ctf_field_class_bit_array_copy_content(struct ctf_field_class_bit_array *dst_fc,
1242 struct ctf_field_class_bit_array *src_fc)
1246 dst_fc->byte_order = src_fc->byte_order;
1247 dst_fc->size = src_fc->size;
1250 static inline void ctf_field_class_int_copy_content(struct ctf_field_class_int *dst_fc,
1251 struct ctf_field_class_int *src_fc)
1253 ctf_field_class_bit_array_copy_content(&dst_fc->base, &src_fc->base);
1254 dst_fc->meaning = src_fc->meaning;
1255 dst_fc->is_signed = src_fc->is_signed;
1256 dst_fc->disp_base = src_fc->disp_base;
1257 dst_fc->encoding = src_fc->encoding;
1258 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
1259 dst_fc->storing_index = src_fc->storing_index;
1262 static inline struct ctf_field_class_int *_ctf_field_class_int_copy(struct ctf_field_class_int *fc)
1264 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
1267 ctf_field_class_int_copy_content(copy_fc, fc);
1271 static inline struct ctf_field_class_enum *
1272 _ctf_field_class_enum_copy(struct ctf_field_class_enum *fc)
1274 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
1278 ctf_field_class_int_copy_content(©_fc->base, &fc->base);
1280 for (i = 0; i < fc->mappings->len; i++) {
1283 struct ctf_field_class_enum_mapping *mapping =
1284 &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i);
1286 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1287 struct ctf_range *range = &bt_g_array_index(mapping->ranges, struct ctf_range, range_i);
1289 ctf_field_class_enum_map_range(copy_fc, mapping->label->str, range->lower.u,
1297 static inline struct ctf_field_class_float *
1298 _ctf_field_class_float_copy(struct ctf_field_class_float *fc)
1300 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
1303 ctf_field_class_bit_array_copy_content(©_fc->base, &fc->base);
1307 static inline struct ctf_field_class_string *
1308 _ctf_field_class_string_copy(struct ctf_field_class_string *)
1310 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
1316 static inline struct ctf_field_class_struct *
1317 _ctf_field_class_struct_copy(struct ctf_field_class_struct *fc)
1319 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
1324 for (i = 0; i < fc->members->len; i++) {
1325 struct ctf_named_field_class *named_fc =
1326 &bt_g_array_index(fc->members, struct ctf_named_field_class, i);
1328 ctf_field_class_struct_append_member(copy_fc, named_fc->name->str,
1329 ctf_field_class_copy(named_fc->fc));
1335 static inline void ctf_field_path_copy_content(struct ctf_field_path *dst_fp,
1336 struct ctf_field_path *src_fp)
1342 dst_fp->root = src_fp->root;
1343 ctf_field_path_clear(dst_fp);
1345 for (i = 0; i < src_fp->path->len; i++) {
1346 int64_t index = ctf_field_path_borrow_index_by_index(src_fp, i);
1348 ctf_field_path_append_index(dst_fp, index);
1352 static inline struct ctf_field_class_variant *
1353 _ctf_field_class_variant_copy(struct ctf_field_class_variant *fc)
1355 struct ctf_field_class_variant *copy_fc = ctf_field_class_variant_create();
1360 for (i = 0; i < fc->options->len; i++) {
1361 struct ctf_named_field_class *named_fc =
1362 &bt_g_array_index(fc->options, struct ctf_named_field_class, i);
1364 ctf_field_class_variant_append_option(copy_fc, named_fc->name->str,
1365 ctf_field_class_copy(named_fc->fc));
1368 for (i = 0; i < fc->ranges->len; i++) {
1369 struct ctf_field_class_variant_range *range =
1370 &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, i);
1372 g_array_append_val(copy_fc->ranges, *range);
1375 ctf_field_path_copy_content(©_fc->tag_path, &fc->tag_path);
1376 g_string_assign(copy_fc->tag_ref, fc->tag_ref->str);
1377 copy_fc->stored_tag_index = fc->stored_tag_index;
1382 ctf_field_class_array_base_copy_content(struct ctf_field_class_array_base *dst_fc,
1383 struct ctf_field_class_array_base *src_fc)
1387 dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc);
1388 dst_fc->is_text = src_fc->is_text;
1391 static inline struct ctf_field_class_array *
1392 _ctf_field_class_array_copy(struct ctf_field_class_array *fc)
1394 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
1397 ctf_field_class_array_base_copy_content(©_fc->base, &fc->base);
1398 copy_fc->length = fc->length;
1402 static inline struct ctf_field_class_sequence *
1403 _ctf_field_class_sequence_copy(struct ctf_field_class_sequence *fc)
1405 struct ctf_field_class_sequence *copy_fc = ctf_field_class_sequence_create();
1408 ctf_field_class_array_base_copy_content(©_fc->base, &fc->base);
1409 ctf_field_path_copy_content(©_fc->length_path, &fc->length_path);
1410 g_string_assign(copy_fc->length_ref, fc->length_ref->str);
1411 copy_fc->stored_length_index = fc->stored_length_index;
1415 static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
1417 struct ctf_field_class *copy_fc = NULL;
1424 * Translation should not have happened yet.
1426 BT_ASSERT(!fc->ir_fc);
1429 case CTF_FIELD_CLASS_TYPE_INT:
1430 copy_fc = &_ctf_field_class_int_copy(ctf_field_class_as_int(fc))->base.base;
1432 case CTF_FIELD_CLASS_TYPE_ENUM:
1433 copy_fc = &_ctf_field_class_enum_copy(ctf_field_class_as_enum(fc))->base.base.base;
1435 case CTF_FIELD_CLASS_TYPE_FLOAT:
1436 copy_fc = &_ctf_field_class_float_copy(ctf_field_class_as_float(fc))->base.base;
1438 case CTF_FIELD_CLASS_TYPE_STRING:
1439 copy_fc = &_ctf_field_class_string_copy(ctf_field_class_as_string(fc))->base;
1441 case CTF_FIELD_CLASS_TYPE_STRUCT:
1442 copy_fc = &_ctf_field_class_struct_copy(ctf_field_class_as_struct(fc))->base;
1444 case CTF_FIELD_CLASS_TYPE_ARRAY:
1445 copy_fc = &_ctf_field_class_array_copy(ctf_field_class_as_array(fc))->base.base;
1447 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1448 copy_fc = &_ctf_field_class_sequence_copy(ctf_field_class_as_sequence(fc))->base.base;
1450 case CTF_FIELD_CLASS_TYPE_VARIANT:
1451 copy_fc = &_ctf_field_class_variant_copy(ctf_field_class_as_variant(fc))->base;
1457 copy_fc->type = fc->type;
1458 copy_fc->alignment = fc->alignment;
1459 copy_fc->in_ir = fc->in_ir;
1465 static inline struct ctf_event_class *ctf_event_class_create(void)
1467 struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1);
1470 ec->name = g_string_new(NULL);
1471 BT_ASSERT(ec->name);
1472 ec->emf_uri = g_string_new(NULL);
1473 BT_ASSERT(ec->emf_uri);
1474 ec->is_log_level_set = false;
1478 static inline void ctf_event_class_set_log_level(struct ctf_event_class *ec,
1479 enum bt_event_class_log_level log_level)
1482 ec->log_level = log_level;
1483 ec->is_log_level_set = true;
1486 static inline void ctf_event_class_destroy(struct ctf_event_class *ec)
1493 g_string_free(ec->name, TRUE);
1497 g_string_free(ec->emf_uri, TRUE);
1500 ctf_field_class_destroy(ec->spec_context_fc);
1501 ctf_field_class_destroy(ec->payload_fc);
1505 static inline struct ctf_stream_class *ctf_stream_class_create(void)
1507 struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1);
1510 sc->event_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_event_class_destroy);
1511 BT_ASSERT(sc->event_classes);
1512 sc->event_classes_by_id = g_hash_table_new(g_direct_hash, g_direct_equal);
1513 BT_ASSERT(sc->event_classes_by_id);
1517 static inline void ctf_stream_class_destroy(struct ctf_stream_class *sc)
1523 if (sc->event_classes) {
1524 g_ptr_array_free(sc->event_classes, TRUE);
1527 if (sc->event_classes_by_id) {
1528 g_hash_table_destroy(sc->event_classes_by_id);
1531 ctf_field_class_destroy(sc->packet_context_fc);
1532 ctf_field_class_destroy(sc->event_header_fc);
1533 ctf_field_class_destroy(sc->event_common_context_fc);
1537 static inline void ctf_stream_class_append_event_class(struct ctf_stream_class *sc,
1538 struct ctf_event_class *ec)
1540 g_ptr_array_add(sc->event_classes, ec);
1541 g_hash_table_insert(sc->event_classes_by_id, GUINT_TO_POINTER((guint) ec->id), ec);
1544 static inline struct ctf_event_class *
1545 ctf_stream_class_borrow_event_class_by_id(struct ctf_stream_class *sc, uint64_t type)
1548 return (struct ctf_event_class *) g_hash_table_lookup(sc->event_classes_by_id,
1549 GUINT_TO_POINTER((guint) type));
1552 static inline void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry)
1555 entry->name = g_string_new(NULL);
1556 BT_ASSERT(entry->name);
1557 entry->value.str = g_string_new(NULL);
1558 BT_ASSERT(entry->value.str);
1561 static inline void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry)
1566 g_string_free(entry->name, TRUE);
1569 if (entry->value.str) {
1570 g_string_free(entry->value.str, TRUE);
1574 static inline struct ctf_clock_class *ctf_clock_class_create(void)
1576 struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1);
1579 cc->name = g_string_new(NULL);
1580 BT_ASSERT(cc->name);
1581 cc->description = g_string_new(NULL);
1582 BT_ASSERT(cc->description);
1586 static inline void ctf_clock_class_destroy(struct ctf_clock_class *cc)
1593 g_string_free(cc->name, TRUE);
1596 if (cc->description) {
1597 g_string_free(cc->description, TRUE);
1600 bt_clock_class_put_ref(cc->ir_cc);
1604 static inline struct ctf_trace_class *ctf_trace_class_create(void)
1606 struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1);
1609 tc->default_byte_order = CTF_BYTE_ORDER_UNKNOWN;
1610 tc->clock_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_clock_class_destroy);
1611 BT_ASSERT(tc->clock_classes);
1612 tc->stream_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_stream_class_destroy);
1613 BT_ASSERT(tc->stream_classes);
1614 tc->env_entries = g_array_new(FALSE, TRUE, sizeof(struct ctf_trace_class_env_entry));
1618 static inline void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1624 ctf_field_class_destroy(tc->packet_header_fc);
1626 if (tc->clock_classes) {
1627 g_ptr_array_free(tc->clock_classes, TRUE);
1630 if (tc->stream_classes) {
1631 g_ptr_array_free(tc->stream_classes, TRUE);
1634 if (tc->env_entries) {
1637 for (i = 0; i < tc->env_entries->len; i++) {
1638 struct ctf_trace_class_env_entry *entry =
1639 &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, i);
1641 _ctf_trace_class_env_entry_fini(entry);
1644 g_array_free(tc->env_entries, TRUE);
1650 static inline void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc, const char *name,
1651 enum ctf_trace_class_env_entry_type type,
1652 const char *str_value, int64_t i_value)
1654 struct ctf_trace_class_env_entry *entry;
1658 g_array_set_size(tc->env_entries, tc->env_entries->len + 1);
1660 entry = &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry,
1661 tc->env_entries->len - 1);
1663 _ctf_trace_class_env_entry_init(entry);
1664 g_string_assign(entry->name, name);
1667 g_string_assign(entry->value.str, str_value);
1670 entry->value.i = i_value;
1673 static inline struct ctf_stream_class *
1674 ctf_trace_class_borrow_stream_class_by_id(struct ctf_trace_class *tc, uint64_t id)
1677 struct ctf_stream_class *ret_sc = NULL;
1681 for (i = 0; i < tc->stream_classes->len; i++) {
1682 struct ctf_stream_class *sc = (struct ctf_stream_class *) tc->stream_classes->pdata[i];
1694 static inline struct ctf_clock_class *
1695 ctf_trace_class_borrow_clock_class_by_name(struct ctf_trace_class *tc, const char *name)
1698 struct ctf_clock_class *ret_cc = NULL;
1701 BT_ASSERT_DBG(name);
1703 for (i = 0; i < tc->clock_classes->len; i++) {
1704 struct ctf_clock_class *cc = (struct ctf_clock_class *) tc->clock_classes->pdata[i];
1706 BT_ASSERT_DBG(cc->name);
1707 if (strcmp(cc->name->str, name) == 0) {
1717 static inline struct ctf_trace_class_env_entry *
1718 ctf_trace_class_borrow_env_entry_by_index(struct ctf_trace_class *tc, uint64_t index)
1721 BT_ASSERT_DBG(index < tc->env_entries->len);
1722 return &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, index);
1725 static inline struct ctf_trace_class_env_entry *
1726 ctf_trace_class_borrow_env_entry_by_name(struct ctf_trace_class *tc, const char *name)
1728 struct ctf_trace_class_env_entry *ret_entry = NULL;
1732 BT_ASSERT_DBG(name);
1734 for (i = 0; i < tc->env_entries->len; i++) {
1735 struct ctf_trace_class_env_entry *env_entry =
1736 ctf_trace_class_borrow_env_entry_by_index(tc, i);
1738 if (strcmp(env_entry->name->str, name) == 0) {
1739 ret_entry = env_entry;
1748 #endif /* _CTF_META_H */