2 * SPDX-License-Identifier: MIT
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
15 #include <babeltrace2/babeltrace.h>
17 #include "common/assert.h"
18 #include "common/common.h"
19 #include "common/uuid.h"
21 enum ctf_field_class_type
23 CTF_FIELD_CLASS_TYPE_INT,
24 CTF_FIELD_CLASS_TYPE_ENUM,
25 CTF_FIELD_CLASS_TYPE_FLOAT,
26 CTF_FIELD_CLASS_TYPE_STRING,
27 CTF_FIELD_CLASS_TYPE_STRUCT,
28 CTF_FIELD_CLASS_TYPE_ARRAY,
29 CTF_FIELD_CLASS_TYPE_SEQUENCE,
30 CTF_FIELD_CLASS_TYPE_VARIANT,
33 enum ctf_field_class_meaning
35 CTF_FIELD_CLASS_MEANING_NONE,
36 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME,
37 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME,
38 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID,
39 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID,
40 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID,
41 CTF_FIELD_CLASS_MEANING_MAGIC,
42 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT,
43 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT,
44 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE,
45 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE,
46 CTF_FIELD_CLASS_MEANING_UUID,
51 CTF_BYTE_ORDER_UNKNOWN,
52 CTF_BYTE_ORDER_DEFAULT,
53 CTF_BYTE_ORDER_LITTLE,
65 CTF_SCOPE_PACKET_UNKNOWN = -1,
66 CTF_SCOPE_PACKET_HEADER = 0,
67 CTF_SCOPE_PACKET_CONTEXT,
68 CTF_SCOPE_EVENT_HEADER,
69 CTF_SCOPE_EVENT_COMMON_CONTEXT,
70 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT,
71 CTF_SCOPE_EVENT_PAYLOAD,
74 struct ctf_clock_class
80 int64_t offset_seconds;
81 uint64_t offset_cycles;
86 /* Weak, set during translation */
87 bt_clock_class *ir_cc;
90 struct ctf_field_class
92 enum ctf_field_class_type type;
93 unsigned int alignment;
97 /* Weak, set during translation. NULL if `in_ir` is false below. */
98 bt_field_class *ir_fc;
101 struct ctf_field_class_bit_array
103 struct ctf_field_class base;
104 enum ctf_byte_order byte_order;
108 struct ctf_field_class_int
110 struct ctf_field_class_bit_array base;
111 enum ctf_field_class_meaning meaning;
113 bt_field_class_integer_preferred_display_base disp_base;
114 enum ctf_encoding encoding;
115 int64_t storing_index;
118 struct ctf_clock_class *mapped_clock_class;
136 struct ctf_field_class_enum_mapping
140 /* Array of `struct ctf_range` */
144 struct ctf_field_class_enum
146 struct ctf_field_class_int base;
148 /* Array of `struct ctf_field_class_enum_mapping` */
152 struct ctf_field_class_float
154 struct ctf_field_class_bit_array base;
157 struct ctf_field_class_string
159 struct ctf_field_class base;
160 enum ctf_encoding encoding;
163 struct ctf_named_field_class
165 /* Original name which can include a leading `_` */
168 /* Name as translated to trace IR (leading `_` removed) */
172 struct ctf_field_class *fc;
175 struct ctf_field_class_struct
177 struct ctf_field_class base;
179 /* Array of `struct ctf_named_field_class` */
183 struct ctf_field_path
187 /* Array of `int64_t` */
191 struct ctf_field_class_variant_range
193 struct ctf_range range;
194 uint64_t option_index;
197 struct ctf_field_class_variant
199 struct ctf_field_class base;
201 struct ctf_field_path tag_path;
202 uint64_t stored_tag_index;
204 /* Array of `struct ctf_named_field_class` */
207 /* Array of `struct ctf_field_class_variant_range` */
211 struct ctf_field_class_enum *tag_fc;
214 struct ctf_field_class_array_base
216 struct ctf_field_class base;
217 struct ctf_field_class *elem_fc;
221 struct ctf_field_class_array
223 struct ctf_field_class_array_base base;
224 enum ctf_field_class_meaning meaning;
228 struct ctf_field_class_sequence
230 struct ctf_field_class_array_base base;
232 struct ctf_field_path length_path;
233 uint64_t stored_length_index;
236 struct ctf_field_class_int *length_fc;
239 struct ctf_event_class
244 bt_event_class_log_level log_level;
246 bool is_log_level_set;
249 struct ctf_field_class *spec_context_fc;
252 struct ctf_field_class *payload_fc;
254 /* Weak, set during translation */
255 bt_event_class *ir_ec;
258 struct ctf_stream_class
262 bool packets_have_ts_begin;
263 bool packets_have_ts_end;
264 bool has_discarded_events;
265 bool has_discarded_packets;
266 bool discarded_events_have_default_cs;
267 bool discarded_packets_have_default_cs;
270 struct ctf_field_class *packet_context_fc;
273 struct ctf_field_class *event_header_fc;
276 struct ctf_field_class *event_common_context_fc;
278 /* Array of `struct ctf_event_class *`, owned by this */
279 GPtrArray *event_classes;
282 * Hash table mapping event class IDs to `struct ctf_event_class *`,
285 GHashTable *event_classes_by_id;
288 struct ctf_clock_class *default_clock_class;
290 /* Weak, set during translation */
291 bt_stream_class *ir_sc;
294 enum ctf_trace_class_env_entry_type
296 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
297 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
300 struct ctf_trace_class_env_entry
302 enum ctf_trace_class_env_entry_type type;
312 struct ctf_trace_class
318 enum ctf_byte_order default_byte_order;
321 struct ctf_field_class *packet_header_fc;
323 uint64_t stored_value_count;
325 /* Array of `struct ctf_clock_class *` (owned by this) */
326 GPtrArray *clock_classes;
328 /* Array of `struct ctf_stream_class *` */
329 GPtrArray *stream_classes;
331 /* Array of `struct ctf_trace_class_env_entry` */
336 /* Weak, set during translation */
337 bt_trace_class *ir_tc;
342 bool lttng_event_after_packet;
343 bool barectf_event_before_packet;
347 static inline ctf_field_class_bit_array *ctf_field_class_as_bit_array(ctf_field_class *fc)
350 (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM ||
351 fc->type == CTF_FIELD_CLASS_TYPE_FLOAT));
352 return (ctf_field_class_bit_array *) fc;
355 static inline ctf_field_class_int *ctf_field_class_as_int(ctf_field_class *fc)
358 (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM));
359 return (ctf_field_class_int *) fc;
362 static inline ctf_field_class_enum *ctf_field_class_as_enum(ctf_field_class *fc)
364 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ENUM);
365 return (ctf_field_class_enum *) fc;
368 static inline ctf_field_class_float *ctf_field_class_as_float(ctf_field_class *fc)
370 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_FLOAT);
371 return (ctf_field_class_float *) fc;
374 static inline ctf_field_class_string *ctf_field_class_as_string(ctf_field_class *fc)
376 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRING);
377 return (ctf_field_class_string *) fc;
380 static inline ctf_field_class_struct *ctf_field_class_as_struct(ctf_field_class *fc)
382 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRUCT);
383 return (ctf_field_class_struct *) fc;
386 static inline ctf_field_class_array_base *ctf_field_class_as_array_base(ctf_field_class *fc)
388 BT_ASSERT_DBG(!fc || (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
389 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE));
390 return (ctf_field_class_array_base *) fc;
393 static inline ctf_field_class_array *ctf_field_class_as_array(ctf_field_class *fc)
395 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ARRAY);
396 return (ctf_field_class_array *) fc;
399 static inline ctf_field_class_sequence *ctf_field_class_as_sequence(ctf_field_class *fc)
401 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE);
402 return (ctf_field_class_sequence *) fc;
405 static inline ctf_field_class_variant *ctf_field_class_as_variant(ctf_field_class *fc)
407 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_VARIANT);
408 return (ctf_field_class_variant *) fc;
411 static inline void ctf_field_class_destroy(struct ctf_field_class *fc);
413 static inline void _ctf_field_class_init(struct ctf_field_class *fc, enum ctf_field_class_type type,
414 unsigned int alignment)
418 fc->alignment = alignment;
422 static inline void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
423 enum ctf_field_class_type type)
425 _ctf_field_class_init(&fc->base, type, 1);
428 static inline void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
429 enum ctf_field_class_type type)
431 _ctf_field_class_bit_array_init(&fc->base, type);
432 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
433 fc->storing_index = -1;
436 static inline void ctf_field_path_init(struct ctf_field_path *field_path)
438 BT_ASSERT(field_path);
439 field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t));
440 BT_ASSERT(field_path->path);
443 static inline void ctf_field_path_fini(struct ctf_field_path *field_path)
445 BT_ASSERT(field_path);
447 if (field_path->path) {
448 g_array_free(field_path->path, TRUE);
452 static inline void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
455 named_fc->name = g_string_new(NULL);
456 BT_ASSERT(named_fc->name);
457 named_fc->orig_name = g_string_new(NULL);
458 BT_ASSERT(named_fc->orig_name);
461 static inline void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
465 if (named_fc->name) {
466 g_string_free(named_fc->name, TRUE);
469 if (named_fc->orig_name) {
470 g_string_free(named_fc->orig_name, TRUE);
473 ctf_field_class_destroy(named_fc->fc);
476 static inline void _ctf_field_class_enum_mapping_init(struct ctf_field_class_enum_mapping *mapping)
479 mapping->label = g_string_new(NULL);
480 BT_ASSERT(mapping->label);
481 mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range));
482 BT_ASSERT(mapping->ranges);
485 static inline void _ctf_field_class_enum_mapping_fini(struct ctf_field_class_enum_mapping *mapping)
489 if (mapping->label) {
490 g_string_free(mapping->label, TRUE);
493 if (mapping->ranges) {
494 g_array_free(mapping->ranges, TRUE);
498 static inline struct ctf_field_class_int *ctf_field_class_int_create(void)
500 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
503 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
507 static inline struct ctf_field_class_float *ctf_field_class_float_create(void)
509 struct ctf_field_class_float *fc = g_new0(struct ctf_field_class_float, 1);
512 _ctf_field_class_bit_array_init(&fc->base, CTF_FIELD_CLASS_TYPE_FLOAT);
516 static inline struct ctf_field_class_string *ctf_field_class_string_create(void)
518 struct ctf_field_class_string *fc = g_new0(struct ctf_field_class_string, 1);
521 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRING, 8);
525 static inline struct ctf_field_class_enum *ctf_field_class_enum_create(void)
527 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
530 _ctf_field_class_int_init(&fc->base, CTF_FIELD_CLASS_TYPE_ENUM);
531 fc->mappings = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_enum_mapping));
532 BT_ASSERT(fc->mappings);
536 static inline struct ctf_field_class_struct *ctf_field_class_struct_create(void)
538 struct ctf_field_class_struct *fc = g_new0(struct ctf_field_class_struct, 1);
541 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
542 fc->members = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class));
543 BT_ASSERT(fc->members);
544 fc->base.is_compound = true;
548 static inline struct ctf_field_class_variant *ctf_field_class_variant_create(void)
550 struct ctf_field_class_variant *fc = g_new0(struct ctf_field_class_variant, 1);
553 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
554 fc->options = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class));
555 BT_ASSERT(fc->options);
556 fc->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_variant_range));
557 BT_ASSERT(fc->ranges);
558 fc->tag_ref = g_string_new(NULL);
559 BT_ASSERT(fc->tag_ref);
560 ctf_field_path_init(&fc->tag_path);
561 fc->base.is_compound = true;
565 static inline struct ctf_field_class_array *ctf_field_class_array_create(void)
567 struct ctf_field_class_array *fc = g_new0(struct ctf_field_class_array, 1);
570 _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
571 fc->base.base.is_compound = true;
575 static inline struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
577 struct ctf_field_class_sequence *fc = g_new0(struct ctf_field_class_sequence, 1);
580 _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
581 fc->length_ref = g_string_new(NULL);
582 BT_ASSERT(fc->length_ref);
583 ctf_field_path_init(&fc->length_path);
584 fc->base.base.is_compound = true;
588 static inline void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
594 static inline void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
601 for (i = 0; i < fc->mappings->len; i++) {
602 struct ctf_field_class_enum_mapping *mapping =
603 &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i);
605 _ctf_field_class_enum_mapping_fini(mapping);
608 g_array_free(fc->mappings, TRUE);
614 static inline void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
620 static inline void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
626 static inline void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
633 for (i = 0; i < fc->members->len; i++) {
634 struct ctf_named_field_class *named_fc =
635 &bt_g_array_index(fc->members, struct ctf_named_field_class, i);
637 _ctf_named_field_class_fini(named_fc);
640 g_array_free(fc->members, TRUE);
646 static inline void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
649 ctf_field_class_destroy(fc->elem_fc);
652 static inline void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
655 _ctf_field_class_array_base_fini(&fc->base);
659 static inline void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
662 _ctf_field_class_array_base_fini(&fc->base);
664 if (fc->length_ref) {
665 g_string_free(fc->length_ref, TRUE);
668 ctf_field_path_fini(&fc->length_path);
672 static inline void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
679 for (i = 0; i < fc->options->len; i++) {
680 struct ctf_named_field_class *named_fc =
681 &bt_g_array_index(fc->options, struct ctf_named_field_class, i);
683 _ctf_named_field_class_fini(named_fc);
686 g_array_free(fc->options, TRUE);
690 g_array_free(fc->ranges, TRUE);
694 g_string_free(fc->tag_ref, TRUE);
697 ctf_field_path_fini(&fc->tag_path);
701 static inline void ctf_field_class_destroy(struct ctf_field_class *fc)
708 case CTF_FIELD_CLASS_TYPE_INT:
709 _ctf_field_class_int_destroy(ctf_field_class_as_int(fc));
711 case CTF_FIELD_CLASS_TYPE_ENUM:
712 _ctf_field_class_enum_destroy(ctf_field_class_as_enum(fc));
714 case CTF_FIELD_CLASS_TYPE_FLOAT:
715 _ctf_field_class_float_destroy(ctf_field_class_as_float(fc));
717 case CTF_FIELD_CLASS_TYPE_STRING:
718 _ctf_field_class_string_destroy(ctf_field_class_as_string(fc));
720 case CTF_FIELD_CLASS_TYPE_STRUCT:
721 _ctf_field_class_struct_destroy(ctf_field_class_as_struct(fc));
723 case CTF_FIELD_CLASS_TYPE_ARRAY:
724 _ctf_field_class_array_destroy(ctf_field_class_as_array(fc));
726 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
727 _ctf_field_class_sequence_destroy(ctf_field_class_as_sequence(fc));
729 case CTF_FIELD_CLASS_TYPE_VARIANT:
730 _ctf_field_class_variant_destroy(ctf_field_class_as_variant(fc));
737 static inline struct ctf_range *
738 ctf_field_class_enum_mapping_borrow_range_by_index(struct ctf_field_class_enum_mapping *mapping,
741 BT_ASSERT_DBG(mapping);
742 BT_ASSERT_DBG(index < mapping->ranges->len);
743 return &bt_g_array_index(mapping->ranges, struct ctf_range, index);
746 static inline struct ctf_field_class_enum_mapping *
747 ctf_field_class_enum_borrow_mapping_by_index(struct ctf_field_class_enum *fc, uint64_t index)
750 BT_ASSERT_DBG(index < fc->mappings->len);
751 return &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, index);
754 static inline struct ctf_field_class_enum_mapping *
755 ctf_field_class_enum_borrow_mapping_by_label(struct ctf_field_class_enum *fc, const char *label)
757 struct ctf_field_class_enum_mapping *ret_mapping = NULL;
761 BT_ASSERT_DBG(label);
763 for (i = 0; i < fc->mappings->len; i++) {
764 struct ctf_field_class_enum_mapping *mapping =
765 ctf_field_class_enum_borrow_mapping_by_index(fc, i);
767 if (strcmp(mapping->label->str, label) == 0) {
768 ret_mapping = mapping;
777 static inline void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc,
778 const char *label, uint64_t u_lower,
781 struct ctf_field_class_enum_mapping *mapping = NULL;
782 struct ctf_range range = {
797 for (i = 0; i < fc->mappings->len; i++) {
798 mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, i);
800 if (strcmp(mapping->label->str, label) == 0) {
805 if (i == fc->mappings->len) {
810 g_array_set_size(fc->mappings, fc->mappings->len + 1);
811 mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, fc->mappings->len - 1);
812 _ctf_field_class_enum_mapping_init(mapping);
813 g_string_assign(mapping->label, label);
816 g_array_append_val(mapping->ranges, range);
819 static inline struct ctf_named_field_class *
820 ctf_field_class_struct_borrow_member_by_index(struct ctf_field_class_struct *fc, uint64_t index)
823 BT_ASSERT_DBG(index < fc->members->len);
824 return &bt_g_array_index(fc->members, struct ctf_named_field_class, index);
827 static inline struct ctf_named_field_class *
828 ctf_field_class_struct_borrow_member_by_name(struct ctf_field_class_struct *fc, const char *name)
831 struct ctf_named_field_class *ret_named_fc = NULL;
836 for (i = 0; i < fc->members->len; i++) {
837 struct ctf_named_field_class *named_fc =
838 ctf_field_class_struct_borrow_member_by_index(fc, i);
840 if (strcmp(name, named_fc->name->str) == 0) {
841 ret_named_fc = named_fc;
850 static inline struct ctf_field_class *
851 ctf_field_class_struct_borrow_member_field_class_by_name(struct ctf_field_class_struct *struct_fc,
854 struct ctf_named_field_class *named_fc = NULL;
855 struct ctf_field_class *fc = NULL;
861 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
872 static inline struct ctf_field_class_int *
873 ctf_field_class_struct_borrow_member_int_field_class_by_name(
874 struct ctf_field_class_struct *struct_fc, const char *name)
876 ctf_field_class *member_fc =
877 ctf_field_class_struct_borrow_member_field_class_by_name(struct_fc, name);
883 if (member_fc->type != CTF_FIELD_CLASS_TYPE_INT &&
884 member_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
888 return ctf_field_class_as_int(member_fc);
891 static inline void _ctf_named_field_class_unescape_orig_name(struct ctf_named_field_class *named_fc)
893 const char *name = named_fc->orig_name->str;
895 if (name[0] == '_') {
899 g_string_assign(named_fc->name, name);
902 static inline void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
903 const char *orig_name,
904 struct ctf_field_class *member_fc)
906 struct ctf_named_field_class *named_fc;
909 BT_ASSERT(orig_name);
910 g_array_set_size(fc->members, fc->members->len + 1);
912 named_fc = &bt_g_array_index(fc->members, struct ctf_named_field_class, fc->members->len - 1);
913 _ctf_named_field_class_init(named_fc);
914 g_string_assign(named_fc->orig_name, orig_name);
915 _ctf_named_field_class_unescape_orig_name(named_fc);
916 named_fc->fc = member_fc;
918 if (member_fc->alignment > fc->base.alignment) {
919 fc->base.alignment = member_fc->alignment;
923 static inline struct ctf_named_field_class *
924 ctf_field_class_variant_borrow_option_by_index(struct ctf_field_class_variant *fc, uint64_t index)
927 BT_ASSERT_DBG(index < fc->options->len);
928 return &bt_g_array_index(fc->options, struct ctf_named_field_class, index);
931 static inline struct ctf_named_field_class *
932 ctf_field_class_variant_borrow_option_by_name(struct ctf_field_class_variant *fc, const char *name)
935 struct ctf_named_field_class *ret_named_fc = NULL;
940 for (i = 0; i < fc->options->len; i++) {
941 struct ctf_named_field_class *named_fc =
942 ctf_field_class_variant_borrow_option_by_index(fc, i);
944 if (strcmp(name, named_fc->name->str) == 0) {
945 ret_named_fc = named_fc;
954 static inline struct ctf_field_class_variant_range *
955 ctf_field_class_variant_borrow_range_by_index(struct ctf_field_class_variant *fc, uint64_t index)
958 BT_ASSERT_DBG(index < fc->ranges->len);
959 return &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, index);
962 static inline void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
963 const char *orig_name,
964 struct ctf_field_class *option_fc)
966 struct ctf_named_field_class *named_fc;
969 BT_ASSERT(orig_name);
970 g_array_set_size(fc->options, fc->options->len + 1);
972 named_fc = &bt_g_array_index(fc->options, struct ctf_named_field_class, fc->options->len - 1);
973 _ctf_named_field_class_init(named_fc);
974 g_string_assign(named_fc->orig_name, orig_name);
975 _ctf_named_field_class_unescape_orig_name(named_fc);
976 named_fc->fc = option_fc;
979 static inline void ctf_field_class_variant_set_tag_field_class(struct ctf_field_class_variant *fc,
980 struct ctf_field_class_enum *tag_fc)
988 for (option_i = 0; option_i < fc->options->len; option_i++) {
990 struct ctf_named_field_class *named_fc =
991 ctf_field_class_variant_borrow_option_by_index(fc, option_i);
992 struct ctf_field_class_enum_mapping *mapping;
994 mapping = ctf_field_class_enum_borrow_mapping_by_label(tag_fc, named_fc->orig_name->str);
999 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1000 struct ctf_range *range =
1001 ctf_field_class_enum_mapping_borrow_range_by_index(mapping, range_i);
1002 struct ctf_field_class_variant_range var_range;
1004 var_range.range = *range;
1005 var_range.option_index = option_i;
1006 g_array_append_val(fc->ranges, var_range);
1011 static inline struct ctf_field_class *
1012 ctf_field_class_compound_borrow_field_class_by_index(struct ctf_field_class *comp_fc,
1015 struct ctf_field_class *fc = NULL;
1017 switch (comp_fc->type) {
1018 case CTF_FIELD_CLASS_TYPE_STRUCT:
1020 struct ctf_named_field_class *named_fc = ctf_field_class_struct_borrow_member_by_index(
1021 (struct ctf_field_class_struct *) comp_fc, index);
1023 BT_ASSERT_DBG(named_fc);
1027 case CTF_FIELD_CLASS_TYPE_VARIANT:
1029 struct ctf_named_field_class *named_fc = ctf_field_class_variant_borrow_option_by_index(
1030 (struct ctf_field_class_variant *) comp_fc, index);
1032 BT_ASSERT_DBG(named_fc);
1036 case CTF_FIELD_CLASS_TYPE_ARRAY:
1037 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1039 struct ctf_field_class_array_base *array_fc = (struct ctf_field_class_array_base *) comp_fc;
1041 fc = array_fc->elem_fc;
1051 static inline uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
1053 uint64_t field_count;
1056 case CTF_FIELD_CLASS_TYPE_STRUCT:
1058 struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc;
1060 field_count = struct_fc->members->len;
1063 case CTF_FIELD_CLASS_TYPE_VARIANT:
1065 struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc;
1067 field_count = var_fc->options->len;
1070 case CTF_FIELD_CLASS_TYPE_ARRAY:
1071 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1073 * Array and sequence types always contain a single
1074 * member (the element type).
1085 static inline int64_t
1086 ctf_field_class_compound_get_field_class_index_from_orig_name(struct ctf_field_class *fc,
1087 const char *orig_name)
1089 int64_t ret_index = -1;
1093 case CTF_FIELD_CLASS_TYPE_STRUCT:
1095 struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc;
1097 for (i = 0; i < struct_fc->members->len; i++) {
1098 struct ctf_named_field_class *named_fc =
1099 ctf_field_class_struct_borrow_member_by_index(struct_fc, i);
1101 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1102 ret_index = (int64_t) i;
1109 case CTF_FIELD_CLASS_TYPE_VARIANT:
1111 struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc;
1113 for (i = 0; i < var_fc->options->len; i++) {
1114 struct ctf_named_field_class *named_fc =
1115 ctf_field_class_variant_borrow_option_by_index(var_fc, i);
1117 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1118 ret_index = (int64_t) i;
1133 static inline void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index)
1136 g_array_append_val(fp->path, index);
1139 static inline int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path *fp,
1143 BT_ASSERT_DBG(index < fp->path->len);
1144 return bt_g_array_index(fp->path, int64_t, index);
1147 static inline void ctf_field_path_clear(struct ctf_field_path *fp)
1150 g_array_set_size(fp->path, 0);
1153 static inline const char *ctf_scope_string(enum ctf_scope scope)
1156 case CTF_SCOPE_PACKET_HEADER:
1157 return "PACKET_HEADER";
1158 case CTF_SCOPE_PACKET_CONTEXT:
1159 return "PACKET_CONTEXT";
1160 case CTF_SCOPE_EVENT_HEADER:
1161 return "EVENT_HEADER";
1162 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1163 return "EVENT_COMMON_CONTEXT";
1164 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1165 return "EVENT_SPECIFIC_CONTEXT";
1166 case CTF_SCOPE_EVENT_PAYLOAD:
1167 return "EVENT_PAYLOAD";
1173 static inline GString *ctf_field_path_string(struct ctf_field_path *path)
1175 GString *str = g_string_new(NULL);
1184 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
1186 for (i = 0; i < path->path->len; i++) {
1187 g_string_append_printf(str, ", %" PRId64, ctf_field_path_borrow_index_by_index(path, i));
1190 g_string_append(str, "]");
1196 static inline struct ctf_field_class *
1197 ctf_field_path_borrow_field_class(struct ctf_field_path *field_path, struct ctf_trace_class *tc,
1198 struct ctf_stream_class *sc, struct ctf_event_class *ec)
1201 struct ctf_field_class *fc;
1203 switch (field_path->root) {
1204 case CTF_SCOPE_PACKET_HEADER:
1205 fc = tc->packet_header_fc;
1207 case CTF_SCOPE_PACKET_CONTEXT:
1208 fc = sc->packet_context_fc;
1210 case CTF_SCOPE_EVENT_HEADER:
1211 fc = sc->event_header_fc;
1213 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1214 fc = sc->event_common_context_fc;
1216 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1217 fc = ec->spec_context_fc;
1219 case CTF_SCOPE_EVENT_PAYLOAD:
1220 fc = ec->payload_fc;
1228 for (i = 0; i < field_path->path->len; i++) {
1229 int64_t child_index = ctf_field_path_borrow_index_by_index(field_path, i);
1230 struct ctf_field_class *child_fc =
1231 ctf_field_class_compound_borrow_field_class_by_index(fc, child_index);
1232 BT_ASSERT_DBG(child_fc);
1240 static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
1242 static inline void ctf_field_class_bit_array_copy_content(struct ctf_field_class_bit_array *dst_fc,
1243 struct ctf_field_class_bit_array *src_fc)
1247 dst_fc->byte_order = src_fc->byte_order;
1248 dst_fc->size = src_fc->size;
1251 static inline void ctf_field_class_int_copy_content(struct ctf_field_class_int *dst_fc,
1252 struct ctf_field_class_int *src_fc)
1254 ctf_field_class_bit_array_copy_content(&dst_fc->base, &src_fc->base);
1255 dst_fc->meaning = src_fc->meaning;
1256 dst_fc->is_signed = src_fc->is_signed;
1257 dst_fc->disp_base = src_fc->disp_base;
1258 dst_fc->encoding = src_fc->encoding;
1259 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
1260 dst_fc->storing_index = src_fc->storing_index;
1263 static inline struct ctf_field_class_int *_ctf_field_class_int_copy(struct ctf_field_class_int *fc)
1265 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
1268 ctf_field_class_int_copy_content(copy_fc, fc);
1272 static inline struct ctf_field_class_enum *
1273 _ctf_field_class_enum_copy(struct ctf_field_class_enum *fc)
1275 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
1279 ctf_field_class_int_copy_content(©_fc->base, &fc->base);
1281 for (i = 0; i < fc->mappings->len; i++) {
1284 struct ctf_field_class_enum_mapping *mapping =
1285 &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i);
1287 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1288 struct ctf_range *range = &bt_g_array_index(mapping->ranges, struct ctf_range, range_i);
1290 ctf_field_class_enum_map_range(copy_fc, mapping->label->str, range->lower.u,
1298 static inline struct ctf_field_class_float *
1299 _ctf_field_class_float_copy(struct ctf_field_class_float *fc)
1301 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
1304 ctf_field_class_bit_array_copy_content(©_fc->base, &fc->base);
1308 static inline struct ctf_field_class_string *
1309 _ctf_field_class_string_copy(struct ctf_field_class_string *)
1311 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
1317 static inline struct ctf_field_class_struct *
1318 _ctf_field_class_struct_copy(struct ctf_field_class_struct *fc)
1320 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
1325 for (i = 0; i < fc->members->len; i++) {
1326 struct ctf_named_field_class *named_fc =
1327 &bt_g_array_index(fc->members, struct ctf_named_field_class, i);
1329 ctf_field_class_struct_append_member(copy_fc, named_fc->name->str,
1330 ctf_field_class_copy(named_fc->fc));
1336 static inline void ctf_field_path_copy_content(struct ctf_field_path *dst_fp,
1337 struct ctf_field_path *src_fp)
1343 dst_fp->root = src_fp->root;
1344 ctf_field_path_clear(dst_fp);
1346 for (i = 0; i < src_fp->path->len; i++) {
1347 int64_t index = ctf_field_path_borrow_index_by_index(src_fp, i);
1349 ctf_field_path_append_index(dst_fp, index);
1353 static inline struct ctf_field_class_variant *
1354 _ctf_field_class_variant_copy(struct ctf_field_class_variant *fc)
1356 struct ctf_field_class_variant *copy_fc = ctf_field_class_variant_create();
1361 for (i = 0; i < fc->options->len; i++) {
1362 struct ctf_named_field_class *named_fc =
1363 &bt_g_array_index(fc->options, struct ctf_named_field_class, i);
1365 ctf_field_class_variant_append_option(copy_fc, named_fc->name->str,
1366 ctf_field_class_copy(named_fc->fc));
1369 for (i = 0; i < fc->ranges->len; i++) {
1370 struct ctf_field_class_variant_range *range =
1371 &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, i);
1373 g_array_append_val(copy_fc->ranges, *range);
1376 ctf_field_path_copy_content(©_fc->tag_path, &fc->tag_path);
1377 g_string_assign(copy_fc->tag_ref, fc->tag_ref->str);
1378 copy_fc->stored_tag_index = fc->stored_tag_index;
1383 ctf_field_class_array_base_copy_content(struct ctf_field_class_array_base *dst_fc,
1384 struct ctf_field_class_array_base *src_fc)
1388 dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc);
1389 dst_fc->is_text = src_fc->is_text;
1392 static inline struct ctf_field_class_array *
1393 _ctf_field_class_array_copy(struct ctf_field_class_array *fc)
1395 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
1398 ctf_field_class_array_base_copy_content(©_fc->base, &fc->base);
1399 copy_fc->length = fc->length;
1403 static inline struct ctf_field_class_sequence *
1404 _ctf_field_class_sequence_copy(struct ctf_field_class_sequence *fc)
1406 struct ctf_field_class_sequence *copy_fc = ctf_field_class_sequence_create();
1409 ctf_field_class_array_base_copy_content(©_fc->base, &fc->base);
1410 ctf_field_path_copy_content(©_fc->length_path, &fc->length_path);
1411 g_string_assign(copy_fc->length_ref, fc->length_ref->str);
1412 copy_fc->stored_length_index = fc->stored_length_index;
1416 static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
1418 struct ctf_field_class *copy_fc = NULL;
1425 * Translation should not have happened yet.
1427 BT_ASSERT(!fc->ir_fc);
1430 case CTF_FIELD_CLASS_TYPE_INT:
1431 copy_fc = &_ctf_field_class_int_copy(ctf_field_class_as_int(fc))->base.base;
1433 case CTF_FIELD_CLASS_TYPE_ENUM:
1434 copy_fc = &_ctf_field_class_enum_copy(ctf_field_class_as_enum(fc))->base.base.base;
1436 case CTF_FIELD_CLASS_TYPE_FLOAT:
1437 copy_fc = &_ctf_field_class_float_copy(ctf_field_class_as_float(fc))->base.base;
1439 case CTF_FIELD_CLASS_TYPE_STRING:
1440 copy_fc = &_ctf_field_class_string_copy(ctf_field_class_as_string(fc))->base;
1442 case CTF_FIELD_CLASS_TYPE_STRUCT:
1443 copy_fc = &_ctf_field_class_struct_copy(ctf_field_class_as_struct(fc))->base;
1445 case CTF_FIELD_CLASS_TYPE_ARRAY:
1446 copy_fc = &_ctf_field_class_array_copy(ctf_field_class_as_array(fc))->base.base;
1448 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1449 copy_fc = &_ctf_field_class_sequence_copy(ctf_field_class_as_sequence(fc))->base.base;
1451 case CTF_FIELD_CLASS_TYPE_VARIANT:
1452 copy_fc = &_ctf_field_class_variant_copy(ctf_field_class_as_variant(fc))->base;
1458 copy_fc->type = fc->type;
1459 copy_fc->alignment = fc->alignment;
1460 copy_fc->in_ir = fc->in_ir;
1466 static inline struct ctf_event_class *ctf_event_class_create(void)
1468 struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1);
1471 ec->name = g_string_new(NULL);
1472 BT_ASSERT(ec->name);
1473 ec->emf_uri = g_string_new(NULL);
1474 BT_ASSERT(ec->emf_uri);
1475 ec->is_log_level_set = false;
1479 static inline void ctf_event_class_set_log_level(struct ctf_event_class *ec,
1480 enum bt_event_class_log_level log_level)
1483 ec->log_level = log_level;
1484 ec->is_log_level_set = true;
1487 static inline void ctf_event_class_destroy(struct ctf_event_class *ec)
1494 g_string_free(ec->name, TRUE);
1498 g_string_free(ec->emf_uri, TRUE);
1501 ctf_field_class_destroy(ec->spec_context_fc);
1502 ctf_field_class_destroy(ec->payload_fc);
1506 static inline struct ctf_stream_class *ctf_stream_class_create(void)
1508 struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1);
1511 sc->event_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_event_class_destroy);
1512 BT_ASSERT(sc->event_classes);
1513 sc->event_classes_by_id = g_hash_table_new(g_direct_hash, g_direct_equal);
1514 BT_ASSERT(sc->event_classes_by_id);
1518 static inline void ctf_stream_class_destroy(struct ctf_stream_class *sc)
1524 if (sc->event_classes) {
1525 g_ptr_array_free(sc->event_classes, TRUE);
1528 if (sc->event_classes_by_id) {
1529 g_hash_table_destroy(sc->event_classes_by_id);
1532 ctf_field_class_destroy(sc->packet_context_fc);
1533 ctf_field_class_destroy(sc->event_header_fc);
1534 ctf_field_class_destroy(sc->event_common_context_fc);
1538 static inline void ctf_stream_class_append_event_class(struct ctf_stream_class *sc,
1539 struct ctf_event_class *ec)
1541 g_ptr_array_add(sc->event_classes, ec);
1542 g_hash_table_insert(sc->event_classes_by_id, GUINT_TO_POINTER((guint) ec->id), ec);
1545 static inline struct ctf_event_class *
1546 ctf_stream_class_borrow_event_class_by_id(struct ctf_stream_class *sc, uint64_t type)
1549 return (struct ctf_event_class *) g_hash_table_lookup(sc->event_classes_by_id,
1550 GUINT_TO_POINTER((guint) type));
1553 static inline void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry)
1556 entry->name = g_string_new(NULL);
1557 BT_ASSERT(entry->name);
1558 entry->value.str = g_string_new(NULL);
1559 BT_ASSERT(entry->value.str);
1562 static inline void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry)
1567 g_string_free(entry->name, TRUE);
1570 if (entry->value.str) {
1571 g_string_free(entry->value.str, TRUE);
1575 static inline struct ctf_clock_class *ctf_clock_class_create(void)
1577 struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1);
1580 cc->name = g_string_new(NULL);
1581 BT_ASSERT(cc->name);
1582 cc->description = g_string_new(NULL);
1583 BT_ASSERT(cc->description);
1587 static inline void ctf_clock_class_destroy(struct ctf_clock_class *cc)
1594 g_string_free(cc->name, TRUE);
1597 if (cc->description) {
1598 g_string_free(cc->description, TRUE);
1601 bt_clock_class_put_ref(cc->ir_cc);
1605 static inline struct ctf_trace_class *ctf_trace_class_create(void)
1607 struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1);
1610 tc->default_byte_order = CTF_BYTE_ORDER_UNKNOWN;
1611 tc->clock_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_clock_class_destroy);
1612 BT_ASSERT(tc->clock_classes);
1613 tc->stream_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_stream_class_destroy);
1614 BT_ASSERT(tc->stream_classes);
1615 tc->env_entries = g_array_new(FALSE, TRUE, sizeof(struct ctf_trace_class_env_entry));
1619 static inline void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1625 ctf_field_class_destroy(tc->packet_header_fc);
1627 if (tc->clock_classes) {
1628 g_ptr_array_free(tc->clock_classes, TRUE);
1631 if (tc->stream_classes) {
1632 g_ptr_array_free(tc->stream_classes, TRUE);
1635 if (tc->env_entries) {
1638 for (i = 0; i < tc->env_entries->len; i++) {
1639 struct ctf_trace_class_env_entry *entry =
1640 &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, i);
1642 _ctf_trace_class_env_entry_fini(entry);
1645 g_array_free(tc->env_entries, TRUE);
1651 static inline void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc, const char *name,
1652 enum ctf_trace_class_env_entry_type type,
1653 const char *str_value, int64_t i_value)
1655 struct ctf_trace_class_env_entry *entry;
1659 g_array_set_size(tc->env_entries, tc->env_entries->len + 1);
1661 entry = &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry,
1662 tc->env_entries->len - 1);
1664 _ctf_trace_class_env_entry_init(entry);
1665 g_string_assign(entry->name, name);
1668 g_string_assign(entry->value.str, str_value);
1671 entry->value.i = i_value;
1674 static inline struct ctf_stream_class *
1675 ctf_trace_class_borrow_stream_class_by_id(struct ctf_trace_class *tc, uint64_t id)
1678 struct ctf_stream_class *ret_sc = NULL;
1682 for (i = 0; i < tc->stream_classes->len; i++) {
1683 struct ctf_stream_class *sc = (struct ctf_stream_class *) tc->stream_classes->pdata[i];
1695 static inline struct ctf_clock_class *
1696 ctf_trace_class_borrow_clock_class_by_name(struct ctf_trace_class *tc, const char *name)
1699 struct ctf_clock_class *ret_cc = NULL;
1702 BT_ASSERT_DBG(name);
1704 for (i = 0; i < tc->clock_classes->len; i++) {
1705 struct ctf_clock_class *cc = (struct ctf_clock_class *) tc->clock_classes->pdata[i];
1707 BT_ASSERT_DBG(cc->name);
1708 if (strcmp(cc->name->str, name) == 0) {
1718 static inline struct ctf_trace_class_env_entry *
1719 ctf_trace_class_borrow_env_entry_by_index(struct ctf_trace_class *tc, uint64_t index)
1722 BT_ASSERT_DBG(index < tc->env_entries->len);
1723 return &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, index);
1726 static inline struct ctf_trace_class_env_entry *
1727 ctf_trace_class_borrow_env_entry_by_name(struct ctf_trace_class *tc, const char *name)
1729 struct ctf_trace_class_env_entry *ret_entry = NULL;
1733 BT_ASSERT_DBG(name);
1735 for (i = 0; i < tc->env_entries->len; i++) {
1736 struct ctf_trace_class_env_entry *env_entry =
1737 ctf_trace_class_borrow_env_entry_by_index(tc, i);
1739 if (strcmp(env_entry->name->str, name) == 0) {
1740 ret_entry = env_entry;
1749 #endif /* _CTF_META_H */