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/uuid.h"
21 #include "common/assert.h"
26 enum ctf_field_class_type
{
27 CTF_FIELD_CLASS_TYPE_INT
,
28 CTF_FIELD_CLASS_TYPE_ENUM
,
29 CTF_FIELD_CLASS_TYPE_FLOAT
,
30 CTF_FIELD_CLASS_TYPE_STRING
,
31 CTF_FIELD_CLASS_TYPE_STRUCT
,
32 CTF_FIELD_CLASS_TYPE_ARRAY
,
33 CTF_FIELD_CLASS_TYPE_SEQUENCE
,
34 CTF_FIELD_CLASS_TYPE_VARIANT
,
37 enum ctf_field_class_meaning
{
38 CTF_FIELD_CLASS_MEANING_NONE
,
39 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
,
40 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
,
41 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
,
42 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
,
43 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
,
44 CTF_FIELD_CLASS_MEANING_MAGIC
,
45 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
,
46 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
,
47 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
,
48 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
,
49 CTF_FIELD_CLASS_MEANING_UUID
,
53 CTF_BYTE_ORDER_UNKNOWN
,
54 CTF_BYTE_ORDER_DEFAULT
,
55 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
{
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
{
90 enum ctf_field_class_type type
;
91 unsigned int alignment
;
95 /* Weak, set during translation. NULL if `in_ir` is false below. */
96 bt_field_class
*ir_fc
;
99 struct ctf_field_class_bit_array
{
100 struct ctf_field_class base
;
101 enum ctf_byte_order byte_order
;
105 struct ctf_field_class_int
{
106 struct ctf_field_class_bit_array base
;
107 enum ctf_field_class_meaning meaning
;
109 bt_field_class_integer_preferred_display_base disp_base
;
110 enum ctf_encoding encoding
;
111 int64_t storing_index
;
114 struct ctf_clock_class
*mapped_clock_class
;
129 struct ctf_field_class_enum_mapping
{
132 /* Array of `struct ctf_range` */
136 struct ctf_field_class_enum
{
137 struct ctf_field_class_int base
;
139 /* Array of `struct ctf_field_class_enum_mapping` */
143 struct ctf_field_class_float
{
144 struct ctf_field_class_bit_array base
;
147 struct ctf_field_class_string
{
148 struct ctf_field_class base
;
149 enum ctf_encoding encoding
;
152 struct ctf_named_field_class
{
153 /* Original name which can include a leading `_` */
156 /* Name as translated to trace IR (leading `_` removed) */
160 struct ctf_field_class
*fc
;
163 struct ctf_field_class_struct
{
164 struct ctf_field_class base
;
166 /* Array of `struct ctf_named_field_class` */
170 struct ctf_field_path
{
173 /* Array of `int64_t` */
177 struct ctf_field_class_variant_range
{
178 struct ctf_range range
;
179 uint64_t option_index
;
182 struct ctf_field_class_variant
{
183 struct ctf_field_class base
;
185 struct ctf_field_path tag_path
;
186 uint64_t stored_tag_index
;
188 /* Array of `struct ctf_named_field_class` */
191 /* Array of `struct ctf_field_class_variant_range` */
195 struct ctf_field_class_enum
*tag_fc
;
198 struct ctf_field_class_array_base
{
199 struct ctf_field_class base
;
200 struct ctf_field_class
*elem_fc
;
204 struct ctf_field_class_array
{
205 struct ctf_field_class_array_base base
;
206 enum ctf_field_class_meaning meaning
;
210 struct ctf_field_class_sequence
{
211 struct ctf_field_class_array_base base
;
213 struct ctf_field_path length_path
;
214 uint64_t stored_length_index
;
217 struct ctf_field_class_int
*length_fc
;
220 struct ctf_event_class
{
224 bt_event_class_log_level log_level
;
226 bool is_log_level_set
;
229 struct ctf_field_class
*spec_context_fc
;
232 struct ctf_field_class
*payload_fc
;
234 /* Weak, set during translation */
235 bt_event_class
*ir_ec
;
238 struct ctf_stream_class
{
241 bool packets_have_ts_begin
;
242 bool packets_have_ts_end
;
243 bool has_discarded_events
;
244 bool has_discarded_packets
;
245 bool discarded_events_have_default_cs
;
246 bool discarded_packets_have_default_cs
;
249 struct ctf_field_class
*packet_context_fc
;
252 struct ctf_field_class
*event_header_fc
;
255 struct ctf_field_class
*event_common_context_fc
;
257 /* Array of `struct ctf_event_class *`, owned by this */
258 GPtrArray
*event_classes
;
261 * Hash table mapping event class IDs to `struct ctf_event_class *`,
264 GHashTable
*event_classes_by_id
;
267 struct ctf_clock_class
*default_clock_class
;
269 /* Weak, set during translation */
270 bt_stream_class
*ir_sc
;
273 enum ctf_trace_class_env_entry_type
{
274 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
275 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
278 struct ctf_trace_class_env_entry
{
279 enum ctf_trace_class_env_entry_type type
;
288 struct ctf_trace_class
{
293 enum ctf_byte_order default_byte_order
;
296 struct ctf_field_class
*packet_header_fc
;
298 uint64_t stored_value_count
;
300 /* Array of `struct ctf_clock_class *` (owned by this) */
301 GPtrArray
*clock_classes
;
303 /* Array of `struct ctf_stream_class *` */
304 GPtrArray
*stream_classes
;
306 /* Array of `struct ctf_trace_class_env_entry` */
311 /* Weak, set during translation */
312 bt_trace_class
*ir_tc
;
320 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
323 void _ctf_field_class_init(struct ctf_field_class
*fc
,
324 enum ctf_field_class_type type
, unsigned int alignment
)
328 fc
->alignment
= alignment
;
333 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
334 enum ctf_field_class_type type
)
336 _ctf_field_class_init((void *) fc
, type
, 1);
340 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
341 enum ctf_field_class_type type
)
343 _ctf_field_class_bit_array_init((void *) fc
, type
);
344 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
345 fc
->storing_index
= -1;
349 void ctf_field_path_init(struct ctf_field_path
*field_path
)
351 BT_ASSERT(field_path
);
352 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
353 BT_ASSERT(field_path
->path
);
357 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
359 BT_ASSERT(field_path
);
361 if (field_path
->path
) {
362 g_array_free(field_path
->path
, TRUE
);
367 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
370 named_fc
->name
= g_string_new(NULL
);
371 BT_ASSERT(named_fc
->name
);
372 named_fc
->orig_name
= g_string_new(NULL
);
373 BT_ASSERT(named_fc
->orig_name
);
377 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
381 if (named_fc
->name
) {
382 g_string_free(named_fc
->name
, TRUE
);
385 if (named_fc
->orig_name
) {
386 g_string_free(named_fc
->orig_name
, TRUE
);
389 ctf_field_class_destroy(named_fc
->fc
);
393 void _ctf_field_class_enum_mapping_init(
394 struct ctf_field_class_enum_mapping
*mapping
)
397 mapping
->label
= g_string_new(NULL
);
398 BT_ASSERT(mapping
->label
);
399 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
400 BT_ASSERT(mapping
->ranges
);
404 void _ctf_field_class_enum_mapping_fini(
405 struct ctf_field_class_enum_mapping
*mapping
)
409 if (mapping
->label
) {
410 g_string_free(mapping
->label
, TRUE
);
413 if (mapping
->ranges
) {
414 g_array_free(mapping
->ranges
, TRUE
);
419 struct ctf_field_class_int
*ctf_field_class_int_create(void)
421 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
424 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
429 struct ctf_field_class_float
*ctf_field_class_float_create(void)
431 struct ctf_field_class_float
*fc
=
432 g_new0(struct ctf_field_class_float
, 1);
435 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
440 struct ctf_field_class_string
*ctf_field_class_string_create(void)
442 struct ctf_field_class_string
*fc
=
443 g_new0(struct ctf_field_class_string
, 1);
446 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
451 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
453 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
456 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
457 fc
->mappings
= g_array_new(FALSE
, TRUE
,
458 sizeof(struct ctf_field_class_enum_mapping
));
459 BT_ASSERT(fc
->mappings
);
464 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
466 struct ctf_field_class_struct
*fc
=
467 g_new0(struct ctf_field_class_struct
, 1);
470 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
471 fc
->members
= g_array_new(FALSE
, TRUE
,
472 sizeof(struct ctf_named_field_class
));
473 BT_ASSERT(fc
->members
);
474 fc
->base
.is_compound
= true;
479 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
481 struct ctf_field_class_variant
*fc
=
482 g_new0(struct ctf_field_class_variant
, 1);
485 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
486 fc
->options
= g_array_new(FALSE
, TRUE
,
487 sizeof(struct ctf_named_field_class
));
488 BT_ASSERT(fc
->options
);
489 fc
->ranges
= g_array_new(FALSE
, TRUE
,
490 sizeof(struct ctf_field_class_variant_range
));
491 BT_ASSERT(fc
->ranges
);
492 fc
->tag_ref
= g_string_new(NULL
);
493 BT_ASSERT(fc
->tag_ref
);
494 ctf_field_path_init(&fc
->tag_path
);
495 fc
->base
.is_compound
= true;
500 struct ctf_field_class_array
*ctf_field_class_array_create(void)
502 struct ctf_field_class_array
*fc
=
503 g_new0(struct ctf_field_class_array
, 1);
506 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
507 fc
->base
.base
.is_compound
= true;
512 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
514 struct ctf_field_class_sequence
*fc
=
515 g_new0(struct ctf_field_class_sequence
, 1);
518 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
519 fc
->length_ref
= g_string_new(NULL
);
520 BT_ASSERT(fc
->length_ref
);
521 ctf_field_path_init(&fc
->length_path
);
522 fc
->base
.base
.is_compound
= true;
527 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
534 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
541 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
542 struct ctf_field_class_enum_mapping
*mapping
=
543 &g_array_index(fc
->mappings
,
544 struct ctf_field_class_enum_mapping
, i
);
546 _ctf_field_class_enum_mapping_fini(mapping
);
549 g_array_free(fc
->mappings
, TRUE
);
556 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
563 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
570 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
577 for (i
= 0; i
< fc
->members
->len
; i
++) {
578 struct ctf_named_field_class
*named_fc
=
579 &g_array_index(fc
->members
,
580 struct ctf_named_field_class
, i
);
582 _ctf_named_field_class_fini(named_fc
);
585 g_array_free(fc
->members
, TRUE
);
592 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
595 ctf_field_class_destroy(fc
->elem_fc
);
599 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
602 _ctf_field_class_array_base_fini((void *) fc
);
607 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
610 _ctf_field_class_array_base_fini((void *) fc
);
612 if (fc
->length_ref
) {
613 g_string_free(fc
->length_ref
, TRUE
);
616 ctf_field_path_fini(&fc
->length_path
);
621 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
628 for (i
= 0; i
< fc
->options
->len
; i
++) {
629 struct ctf_named_field_class
*named_fc
=
630 &g_array_index(fc
->options
,
631 struct ctf_named_field_class
, i
);
633 _ctf_named_field_class_fini(named_fc
);
636 g_array_free(fc
->options
, TRUE
);
640 g_array_free(fc
->ranges
, TRUE
);
644 g_string_free(fc
->tag_ref
, TRUE
);
647 ctf_field_path_fini(&fc
->tag_path
);
652 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
659 case CTF_FIELD_CLASS_TYPE_INT
:
660 _ctf_field_class_int_destroy((void *) fc
);
662 case CTF_FIELD_CLASS_TYPE_ENUM
:
663 _ctf_field_class_enum_destroy((void *) fc
);
665 case CTF_FIELD_CLASS_TYPE_FLOAT
:
666 _ctf_field_class_float_destroy((void *) fc
);
668 case CTF_FIELD_CLASS_TYPE_STRING
:
669 _ctf_field_class_string_destroy((void *) fc
);
671 case CTF_FIELD_CLASS_TYPE_STRUCT
:
672 _ctf_field_class_struct_destroy((void *) fc
);
674 case CTF_FIELD_CLASS_TYPE_ARRAY
:
675 _ctf_field_class_array_destroy((void *) fc
);
677 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
678 _ctf_field_class_sequence_destroy((void *) fc
);
680 case CTF_FIELD_CLASS_TYPE_VARIANT
:
681 _ctf_field_class_variant_destroy((void *) fc
);
689 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
690 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
693 BT_ASSERT(index
< mapping
->ranges
->len
);
694 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
698 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
699 struct ctf_field_class_enum
*fc
, uint64_t index
)
702 BT_ASSERT(index
< fc
->mappings
->len
);
703 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
708 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
709 struct ctf_field_class_enum
*fc
, const char *label
)
711 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
717 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
718 struct ctf_field_class_enum_mapping
*mapping
=
719 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
721 if (strcmp(mapping
->label
->str
, label
) == 0) {
722 ret_mapping
= mapping
;
732 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
733 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
735 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
736 struct ctf_range range
= {
745 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
746 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
749 if (strcmp(mapping
->label
->str
, label
) == 0) {
754 if (i
== fc
->mappings
->len
) {
759 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
760 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
761 fc
, fc
->mappings
->len
- 1);
762 _ctf_field_class_enum_mapping_init(mapping
);
763 g_string_assign(mapping
->label
, label
);
766 g_array_append_val(mapping
->ranges
, range
);
770 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
771 struct ctf_field_class_struct
*fc
, uint64_t index
)
774 BT_ASSERT(index
< fc
->members
->len
);
775 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
780 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
781 struct ctf_field_class_struct
*fc
, const char *name
)
784 struct ctf_named_field_class
*ret_named_fc
= NULL
;
789 for (i
= 0; i
< fc
->members
->len
; i
++) {
790 struct ctf_named_field_class
*named_fc
=
791 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
793 if (strcmp(name
, named_fc
->name
->str
) == 0) {
794 ret_named_fc
= named_fc
;
804 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
805 struct ctf_field_class_struct
*struct_fc
, const char *name
)
807 struct ctf_named_field_class
*named_fc
= NULL
;
808 struct ctf_field_class
*fc
= NULL
;
814 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
826 struct ctf_field_class_int
*
827 ctf_field_class_struct_borrow_member_int_field_class_by_name(
828 struct ctf_field_class_struct
*struct_fc
, const char *name
)
830 struct ctf_field_class_int
*int_fc
= NULL
;
833 ctf_field_class_struct_borrow_member_field_class_by_name(
839 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
840 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
850 void _ctf_named_field_class_unescape_orig_name(
851 struct ctf_named_field_class
*named_fc
)
853 const char *name
= named_fc
->orig_name
->str
;
855 if (name
[0] == '_') {
859 g_string_assign(named_fc
->name
, name
);
863 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
864 const char *orig_name
, struct ctf_field_class
*member_fc
)
866 struct ctf_named_field_class
*named_fc
;
869 BT_ASSERT(orig_name
);
870 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
872 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
873 fc
->members
->len
- 1);
874 _ctf_named_field_class_init(named_fc
);
875 g_string_assign(named_fc
->orig_name
, orig_name
);
876 _ctf_named_field_class_unescape_orig_name(named_fc
);
877 named_fc
->fc
= member_fc
;
879 if (member_fc
->alignment
> fc
->base
.alignment
) {
880 fc
->base
.alignment
= member_fc
->alignment
;
885 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
886 struct ctf_field_class_variant
*fc
, uint64_t index
)
889 BT_ASSERT(index
< fc
->options
->len
);
890 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
895 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
896 struct ctf_field_class_variant
*fc
, const char *name
)
899 struct ctf_named_field_class
*ret_named_fc
= NULL
;
904 for (i
= 0; i
< fc
->options
->len
; i
++) {
905 struct ctf_named_field_class
*named_fc
=
906 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
908 if (strcmp(name
, named_fc
->name
->str
) == 0) {
909 ret_named_fc
= named_fc
;
919 struct ctf_field_class_variant_range
*
920 ctf_field_class_variant_borrow_range_by_index(
921 struct ctf_field_class_variant
*fc
, uint64_t index
)
924 BT_ASSERT(index
< fc
->ranges
->len
);
925 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
930 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
931 const char *orig_name
, struct ctf_field_class
*option_fc
)
933 struct ctf_named_field_class
*named_fc
;
936 BT_ASSERT(orig_name
);
937 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
939 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
940 fc
->options
->len
- 1);
941 _ctf_named_field_class_init(named_fc
);
942 g_string_assign(named_fc
->orig_name
, orig_name
);
943 _ctf_named_field_class_unescape_orig_name(named_fc
);
944 named_fc
->fc
= option_fc
;
948 void ctf_field_class_variant_set_tag_field_class(
949 struct ctf_field_class_variant
*fc
,
950 struct ctf_field_class_enum
*tag_fc
)
958 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
960 struct ctf_named_field_class
*named_fc
=
961 ctf_field_class_variant_borrow_option_by_index(
963 struct ctf_field_class_enum_mapping
*mapping
;
965 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
966 tag_fc
, named_fc
->orig_name
->str
);
971 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
973 struct ctf_range
*range
=
974 ctf_field_class_enum_mapping_borrow_range_by_index(
976 struct ctf_field_class_variant_range var_range
;
978 var_range
.range
= *range
;
979 var_range
.option_index
= option_i
;
980 g_array_append_val(fc
->ranges
, var_range
);
986 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
987 struct ctf_field_class
*comp_fc
, uint64_t index
)
989 struct ctf_field_class
*fc
= NULL
;
991 switch (comp_fc
->type
) {
992 case CTF_FIELD_CLASS_TYPE_STRUCT
:
994 struct ctf_named_field_class
*named_fc
=
995 ctf_field_class_struct_borrow_member_by_index(
996 (void *) comp_fc
, index
);
1002 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1004 struct ctf_named_field_class
*named_fc
=
1005 ctf_field_class_variant_borrow_option_by_index(
1006 (void *) comp_fc
, index
);
1008 BT_ASSERT(named_fc
);
1012 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1013 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1015 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1017 fc
= array_fc
->elem_fc
;
1028 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1030 uint64_t field_count
;
1033 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1035 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1037 field_count
= struct_fc
->members
->len
;
1040 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1042 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1044 field_count
= var_fc
->options
->len
;
1047 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1048 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1050 * Array and sequence types always contain a single
1051 * member (the element type).
1063 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1064 struct ctf_field_class
*fc
, const char *orig_name
)
1066 int64_t ret_index
= -1;
1070 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1072 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1074 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1075 struct ctf_named_field_class
*named_fc
=
1076 ctf_field_class_struct_borrow_member_by_index(
1079 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1080 ret_index
= (int64_t) i
;
1087 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1089 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1091 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1092 struct ctf_named_field_class
*named_fc
=
1093 ctf_field_class_variant_borrow_option_by_index(
1096 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1097 ret_index
= (int64_t) i
;
1113 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1116 g_array_append_val(fp
->path
, index
);
1120 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1124 BT_ASSERT(index
< fp
->path
->len
);
1125 return g_array_index(fp
->path
, int64_t, index
);
1129 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1132 g_array_set_size(fp
->path
, 0);
1136 const char *ctf_scope_string(enum ctf_scope scope
)
1139 case CTF_SCOPE_PACKET_HEADER
:
1140 return "PACKET_HEADER";
1141 case CTF_SCOPE_PACKET_CONTEXT
:
1142 return "PACKET_CONTEXT";
1143 case CTF_SCOPE_EVENT_HEADER
:
1144 return "EVENT_HEADER";
1145 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1146 return "EVENT_COMMON_CONTEXT";
1147 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1148 return "EVENT_SPECIFIC_CONTEXT";
1149 case CTF_SCOPE_EVENT_PAYLOAD
:
1150 return "EVENT_PAYLOAD";
1157 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1159 GString
*str
= g_string_new(NULL
);
1168 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1170 for (i
= 0; i
< path
->path
->len
; i
++) {
1171 g_string_append_printf(str
, ", %" PRId64
,
1172 ctf_field_path_borrow_index_by_index(path
, i
));
1175 g_string_append(str
, "]");
1182 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1183 struct ctf_field_path
*field_path
,
1184 struct ctf_trace_class
*tc
,
1185 struct ctf_stream_class
*sc
,
1186 struct ctf_event_class
*ec
)
1189 struct ctf_field_class
*fc
;
1191 switch (field_path
->root
) {
1192 case CTF_SCOPE_PACKET_HEADER
:
1193 fc
= tc
->packet_header_fc
;
1195 case CTF_SCOPE_PACKET_CONTEXT
:
1196 fc
= sc
->packet_context_fc
;
1198 case CTF_SCOPE_EVENT_HEADER
:
1199 fc
= sc
->event_header_fc
;
1201 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1202 fc
= sc
->event_common_context_fc
;
1204 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1205 fc
= ec
->spec_context_fc
;
1207 case CTF_SCOPE_EVENT_PAYLOAD
:
1208 fc
= ec
->payload_fc
;
1216 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1217 int64_t child_index
=
1218 ctf_field_path_borrow_index_by_index(field_path
, i
);
1219 struct ctf_field_class
*child_fc
=
1220 ctf_field_class_compound_borrow_field_class_by_index(
1222 BT_ASSERT(child_fc
);
1231 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1234 void ctf_field_class_bit_array_copy_content(
1235 struct ctf_field_class_bit_array
*dst_fc
,
1236 struct ctf_field_class_bit_array
*src_fc
)
1240 dst_fc
->byte_order
= src_fc
->byte_order
;
1241 dst_fc
->size
= src_fc
->size
;
1245 void ctf_field_class_int_copy_content(
1246 struct ctf_field_class_int
*dst_fc
,
1247 struct ctf_field_class_int
*src_fc
)
1249 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1250 dst_fc
->meaning
= src_fc
->meaning
;
1251 dst_fc
->is_signed
= src_fc
->is_signed
;
1252 dst_fc
->disp_base
= src_fc
->disp_base
;
1253 dst_fc
->encoding
= src_fc
->encoding
;
1254 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1255 dst_fc
->storing_index
= src_fc
->storing_index
;
1259 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1260 struct ctf_field_class_int
*fc
)
1262 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1265 ctf_field_class_int_copy_content(copy_fc
, fc
);
1270 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1271 struct ctf_field_class_enum
*fc
)
1273 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1277 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1279 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1282 struct ctf_field_class_enum_mapping
*mapping
=
1283 &g_array_index(fc
->mappings
,
1284 struct ctf_field_class_enum_mapping
, i
);
1286 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1287 struct ctf_range
*range
=
1288 &g_array_index(mapping
->ranges
,
1289 struct ctf_range
, range_i
);
1291 ctf_field_class_enum_map_range(copy_fc
,
1292 mapping
->label
->str
, range
->lower
.u
,
1301 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1302 struct ctf_field_class_float
*fc
)
1304 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1307 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1312 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1313 struct ctf_field_class_string
*fc
)
1315 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1322 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1323 struct ctf_field_class_struct
*fc
)
1325 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1330 for (i
= 0; i
< fc
->members
->len
; i
++) {
1331 struct ctf_named_field_class
*named_fc
=
1332 &g_array_index(fc
->members
,
1333 struct ctf_named_field_class
, i
);
1335 ctf_field_class_struct_append_member(copy_fc
,
1336 named_fc
->name
->str
,
1337 ctf_field_class_copy(named_fc
->fc
));
1344 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1345 struct ctf_field_path
*src_fp
)
1351 dst_fp
->root
= src_fp
->root
;
1352 ctf_field_path_clear(dst_fp
);
1354 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1355 int64_t index
= ctf_field_path_borrow_index_by_index(
1358 ctf_field_path_append_index(dst_fp
, index
);
1363 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1364 struct ctf_field_class_variant
*fc
)
1366 struct ctf_field_class_variant
*copy_fc
=
1367 ctf_field_class_variant_create();
1372 for (i
= 0; i
< fc
->options
->len
; i
++) {
1373 struct ctf_named_field_class
*named_fc
=
1374 &g_array_index(fc
->options
,
1375 struct ctf_named_field_class
, i
);
1377 ctf_field_class_variant_append_option(copy_fc
,
1378 named_fc
->name
->str
,
1379 ctf_field_class_copy(named_fc
->fc
));
1382 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1383 struct ctf_field_class_variant_range
*range
=
1384 &g_array_index(fc
->ranges
,
1385 struct ctf_field_class_variant_range
, i
);
1387 g_array_append_val(copy_fc
->ranges
, *range
);
1390 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1391 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1392 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1397 void ctf_field_class_array_base_copy_content(
1398 struct ctf_field_class_array_base
*dst_fc
,
1399 struct ctf_field_class_array_base
*src_fc
)
1403 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1404 dst_fc
->is_text
= src_fc
->is_text
;
1408 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1409 struct ctf_field_class_array
*fc
)
1411 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1414 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1415 copy_fc
->length
= fc
->length
;
1420 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1421 struct ctf_field_class_sequence
*fc
)
1423 struct ctf_field_class_sequence
*copy_fc
=
1424 ctf_field_class_sequence_create();
1427 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1428 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1429 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1430 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1435 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1437 struct ctf_field_class
*copy_fc
= NULL
;
1444 * Translation should not have happened yet.
1446 BT_ASSERT(!fc
->ir_fc
);
1449 case CTF_FIELD_CLASS_TYPE_INT
:
1450 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1452 case CTF_FIELD_CLASS_TYPE_ENUM
:
1453 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1455 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1456 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1458 case CTF_FIELD_CLASS_TYPE_STRING
:
1459 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1461 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1462 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1464 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1465 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1467 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1468 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1470 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1471 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1477 copy_fc
->type
= fc
->type
;
1478 copy_fc
->alignment
= fc
->alignment
;
1479 copy_fc
->in_ir
= fc
->in_ir
;
1486 struct ctf_event_class
*ctf_event_class_create(void)
1488 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1491 ec
->name
= g_string_new(NULL
);
1492 BT_ASSERT(ec
->name
);
1493 ec
->emf_uri
= g_string_new(NULL
);
1494 BT_ASSERT(ec
->emf_uri
);
1495 ec
->is_log_level_set
= false;
1500 void ctf_event_class_set_log_level(struct ctf_event_class
*ec
,
1501 enum bt_event_class_log_level log_level
)
1504 ec
->log_level
= log_level
;
1505 ec
->is_log_level_set
= true;
1509 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1516 g_string_free(ec
->name
, TRUE
);
1520 g_string_free(ec
->emf_uri
, TRUE
);
1523 ctf_field_class_destroy(ec
->spec_context_fc
);
1524 ctf_field_class_destroy(ec
->payload_fc
);
1529 struct ctf_stream_class
*ctf_stream_class_create(void)
1531 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1534 sc
->event_classes
= g_ptr_array_new_with_free_func(
1535 (GDestroyNotify
) ctf_event_class_destroy
);
1536 BT_ASSERT(sc
->event_classes
);
1537 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1539 BT_ASSERT(sc
->event_classes_by_id
);
1544 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1550 if (sc
->event_classes
) {
1551 g_ptr_array_free(sc
->event_classes
, TRUE
);
1554 if (sc
->event_classes_by_id
) {
1555 g_hash_table_destroy(sc
->event_classes_by_id
);
1558 ctf_field_class_destroy(sc
->packet_context_fc
);
1559 ctf_field_class_destroy(sc
->event_header_fc
);
1560 ctf_field_class_destroy(sc
->event_common_context_fc
);
1565 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1566 struct ctf_event_class
*ec
)
1568 g_ptr_array_add(sc
->event_classes
, ec
);
1569 g_hash_table_insert(sc
->event_classes_by_id
,
1570 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1574 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1575 struct ctf_stream_class
*sc
, uint64_t type
)
1578 return g_hash_table_lookup(sc
->event_classes_by_id
,
1579 GUINT_TO_POINTER((guint
) type
));
1583 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1586 entry
->name
= g_string_new(NULL
);
1587 BT_ASSERT(entry
->name
);
1588 entry
->value
.str
= g_string_new(NULL
);
1589 BT_ASSERT(entry
->value
.str
);
1593 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1598 g_string_free(entry
->name
, TRUE
);
1601 if (entry
->value
.str
) {
1602 g_string_free(entry
->value
.str
, TRUE
);
1607 struct ctf_clock_class
*ctf_clock_class_create(void)
1609 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1612 cc
->name
= g_string_new(NULL
);
1613 BT_ASSERT(cc
->name
);
1614 cc
->description
= g_string_new(NULL
);
1615 BT_ASSERT(cc
->description
);
1620 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1627 g_string_free(cc
->name
, TRUE
);
1630 if (cc
->description
) {
1631 g_string_free(cc
->description
, TRUE
);
1634 bt_clock_class_put_ref(cc
->ir_cc
);
1639 struct ctf_trace_class
*ctf_trace_class_create(void)
1641 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1644 tc
->default_byte_order
= CTF_BYTE_ORDER_UNKNOWN
;
1645 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1646 (GDestroyNotify
) ctf_clock_class_destroy
);
1647 BT_ASSERT(tc
->clock_classes
);
1648 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1649 (GDestroyNotify
) ctf_stream_class_destroy
);
1650 BT_ASSERT(tc
->stream_classes
);
1651 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1652 sizeof(struct ctf_trace_class_env_entry
));
1657 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1663 ctf_field_class_destroy(tc
->packet_header_fc
);
1665 if (tc
->clock_classes
) {
1666 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1669 if (tc
->stream_classes
) {
1670 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1673 if (tc
->env_entries
) {
1676 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1677 struct ctf_trace_class_env_entry
*entry
=
1678 &g_array_index(tc
->env_entries
,
1679 struct ctf_trace_class_env_entry
, i
);
1681 _ctf_trace_class_env_entry_fini(entry
);
1684 g_array_free(tc
->env_entries
, TRUE
);
1691 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1692 const char *name
, enum ctf_trace_class_env_entry_type type
,
1693 const char *str_value
, int64_t i_value
)
1695 struct ctf_trace_class_env_entry
*entry
;
1699 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1701 entry
= &g_array_index(tc
->env_entries
,
1702 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1704 _ctf_trace_class_env_entry_init(entry
);
1705 g_string_assign(entry
->name
, name
);
1708 g_string_assign(entry
->value
.str
, str_value
);
1711 entry
->value
.i
= i_value
;
1715 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1716 struct ctf_trace_class
*tc
, uint64_t id
)
1719 struct ctf_stream_class
*ret_sc
= NULL
;
1723 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1724 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1737 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1738 struct ctf_trace_class
*tc
, const char *name
)
1741 struct ctf_clock_class
*ret_cc
= NULL
;
1746 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1747 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1749 BT_ASSERT(cc
->name
);
1750 if (strcmp(cc
->name
->str
, name
) == 0) {
1761 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1762 struct ctf_trace_class
*tc
, uint64_t index
)
1765 BT_ASSERT(index
< tc
->env_entries
->len
);
1766 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1771 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1772 struct ctf_trace_class
*tc
, const char *name
)
1774 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1780 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1781 struct ctf_trace_class_env_entry
*env_entry
=
1782 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1784 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1785 ret_entry
= env_entry
;
1794 #endif /* _CTF_META_H */