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
;
316 bool lttng_event_after_packet
;
317 bool barectf_event_before_packet
;
322 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
325 void _ctf_field_class_init(struct ctf_field_class
*fc
,
326 enum ctf_field_class_type type
, unsigned int alignment
)
330 fc
->alignment
= alignment
;
335 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
336 enum ctf_field_class_type type
)
338 _ctf_field_class_init((void *) fc
, type
, 1);
342 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
343 enum ctf_field_class_type type
)
345 _ctf_field_class_bit_array_init((void *) fc
, type
);
346 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
347 fc
->storing_index
= -1;
351 void ctf_field_path_init(struct ctf_field_path
*field_path
)
353 BT_ASSERT(field_path
);
354 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
355 BT_ASSERT(field_path
->path
);
359 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
361 BT_ASSERT(field_path
);
363 if (field_path
->path
) {
364 g_array_free(field_path
->path
, TRUE
);
369 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
372 named_fc
->name
= g_string_new(NULL
);
373 BT_ASSERT(named_fc
->name
);
374 named_fc
->orig_name
= g_string_new(NULL
);
375 BT_ASSERT(named_fc
->orig_name
);
379 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
383 if (named_fc
->name
) {
384 g_string_free(named_fc
->name
, TRUE
);
387 if (named_fc
->orig_name
) {
388 g_string_free(named_fc
->orig_name
, TRUE
);
391 ctf_field_class_destroy(named_fc
->fc
);
395 void _ctf_field_class_enum_mapping_init(
396 struct ctf_field_class_enum_mapping
*mapping
)
399 mapping
->label
= g_string_new(NULL
);
400 BT_ASSERT(mapping
->label
);
401 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
402 BT_ASSERT(mapping
->ranges
);
406 void _ctf_field_class_enum_mapping_fini(
407 struct ctf_field_class_enum_mapping
*mapping
)
411 if (mapping
->label
) {
412 g_string_free(mapping
->label
, TRUE
);
415 if (mapping
->ranges
) {
416 g_array_free(mapping
->ranges
, TRUE
);
421 struct ctf_field_class_int
*ctf_field_class_int_create(void)
423 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
426 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
431 struct ctf_field_class_float
*ctf_field_class_float_create(void)
433 struct ctf_field_class_float
*fc
=
434 g_new0(struct ctf_field_class_float
, 1);
437 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
442 struct ctf_field_class_string
*ctf_field_class_string_create(void)
444 struct ctf_field_class_string
*fc
=
445 g_new0(struct ctf_field_class_string
, 1);
448 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
453 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
455 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
458 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
459 fc
->mappings
= g_array_new(FALSE
, TRUE
,
460 sizeof(struct ctf_field_class_enum_mapping
));
461 BT_ASSERT(fc
->mappings
);
466 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
468 struct ctf_field_class_struct
*fc
=
469 g_new0(struct ctf_field_class_struct
, 1);
472 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
473 fc
->members
= g_array_new(FALSE
, TRUE
,
474 sizeof(struct ctf_named_field_class
));
475 BT_ASSERT(fc
->members
);
476 fc
->base
.is_compound
= true;
481 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
483 struct ctf_field_class_variant
*fc
=
484 g_new0(struct ctf_field_class_variant
, 1);
487 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
488 fc
->options
= g_array_new(FALSE
, TRUE
,
489 sizeof(struct ctf_named_field_class
));
490 BT_ASSERT(fc
->options
);
491 fc
->ranges
= g_array_new(FALSE
, TRUE
,
492 sizeof(struct ctf_field_class_variant_range
));
493 BT_ASSERT(fc
->ranges
);
494 fc
->tag_ref
= g_string_new(NULL
);
495 BT_ASSERT(fc
->tag_ref
);
496 ctf_field_path_init(&fc
->tag_path
);
497 fc
->base
.is_compound
= true;
502 struct ctf_field_class_array
*ctf_field_class_array_create(void)
504 struct ctf_field_class_array
*fc
=
505 g_new0(struct ctf_field_class_array
, 1);
508 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
509 fc
->base
.base
.is_compound
= true;
514 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
516 struct ctf_field_class_sequence
*fc
=
517 g_new0(struct ctf_field_class_sequence
, 1);
520 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
521 fc
->length_ref
= g_string_new(NULL
);
522 BT_ASSERT(fc
->length_ref
);
523 ctf_field_path_init(&fc
->length_path
);
524 fc
->base
.base
.is_compound
= true;
529 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
536 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
543 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
544 struct ctf_field_class_enum_mapping
*mapping
=
545 &g_array_index(fc
->mappings
,
546 struct ctf_field_class_enum_mapping
, i
);
548 _ctf_field_class_enum_mapping_fini(mapping
);
551 g_array_free(fc
->mappings
, TRUE
);
558 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
565 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
572 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
579 for (i
= 0; i
< fc
->members
->len
; i
++) {
580 struct ctf_named_field_class
*named_fc
=
581 &g_array_index(fc
->members
,
582 struct ctf_named_field_class
, i
);
584 _ctf_named_field_class_fini(named_fc
);
587 g_array_free(fc
->members
, TRUE
);
594 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
597 ctf_field_class_destroy(fc
->elem_fc
);
601 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
604 _ctf_field_class_array_base_fini((void *) fc
);
609 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
612 _ctf_field_class_array_base_fini((void *) fc
);
614 if (fc
->length_ref
) {
615 g_string_free(fc
->length_ref
, TRUE
);
618 ctf_field_path_fini(&fc
->length_path
);
623 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
630 for (i
= 0; i
< fc
->options
->len
; i
++) {
631 struct ctf_named_field_class
*named_fc
=
632 &g_array_index(fc
->options
,
633 struct ctf_named_field_class
, i
);
635 _ctf_named_field_class_fini(named_fc
);
638 g_array_free(fc
->options
, TRUE
);
642 g_array_free(fc
->ranges
, TRUE
);
646 g_string_free(fc
->tag_ref
, TRUE
);
649 ctf_field_path_fini(&fc
->tag_path
);
654 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
661 case CTF_FIELD_CLASS_TYPE_INT
:
662 _ctf_field_class_int_destroy((void *) fc
);
664 case CTF_FIELD_CLASS_TYPE_ENUM
:
665 _ctf_field_class_enum_destroy((void *) fc
);
667 case CTF_FIELD_CLASS_TYPE_FLOAT
:
668 _ctf_field_class_float_destroy((void *) fc
);
670 case CTF_FIELD_CLASS_TYPE_STRING
:
671 _ctf_field_class_string_destroy((void *) fc
);
673 case CTF_FIELD_CLASS_TYPE_STRUCT
:
674 _ctf_field_class_struct_destroy((void *) fc
);
676 case CTF_FIELD_CLASS_TYPE_ARRAY
:
677 _ctf_field_class_array_destroy((void *) fc
);
679 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
680 _ctf_field_class_sequence_destroy((void *) fc
);
682 case CTF_FIELD_CLASS_TYPE_VARIANT
:
683 _ctf_field_class_variant_destroy((void *) fc
);
691 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
692 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
695 BT_ASSERT(index
< mapping
->ranges
->len
);
696 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
700 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
701 struct ctf_field_class_enum
*fc
, uint64_t index
)
704 BT_ASSERT(index
< fc
->mappings
->len
);
705 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
710 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
711 struct ctf_field_class_enum
*fc
, const char *label
)
713 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
719 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
720 struct ctf_field_class_enum_mapping
*mapping
=
721 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
723 if (strcmp(mapping
->label
->str
, label
) == 0) {
724 ret_mapping
= mapping
;
734 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
735 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
737 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
738 struct ctf_range range
= {
747 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
748 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
751 if (strcmp(mapping
->label
->str
, label
) == 0) {
756 if (i
== fc
->mappings
->len
) {
761 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
762 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
763 fc
, fc
->mappings
->len
- 1);
764 _ctf_field_class_enum_mapping_init(mapping
);
765 g_string_assign(mapping
->label
, label
);
768 g_array_append_val(mapping
->ranges
, range
);
772 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
773 struct ctf_field_class_struct
*fc
, uint64_t index
)
776 BT_ASSERT(index
< fc
->members
->len
);
777 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
782 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
783 struct ctf_field_class_struct
*fc
, const char *name
)
786 struct ctf_named_field_class
*ret_named_fc
= NULL
;
791 for (i
= 0; i
< fc
->members
->len
; i
++) {
792 struct ctf_named_field_class
*named_fc
=
793 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
795 if (strcmp(name
, named_fc
->name
->str
) == 0) {
796 ret_named_fc
= named_fc
;
806 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
807 struct ctf_field_class_struct
*struct_fc
, const char *name
)
809 struct ctf_named_field_class
*named_fc
= NULL
;
810 struct ctf_field_class
*fc
= NULL
;
816 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
828 struct ctf_field_class_int
*
829 ctf_field_class_struct_borrow_member_int_field_class_by_name(
830 struct ctf_field_class_struct
*struct_fc
, const char *name
)
832 struct ctf_field_class_int
*int_fc
= NULL
;
835 ctf_field_class_struct_borrow_member_field_class_by_name(
841 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
842 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
852 void _ctf_named_field_class_unescape_orig_name(
853 struct ctf_named_field_class
*named_fc
)
855 const char *name
= named_fc
->orig_name
->str
;
857 if (name
[0] == '_') {
861 g_string_assign(named_fc
->name
, name
);
865 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
866 const char *orig_name
, struct ctf_field_class
*member_fc
)
868 struct ctf_named_field_class
*named_fc
;
871 BT_ASSERT(orig_name
);
872 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
874 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
875 fc
->members
->len
- 1);
876 _ctf_named_field_class_init(named_fc
);
877 g_string_assign(named_fc
->orig_name
, orig_name
);
878 _ctf_named_field_class_unescape_orig_name(named_fc
);
879 named_fc
->fc
= member_fc
;
881 if (member_fc
->alignment
> fc
->base
.alignment
) {
882 fc
->base
.alignment
= member_fc
->alignment
;
887 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
888 struct ctf_field_class_variant
*fc
, uint64_t index
)
891 BT_ASSERT(index
< fc
->options
->len
);
892 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
897 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
898 struct ctf_field_class_variant
*fc
, const char *name
)
901 struct ctf_named_field_class
*ret_named_fc
= NULL
;
906 for (i
= 0; i
< fc
->options
->len
; i
++) {
907 struct ctf_named_field_class
*named_fc
=
908 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
910 if (strcmp(name
, named_fc
->name
->str
) == 0) {
911 ret_named_fc
= named_fc
;
921 struct ctf_field_class_variant_range
*
922 ctf_field_class_variant_borrow_range_by_index(
923 struct ctf_field_class_variant
*fc
, uint64_t index
)
926 BT_ASSERT(index
< fc
->ranges
->len
);
927 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
932 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
933 const char *orig_name
, struct ctf_field_class
*option_fc
)
935 struct ctf_named_field_class
*named_fc
;
938 BT_ASSERT(orig_name
);
939 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
941 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
942 fc
->options
->len
- 1);
943 _ctf_named_field_class_init(named_fc
);
944 g_string_assign(named_fc
->orig_name
, orig_name
);
945 _ctf_named_field_class_unescape_orig_name(named_fc
);
946 named_fc
->fc
= option_fc
;
950 void ctf_field_class_variant_set_tag_field_class(
951 struct ctf_field_class_variant
*fc
,
952 struct ctf_field_class_enum
*tag_fc
)
960 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
962 struct ctf_named_field_class
*named_fc
=
963 ctf_field_class_variant_borrow_option_by_index(
965 struct ctf_field_class_enum_mapping
*mapping
;
967 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
968 tag_fc
, named_fc
->orig_name
->str
);
973 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
975 struct ctf_range
*range
=
976 ctf_field_class_enum_mapping_borrow_range_by_index(
978 struct ctf_field_class_variant_range var_range
;
980 var_range
.range
= *range
;
981 var_range
.option_index
= option_i
;
982 g_array_append_val(fc
->ranges
, var_range
);
988 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
989 struct ctf_field_class
*comp_fc
, uint64_t index
)
991 struct ctf_field_class
*fc
= NULL
;
993 switch (comp_fc
->type
) {
994 case CTF_FIELD_CLASS_TYPE_STRUCT
:
996 struct ctf_named_field_class
*named_fc
=
997 ctf_field_class_struct_borrow_member_by_index(
998 (void *) comp_fc
, index
);
1000 BT_ASSERT(named_fc
);
1004 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1006 struct ctf_named_field_class
*named_fc
=
1007 ctf_field_class_variant_borrow_option_by_index(
1008 (void *) comp_fc
, index
);
1010 BT_ASSERT(named_fc
);
1014 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1015 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1017 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1019 fc
= array_fc
->elem_fc
;
1030 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1032 uint64_t field_count
;
1035 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1037 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1039 field_count
= struct_fc
->members
->len
;
1042 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1044 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1046 field_count
= var_fc
->options
->len
;
1049 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1050 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1052 * Array and sequence types always contain a single
1053 * member (the element type).
1065 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1066 struct ctf_field_class
*fc
, const char *orig_name
)
1068 int64_t ret_index
= -1;
1072 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1074 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1076 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1077 struct ctf_named_field_class
*named_fc
=
1078 ctf_field_class_struct_borrow_member_by_index(
1081 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1082 ret_index
= (int64_t) i
;
1089 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1091 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1093 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1094 struct ctf_named_field_class
*named_fc
=
1095 ctf_field_class_variant_borrow_option_by_index(
1098 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1099 ret_index
= (int64_t) i
;
1115 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1118 g_array_append_val(fp
->path
, index
);
1122 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1126 BT_ASSERT(index
< fp
->path
->len
);
1127 return g_array_index(fp
->path
, int64_t, index
);
1131 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1134 g_array_set_size(fp
->path
, 0);
1138 const char *ctf_scope_string(enum ctf_scope scope
)
1141 case CTF_SCOPE_PACKET_HEADER
:
1142 return "PACKET_HEADER";
1143 case CTF_SCOPE_PACKET_CONTEXT
:
1144 return "PACKET_CONTEXT";
1145 case CTF_SCOPE_EVENT_HEADER
:
1146 return "EVENT_HEADER";
1147 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1148 return "EVENT_COMMON_CONTEXT";
1149 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1150 return "EVENT_SPECIFIC_CONTEXT";
1151 case CTF_SCOPE_EVENT_PAYLOAD
:
1152 return "EVENT_PAYLOAD";
1159 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1161 GString
*str
= g_string_new(NULL
);
1170 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1172 for (i
= 0; i
< path
->path
->len
; i
++) {
1173 g_string_append_printf(str
, ", %" PRId64
,
1174 ctf_field_path_borrow_index_by_index(path
, i
));
1177 g_string_append(str
, "]");
1184 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1185 struct ctf_field_path
*field_path
,
1186 struct ctf_trace_class
*tc
,
1187 struct ctf_stream_class
*sc
,
1188 struct ctf_event_class
*ec
)
1191 struct ctf_field_class
*fc
;
1193 switch (field_path
->root
) {
1194 case CTF_SCOPE_PACKET_HEADER
:
1195 fc
= tc
->packet_header_fc
;
1197 case CTF_SCOPE_PACKET_CONTEXT
:
1198 fc
= sc
->packet_context_fc
;
1200 case CTF_SCOPE_EVENT_HEADER
:
1201 fc
= sc
->event_header_fc
;
1203 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1204 fc
= sc
->event_common_context_fc
;
1206 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1207 fc
= ec
->spec_context_fc
;
1209 case CTF_SCOPE_EVENT_PAYLOAD
:
1210 fc
= ec
->payload_fc
;
1218 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1219 int64_t child_index
=
1220 ctf_field_path_borrow_index_by_index(field_path
, i
);
1221 struct ctf_field_class
*child_fc
=
1222 ctf_field_class_compound_borrow_field_class_by_index(
1224 BT_ASSERT(child_fc
);
1233 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1236 void ctf_field_class_bit_array_copy_content(
1237 struct ctf_field_class_bit_array
*dst_fc
,
1238 struct ctf_field_class_bit_array
*src_fc
)
1242 dst_fc
->byte_order
= src_fc
->byte_order
;
1243 dst_fc
->size
= src_fc
->size
;
1247 void ctf_field_class_int_copy_content(
1248 struct ctf_field_class_int
*dst_fc
,
1249 struct ctf_field_class_int
*src_fc
)
1251 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1252 dst_fc
->meaning
= src_fc
->meaning
;
1253 dst_fc
->is_signed
= src_fc
->is_signed
;
1254 dst_fc
->disp_base
= src_fc
->disp_base
;
1255 dst_fc
->encoding
= src_fc
->encoding
;
1256 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1257 dst_fc
->storing_index
= src_fc
->storing_index
;
1261 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1262 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
);
1272 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1273 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((void *) copy_fc
, (void *) fc
);
1281 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1284 struct ctf_field_class_enum_mapping
*mapping
=
1285 &g_array_index(fc
->mappings
,
1286 struct ctf_field_class_enum_mapping
, i
);
1288 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1289 struct ctf_range
*range
=
1290 &g_array_index(mapping
->ranges
,
1291 struct ctf_range
, range_i
);
1293 ctf_field_class_enum_map_range(copy_fc
,
1294 mapping
->label
->str
, range
->lower
.u
,
1303 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1304 struct ctf_field_class_float
*fc
)
1306 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1309 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1314 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1315 struct ctf_field_class_string
*fc
)
1317 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1324 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1325 struct ctf_field_class_struct
*fc
)
1327 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1332 for (i
= 0; i
< fc
->members
->len
; i
++) {
1333 struct ctf_named_field_class
*named_fc
=
1334 &g_array_index(fc
->members
,
1335 struct ctf_named_field_class
, i
);
1337 ctf_field_class_struct_append_member(copy_fc
,
1338 named_fc
->name
->str
,
1339 ctf_field_class_copy(named_fc
->fc
));
1346 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1347 struct ctf_field_path
*src_fp
)
1353 dst_fp
->root
= src_fp
->root
;
1354 ctf_field_path_clear(dst_fp
);
1356 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1357 int64_t index
= ctf_field_path_borrow_index_by_index(
1360 ctf_field_path_append_index(dst_fp
, index
);
1365 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1366 struct ctf_field_class_variant
*fc
)
1368 struct ctf_field_class_variant
*copy_fc
=
1369 ctf_field_class_variant_create();
1374 for (i
= 0; i
< fc
->options
->len
; i
++) {
1375 struct ctf_named_field_class
*named_fc
=
1376 &g_array_index(fc
->options
,
1377 struct ctf_named_field_class
, i
);
1379 ctf_field_class_variant_append_option(copy_fc
,
1380 named_fc
->name
->str
,
1381 ctf_field_class_copy(named_fc
->fc
));
1384 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1385 struct ctf_field_class_variant_range
*range
=
1386 &g_array_index(fc
->ranges
,
1387 struct ctf_field_class_variant_range
, i
);
1389 g_array_append_val(copy_fc
->ranges
, *range
);
1392 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1393 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1394 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1399 void ctf_field_class_array_base_copy_content(
1400 struct ctf_field_class_array_base
*dst_fc
,
1401 struct ctf_field_class_array_base
*src_fc
)
1405 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1406 dst_fc
->is_text
= src_fc
->is_text
;
1410 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1411 struct ctf_field_class_array
*fc
)
1413 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1416 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1417 copy_fc
->length
= fc
->length
;
1422 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1423 struct ctf_field_class_sequence
*fc
)
1425 struct ctf_field_class_sequence
*copy_fc
=
1426 ctf_field_class_sequence_create();
1429 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1430 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1431 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1432 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1437 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1439 struct ctf_field_class
*copy_fc
= NULL
;
1446 * Translation should not have happened yet.
1448 BT_ASSERT(!fc
->ir_fc
);
1451 case CTF_FIELD_CLASS_TYPE_INT
:
1452 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1454 case CTF_FIELD_CLASS_TYPE_ENUM
:
1455 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1457 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1458 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1460 case CTF_FIELD_CLASS_TYPE_STRING
:
1461 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1463 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1464 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1466 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1467 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1469 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1470 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1472 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1473 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1479 copy_fc
->type
= fc
->type
;
1480 copy_fc
->alignment
= fc
->alignment
;
1481 copy_fc
->in_ir
= fc
->in_ir
;
1488 struct ctf_event_class
*ctf_event_class_create(void)
1490 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1493 ec
->name
= g_string_new(NULL
);
1494 BT_ASSERT(ec
->name
);
1495 ec
->emf_uri
= g_string_new(NULL
);
1496 BT_ASSERT(ec
->emf_uri
);
1497 ec
->is_log_level_set
= false;
1502 void ctf_event_class_set_log_level(struct ctf_event_class
*ec
,
1503 enum bt_event_class_log_level log_level
)
1506 ec
->log_level
= log_level
;
1507 ec
->is_log_level_set
= true;
1511 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1518 g_string_free(ec
->name
, TRUE
);
1522 g_string_free(ec
->emf_uri
, TRUE
);
1525 ctf_field_class_destroy(ec
->spec_context_fc
);
1526 ctf_field_class_destroy(ec
->payload_fc
);
1531 struct ctf_stream_class
*ctf_stream_class_create(void)
1533 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1536 sc
->event_classes
= g_ptr_array_new_with_free_func(
1537 (GDestroyNotify
) ctf_event_class_destroy
);
1538 BT_ASSERT(sc
->event_classes
);
1539 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1541 BT_ASSERT(sc
->event_classes_by_id
);
1546 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1552 if (sc
->event_classes
) {
1553 g_ptr_array_free(sc
->event_classes
, TRUE
);
1556 if (sc
->event_classes_by_id
) {
1557 g_hash_table_destroy(sc
->event_classes_by_id
);
1560 ctf_field_class_destroy(sc
->packet_context_fc
);
1561 ctf_field_class_destroy(sc
->event_header_fc
);
1562 ctf_field_class_destroy(sc
->event_common_context_fc
);
1567 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1568 struct ctf_event_class
*ec
)
1570 g_ptr_array_add(sc
->event_classes
, ec
);
1571 g_hash_table_insert(sc
->event_classes_by_id
,
1572 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1576 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1577 struct ctf_stream_class
*sc
, uint64_t type
)
1580 return g_hash_table_lookup(sc
->event_classes_by_id
,
1581 GUINT_TO_POINTER((guint
) type
));
1585 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1588 entry
->name
= g_string_new(NULL
);
1589 BT_ASSERT(entry
->name
);
1590 entry
->value
.str
= g_string_new(NULL
);
1591 BT_ASSERT(entry
->value
.str
);
1595 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1600 g_string_free(entry
->name
, TRUE
);
1603 if (entry
->value
.str
) {
1604 g_string_free(entry
->value
.str
, TRUE
);
1609 struct ctf_clock_class
*ctf_clock_class_create(void)
1611 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1614 cc
->name
= g_string_new(NULL
);
1615 BT_ASSERT(cc
->name
);
1616 cc
->description
= g_string_new(NULL
);
1617 BT_ASSERT(cc
->description
);
1622 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1629 g_string_free(cc
->name
, TRUE
);
1632 if (cc
->description
) {
1633 g_string_free(cc
->description
, TRUE
);
1636 bt_clock_class_put_ref(cc
->ir_cc
);
1641 struct ctf_trace_class
*ctf_trace_class_create(void)
1643 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1646 tc
->default_byte_order
= CTF_BYTE_ORDER_UNKNOWN
;
1647 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1648 (GDestroyNotify
) ctf_clock_class_destroy
);
1649 BT_ASSERT(tc
->clock_classes
);
1650 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1651 (GDestroyNotify
) ctf_stream_class_destroy
);
1652 BT_ASSERT(tc
->stream_classes
);
1653 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1654 sizeof(struct ctf_trace_class_env_entry
));
1659 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1665 ctf_field_class_destroy(tc
->packet_header_fc
);
1667 if (tc
->clock_classes
) {
1668 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1671 if (tc
->stream_classes
) {
1672 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1675 if (tc
->env_entries
) {
1678 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1679 struct ctf_trace_class_env_entry
*entry
=
1680 &g_array_index(tc
->env_entries
,
1681 struct ctf_trace_class_env_entry
, i
);
1683 _ctf_trace_class_env_entry_fini(entry
);
1686 g_array_free(tc
->env_entries
, TRUE
);
1693 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1694 const char *name
, enum ctf_trace_class_env_entry_type type
,
1695 const char *str_value
, int64_t i_value
)
1697 struct ctf_trace_class_env_entry
*entry
;
1701 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1703 entry
= &g_array_index(tc
->env_entries
,
1704 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1706 _ctf_trace_class_env_entry_init(entry
);
1707 g_string_assign(entry
->name
, name
);
1710 g_string_assign(entry
->value
.str
, str_value
);
1713 entry
->value
.i
= i_value
;
1717 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1718 struct ctf_trace_class
*tc
, uint64_t id
)
1721 struct ctf_stream_class
*ret_sc
= NULL
;
1725 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1726 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1739 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1740 struct ctf_trace_class
*tc
, const char *name
)
1743 struct ctf_clock_class
*ret_cc
= NULL
;
1748 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1749 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1751 BT_ASSERT(cc
->name
);
1752 if (strcmp(cc
->name
->str
, name
) == 0) {
1763 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1764 struct ctf_trace_class
*tc
, uint64_t index
)
1767 BT_ASSERT(index
< tc
->env_entries
->len
);
1768 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1773 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1774 struct ctf_trace_class
*tc
, const char *name
)
1776 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1782 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1783 struct ctf_trace_class_env_entry
*env_entry
=
1784 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1786 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1787 ret_entry
= env_entry
;
1796 #endif /* _CTF_META_H */