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_DEFAULT
,
54 CTF_BYTE_ORDER_LITTLE
,
64 CTF_SCOPE_PACKET_HEADER
,
65 CTF_SCOPE_PACKET_CONTEXT
,
66 CTF_SCOPE_EVENT_HEADER
,
67 CTF_SCOPE_EVENT_COMMON_CONTEXT
,
68 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
,
69 CTF_SCOPE_EVENT_PAYLOAD
,
72 struct ctf_clock_class
{
77 int64_t offset_seconds
;
78 uint64_t offset_cycles
;
83 /* Weak, set during translation */
84 bt_clock_class
*ir_cc
;
87 struct ctf_field_class
{
88 enum ctf_field_class_type type
;
89 unsigned int alignment
;
93 /* Weak, set during translation. NULL if `in_ir` is false below. */
94 bt_field_class
*ir_fc
;
97 struct ctf_field_class_bit_array
{
98 struct ctf_field_class base
;
99 enum ctf_byte_order byte_order
;
103 struct ctf_field_class_int
{
104 struct ctf_field_class_bit_array base
;
105 enum ctf_field_class_meaning meaning
;
107 bt_field_class_integer_preferred_display_base disp_base
;
108 enum ctf_encoding encoding
;
109 int64_t storing_index
;
112 struct ctf_clock_class
*mapped_clock_class
;
127 struct ctf_field_class_enum_mapping
{
130 /* Array of `struct ctf_range` */
134 struct ctf_field_class_enum
{
135 struct ctf_field_class_int base
;
137 /* Array of `struct ctf_field_class_enum_mapping` */
141 struct ctf_field_class_float
{
142 struct ctf_field_class_bit_array base
;
145 struct ctf_field_class_string
{
146 struct ctf_field_class base
;
147 enum ctf_encoding encoding
;
150 struct ctf_named_field_class
{
151 /* Original name which can include a leading `_` */
154 /* Name as translated to trace IR (leading `_` removed) */
158 struct ctf_field_class
*fc
;
161 struct ctf_field_class_struct
{
162 struct ctf_field_class base
;
164 /* Array of `struct ctf_named_field_class` */
168 struct ctf_field_path
{
171 /* Array of `int64_t` */
175 struct ctf_field_class_variant_range
{
176 struct ctf_range range
;
177 uint64_t option_index
;
180 struct ctf_field_class_variant
{
181 struct ctf_field_class base
;
183 struct ctf_field_path tag_path
;
184 uint64_t stored_tag_index
;
186 /* Array of `struct ctf_named_field_class` */
189 /* Array of `struct ctf_field_class_variant_range` */
193 struct ctf_field_class_enum
*tag_fc
;
196 struct ctf_field_class_array_base
{
197 struct ctf_field_class base
;
198 struct ctf_field_class
*elem_fc
;
202 struct ctf_field_class_array
{
203 struct ctf_field_class_array_base base
;
204 enum ctf_field_class_meaning meaning
;
208 struct ctf_field_class_sequence
{
209 struct ctf_field_class_array_base base
;
211 struct ctf_field_path length_path
;
212 uint64_t stored_length_index
;
215 struct ctf_field_class_int
*length_fc
;
218 struct ctf_event_class
{
222 bt_event_class_log_level log_level
;
226 struct ctf_field_class
*spec_context_fc
;
229 struct ctf_field_class
*payload_fc
;
231 /* Weak, set during translation */
232 bt_event_class
*ir_ec
;
235 struct ctf_stream_class
{
238 bool packets_have_ts_begin
;
239 bool packets_have_ts_end
;
240 bool has_discarded_events
;
241 bool has_discarded_packets
;
242 bool discarded_events_have_default_cs
;
243 bool discarded_packets_have_default_cs
;
246 struct ctf_field_class
*packet_context_fc
;
249 struct ctf_field_class
*event_header_fc
;
252 struct ctf_field_class
*event_common_context_fc
;
254 /* Array of `struct ctf_event_class *`, owned by this */
255 GPtrArray
*event_classes
;
258 * Hash table mapping event class IDs to `struct ctf_event_class *`,
261 GHashTable
*event_classes_by_id
;
264 struct ctf_clock_class
*default_clock_class
;
266 /* Weak, set during translation */
267 bt_stream_class
*ir_sc
;
270 enum ctf_trace_class_env_entry_type
{
271 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
272 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
275 struct ctf_trace_class_env_entry
{
276 enum ctf_trace_class_env_entry_type type
;
285 struct ctf_trace_class
{
290 enum ctf_byte_order default_byte_order
;
293 struct ctf_field_class
*packet_header_fc
;
295 uint64_t stored_value_count
;
297 /* Array of `struct ctf_clock_class *` (owned by this) */
298 GPtrArray
*clock_classes
;
300 /* Array of `struct ctf_stream_class *` */
301 GPtrArray
*stream_classes
;
303 /* Array of `struct ctf_trace_class_env_entry` */
308 /* Weak, set during translation */
309 bt_trace_class
*ir_tc
;
313 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
316 void _ctf_field_class_init(struct ctf_field_class
*fc
,
317 enum ctf_field_class_type type
, unsigned int alignment
)
321 fc
->alignment
= alignment
;
326 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
327 enum ctf_field_class_type type
)
329 _ctf_field_class_init((void *) fc
, type
, 1);
333 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
334 enum ctf_field_class_type type
)
336 _ctf_field_class_bit_array_init((void *) fc
, type
);
337 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
338 fc
->storing_index
= -1;
342 void ctf_field_path_init(struct ctf_field_path
*field_path
)
344 BT_ASSERT(field_path
);
345 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
346 BT_ASSERT(field_path
->path
);
350 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
352 BT_ASSERT(field_path
);
354 if (field_path
->path
) {
355 g_array_free(field_path
->path
, TRUE
);
360 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
363 named_fc
->name
= g_string_new(NULL
);
364 BT_ASSERT(named_fc
->name
);
365 named_fc
->orig_name
= g_string_new(NULL
);
366 BT_ASSERT(named_fc
->orig_name
);
370 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
374 if (named_fc
->name
) {
375 g_string_free(named_fc
->name
, TRUE
);
378 if (named_fc
->orig_name
) {
379 g_string_free(named_fc
->orig_name
, TRUE
);
382 ctf_field_class_destroy(named_fc
->fc
);
386 void _ctf_field_class_enum_mapping_init(
387 struct ctf_field_class_enum_mapping
*mapping
)
390 mapping
->label
= g_string_new(NULL
);
391 BT_ASSERT(mapping
->label
);
392 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
393 BT_ASSERT(mapping
->ranges
);
397 void _ctf_field_class_enum_mapping_fini(
398 struct ctf_field_class_enum_mapping
*mapping
)
402 if (mapping
->label
) {
403 g_string_free(mapping
->label
, TRUE
);
406 if (mapping
->ranges
) {
407 g_array_free(mapping
->ranges
, TRUE
);
412 struct ctf_field_class_int
*ctf_field_class_int_create(void)
414 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
417 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
422 struct ctf_field_class_float
*ctf_field_class_float_create(void)
424 struct ctf_field_class_float
*fc
=
425 g_new0(struct ctf_field_class_float
, 1);
428 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
433 struct ctf_field_class_string
*ctf_field_class_string_create(void)
435 struct ctf_field_class_string
*fc
=
436 g_new0(struct ctf_field_class_string
, 1);
439 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
444 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
446 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
449 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
450 fc
->mappings
= g_array_new(FALSE
, TRUE
,
451 sizeof(struct ctf_field_class_enum_mapping
));
452 BT_ASSERT(fc
->mappings
);
457 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
459 struct ctf_field_class_struct
*fc
=
460 g_new0(struct ctf_field_class_struct
, 1);
463 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
464 fc
->members
= g_array_new(FALSE
, TRUE
,
465 sizeof(struct ctf_named_field_class
));
466 BT_ASSERT(fc
->members
);
467 fc
->base
.is_compound
= true;
472 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
474 struct ctf_field_class_variant
*fc
=
475 g_new0(struct ctf_field_class_variant
, 1);
478 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
479 fc
->options
= g_array_new(FALSE
, TRUE
,
480 sizeof(struct ctf_named_field_class
));
481 BT_ASSERT(fc
->options
);
482 fc
->ranges
= g_array_new(FALSE
, TRUE
,
483 sizeof(struct ctf_field_class_variant_range
));
484 BT_ASSERT(fc
->ranges
);
485 fc
->tag_ref
= g_string_new(NULL
);
486 BT_ASSERT(fc
->tag_ref
);
487 ctf_field_path_init(&fc
->tag_path
);
488 fc
->base
.is_compound
= true;
493 struct ctf_field_class_array
*ctf_field_class_array_create(void)
495 struct ctf_field_class_array
*fc
=
496 g_new0(struct ctf_field_class_array
, 1);
499 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
500 fc
->base
.base
.is_compound
= true;
505 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
507 struct ctf_field_class_sequence
*fc
=
508 g_new0(struct ctf_field_class_sequence
, 1);
511 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
512 fc
->length_ref
= g_string_new(NULL
);
513 BT_ASSERT(fc
->length_ref
);
514 ctf_field_path_init(&fc
->length_path
);
515 fc
->base
.base
.is_compound
= true;
520 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
527 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
534 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
535 struct ctf_field_class_enum_mapping
*mapping
=
536 &g_array_index(fc
->mappings
,
537 struct ctf_field_class_enum_mapping
, i
);
539 _ctf_field_class_enum_mapping_fini(mapping
);
542 g_array_free(fc
->mappings
, TRUE
);
549 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
556 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
563 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
570 for (i
= 0; i
< fc
->members
->len
; i
++) {
571 struct ctf_named_field_class
*named_fc
=
572 &g_array_index(fc
->members
,
573 struct ctf_named_field_class
, i
);
575 _ctf_named_field_class_fini(named_fc
);
578 g_array_free(fc
->members
, TRUE
);
585 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
588 ctf_field_class_destroy(fc
->elem_fc
);
592 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
595 _ctf_field_class_array_base_fini((void *) fc
);
600 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
603 _ctf_field_class_array_base_fini((void *) fc
);
605 if (fc
->length_ref
) {
606 g_string_free(fc
->length_ref
, TRUE
);
609 ctf_field_path_fini(&fc
->length_path
);
614 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
621 for (i
= 0; i
< fc
->options
->len
; i
++) {
622 struct ctf_named_field_class
*named_fc
=
623 &g_array_index(fc
->options
,
624 struct ctf_named_field_class
, i
);
626 _ctf_named_field_class_fini(named_fc
);
629 g_array_free(fc
->options
, TRUE
);
633 g_array_free(fc
->ranges
, TRUE
);
637 g_string_free(fc
->tag_ref
, TRUE
);
640 ctf_field_path_fini(&fc
->tag_path
);
645 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
652 case CTF_FIELD_CLASS_TYPE_INT
:
653 _ctf_field_class_int_destroy((void *) fc
);
655 case CTF_FIELD_CLASS_TYPE_ENUM
:
656 _ctf_field_class_enum_destroy((void *) fc
);
658 case CTF_FIELD_CLASS_TYPE_FLOAT
:
659 _ctf_field_class_float_destroy((void *) fc
);
661 case CTF_FIELD_CLASS_TYPE_STRING
:
662 _ctf_field_class_string_destroy((void *) fc
);
664 case CTF_FIELD_CLASS_TYPE_STRUCT
:
665 _ctf_field_class_struct_destroy((void *) fc
);
667 case CTF_FIELD_CLASS_TYPE_ARRAY
:
668 _ctf_field_class_array_destroy((void *) fc
);
670 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
671 _ctf_field_class_sequence_destroy((void *) fc
);
673 case CTF_FIELD_CLASS_TYPE_VARIANT
:
674 _ctf_field_class_variant_destroy((void *) fc
);
682 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
683 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
686 BT_ASSERT(index
< mapping
->ranges
->len
);
687 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
691 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
692 struct ctf_field_class_enum
*fc
, uint64_t index
)
695 BT_ASSERT(index
< fc
->mappings
->len
);
696 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
701 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
702 struct ctf_field_class_enum
*fc
, const char *label
)
704 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
710 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
711 struct ctf_field_class_enum_mapping
*mapping
=
712 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
714 if (strcmp(mapping
->label
->str
, label
) == 0) {
715 ret_mapping
= mapping
;
725 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
726 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
728 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
729 struct ctf_range range
= {
738 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
739 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
742 if (strcmp(mapping
->label
->str
, label
) == 0) {
747 if (i
== fc
->mappings
->len
) {
752 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
753 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
754 fc
, fc
->mappings
->len
- 1);
755 _ctf_field_class_enum_mapping_init(mapping
);
756 g_string_assign(mapping
->label
, label
);
759 g_array_append_val(mapping
->ranges
, range
);
763 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
764 struct ctf_field_class_struct
*fc
, uint64_t index
)
767 BT_ASSERT(index
< fc
->members
->len
);
768 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
773 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
774 struct ctf_field_class_struct
*fc
, const char *name
)
777 struct ctf_named_field_class
*ret_named_fc
= NULL
;
782 for (i
= 0; i
< fc
->members
->len
; i
++) {
783 struct ctf_named_field_class
*named_fc
=
784 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
786 if (strcmp(name
, named_fc
->name
->str
) == 0) {
787 ret_named_fc
= named_fc
;
797 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
798 struct ctf_field_class_struct
*struct_fc
, const char *name
)
800 struct ctf_named_field_class
*named_fc
= NULL
;
801 struct ctf_field_class
*fc
= NULL
;
807 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
819 struct ctf_field_class_int
*
820 ctf_field_class_struct_borrow_member_int_field_class_by_name(
821 struct ctf_field_class_struct
*struct_fc
, const char *name
)
823 struct ctf_field_class_int
*int_fc
= NULL
;
826 ctf_field_class_struct_borrow_member_field_class_by_name(
832 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
833 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
843 void _ctf_named_field_class_unescape_orig_name(
844 struct ctf_named_field_class
*named_fc
)
846 const char *name
= named_fc
->orig_name
->str
;
848 if (name
[0] == '_') {
852 g_string_assign(named_fc
->name
, name
);
856 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
857 const char *orig_name
, struct ctf_field_class
*member_fc
)
859 struct ctf_named_field_class
*named_fc
;
862 BT_ASSERT(orig_name
);
863 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
865 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
866 fc
->members
->len
- 1);
867 _ctf_named_field_class_init(named_fc
);
868 g_string_assign(named_fc
->orig_name
, orig_name
);
869 _ctf_named_field_class_unescape_orig_name(named_fc
);
870 named_fc
->fc
= member_fc
;
872 if (member_fc
->alignment
> fc
->base
.alignment
) {
873 fc
->base
.alignment
= member_fc
->alignment
;
878 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
879 struct ctf_field_class_variant
*fc
, uint64_t index
)
882 BT_ASSERT(index
< fc
->options
->len
);
883 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
888 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
889 struct ctf_field_class_variant
*fc
, const char *name
)
892 struct ctf_named_field_class
*ret_named_fc
= NULL
;
897 for (i
= 0; i
< fc
->options
->len
; i
++) {
898 struct ctf_named_field_class
*named_fc
=
899 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
901 if (strcmp(name
, named_fc
->name
->str
) == 0) {
902 ret_named_fc
= named_fc
;
912 struct ctf_field_class_variant_range
*
913 ctf_field_class_variant_borrow_range_by_index(
914 struct ctf_field_class_variant
*fc
, uint64_t index
)
917 BT_ASSERT(index
< fc
->ranges
->len
);
918 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
923 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
924 const char *orig_name
, struct ctf_field_class
*option_fc
)
926 struct ctf_named_field_class
*named_fc
;
929 BT_ASSERT(orig_name
);
930 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
932 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
933 fc
->options
->len
- 1);
934 _ctf_named_field_class_init(named_fc
);
935 g_string_assign(named_fc
->orig_name
, orig_name
);
936 _ctf_named_field_class_unescape_orig_name(named_fc
);
937 named_fc
->fc
= option_fc
;
941 void ctf_field_class_variant_set_tag_field_class(
942 struct ctf_field_class_variant
*fc
,
943 struct ctf_field_class_enum
*tag_fc
)
951 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
953 struct ctf_named_field_class
*named_fc
=
954 ctf_field_class_variant_borrow_option_by_index(
956 struct ctf_field_class_enum_mapping
*mapping
;
958 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
959 tag_fc
, named_fc
->orig_name
->str
);
964 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
966 struct ctf_range
*range
=
967 ctf_field_class_enum_mapping_borrow_range_by_index(
969 struct ctf_field_class_variant_range var_range
;
971 var_range
.range
= *range
;
972 var_range
.option_index
= option_i
;
973 g_array_append_val(fc
->ranges
, var_range
);
979 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
980 struct ctf_field_class
*comp_fc
, uint64_t index
)
982 struct ctf_field_class
*fc
= NULL
;
984 switch (comp_fc
->type
) {
985 case CTF_FIELD_CLASS_TYPE_STRUCT
:
987 struct ctf_named_field_class
*named_fc
=
988 ctf_field_class_struct_borrow_member_by_index(
989 (void *) comp_fc
, index
);
995 case CTF_FIELD_CLASS_TYPE_VARIANT
:
997 struct ctf_named_field_class
*named_fc
=
998 ctf_field_class_variant_borrow_option_by_index(
999 (void *) comp_fc
, index
);
1001 BT_ASSERT(named_fc
);
1005 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1006 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1008 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1010 fc
= array_fc
->elem_fc
;
1021 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1023 uint64_t field_count
;
1026 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1028 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1030 field_count
= struct_fc
->members
->len
;
1033 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1035 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1037 field_count
= var_fc
->options
->len
;
1040 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1041 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1043 * Array and sequence types always contain a single
1044 * member (the element type).
1056 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1057 struct ctf_field_class
*fc
, const char *orig_name
)
1059 int64_t ret_index
= -1;
1063 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1065 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1067 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1068 struct ctf_named_field_class
*named_fc
=
1069 ctf_field_class_struct_borrow_member_by_index(
1072 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1073 ret_index
= (int64_t) i
;
1080 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1082 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1084 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1085 struct ctf_named_field_class
*named_fc
=
1086 ctf_field_class_variant_borrow_option_by_index(
1089 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1090 ret_index
= (int64_t) i
;
1106 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1109 g_array_append_val(fp
->path
, index
);
1113 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1117 BT_ASSERT(index
< fp
->path
->len
);
1118 return g_array_index(fp
->path
, int64_t, index
);
1122 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1125 g_array_set_size(fp
->path
, 0);
1129 const char *ctf_scope_string(enum ctf_scope scope
)
1132 case CTF_SCOPE_PACKET_HEADER
:
1133 return "PACKET_HEADER";
1134 case CTF_SCOPE_PACKET_CONTEXT
:
1135 return "PACKET_CONTEXT";
1136 case CTF_SCOPE_EVENT_HEADER
:
1137 return "EVENT_HEADER";
1138 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1139 return "EVENT_COMMON_CONTEXT";
1140 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1141 return "EVENT_SPECIFIC_CONTEXT";
1142 case CTF_SCOPE_EVENT_PAYLOAD
:
1143 return "EVENT_PAYLOAD";
1150 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1152 GString
*str
= g_string_new(NULL
);
1161 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1163 for (i
= 0; i
< path
->path
->len
; i
++) {
1164 g_string_append_printf(str
, ", %" PRId64
,
1165 ctf_field_path_borrow_index_by_index(path
, i
));
1168 g_string_append(str
, "]");
1175 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1176 struct ctf_field_path
*field_path
,
1177 struct ctf_trace_class
*tc
,
1178 struct ctf_stream_class
*sc
,
1179 struct ctf_event_class
*ec
)
1182 struct ctf_field_class
*fc
;
1184 switch (field_path
->root
) {
1185 case CTF_SCOPE_PACKET_HEADER
:
1186 fc
= tc
->packet_header_fc
;
1188 case CTF_SCOPE_PACKET_CONTEXT
:
1189 fc
= sc
->packet_context_fc
;
1191 case CTF_SCOPE_EVENT_HEADER
:
1192 fc
= sc
->event_header_fc
;
1194 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1195 fc
= sc
->event_common_context_fc
;
1197 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1198 fc
= ec
->spec_context_fc
;
1200 case CTF_SCOPE_EVENT_PAYLOAD
:
1201 fc
= ec
->payload_fc
;
1209 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1210 int64_t child_index
=
1211 ctf_field_path_borrow_index_by_index(field_path
, i
);
1212 struct ctf_field_class
*child_fc
=
1213 ctf_field_class_compound_borrow_field_class_by_index(
1215 BT_ASSERT(child_fc
);
1224 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1227 void ctf_field_class_bit_array_copy_content(
1228 struct ctf_field_class_bit_array
*dst_fc
,
1229 struct ctf_field_class_bit_array
*src_fc
)
1233 dst_fc
->byte_order
= src_fc
->byte_order
;
1234 dst_fc
->size
= src_fc
->size
;
1238 void ctf_field_class_int_copy_content(
1239 struct ctf_field_class_int
*dst_fc
,
1240 struct ctf_field_class_int
*src_fc
)
1242 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1243 dst_fc
->meaning
= src_fc
->meaning
;
1244 dst_fc
->is_signed
= src_fc
->is_signed
;
1245 dst_fc
->disp_base
= src_fc
->disp_base
;
1246 dst_fc
->encoding
= src_fc
->encoding
;
1247 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1248 dst_fc
->storing_index
= src_fc
->storing_index
;
1252 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1253 struct ctf_field_class_int
*fc
)
1255 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1258 ctf_field_class_int_copy_content(copy_fc
, fc
);
1263 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1264 struct ctf_field_class_enum
*fc
)
1266 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1270 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1272 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1275 struct ctf_field_class_enum_mapping
*mapping
=
1276 &g_array_index(fc
->mappings
,
1277 struct ctf_field_class_enum_mapping
, i
);
1279 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1280 struct ctf_range
*range
=
1281 &g_array_index(mapping
->ranges
,
1282 struct ctf_range
, range_i
);
1284 ctf_field_class_enum_map_range(copy_fc
,
1285 mapping
->label
->str
, range
->lower
.u
,
1294 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1295 struct ctf_field_class_float
*fc
)
1297 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1300 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1305 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1306 struct ctf_field_class_string
*fc
)
1308 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1315 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1316 struct ctf_field_class_struct
*fc
)
1318 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1323 for (i
= 0; i
< fc
->members
->len
; i
++) {
1324 struct ctf_named_field_class
*named_fc
=
1325 &g_array_index(fc
->members
,
1326 struct ctf_named_field_class
, i
);
1328 ctf_field_class_struct_append_member(copy_fc
,
1329 named_fc
->name
->str
,
1330 ctf_field_class_copy(named_fc
->fc
));
1337 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1338 struct ctf_field_path
*src_fp
)
1344 dst_fp
->root
= src_fp
->root
;
1345 ctf_field_path_clear(dst_fp
);
1347 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1348 int64_t index
= ctf_field_path_borrow_index_by_index(
1351 ctf_field_path_append_index(dst_fp
, index
);
1356 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1357 struct ctf_field_class_variant
*fc
)
1359 struct ctf_field_class_variant
*copy_fc
=
1360 ctf_field_class_variant_create();
1365 for (i
= 0; i
< fc
->options
->len
; i
++) {
1366 struct ctf_named_field_class
*named_fc
=
1367 &g_array_index(fc
->options
,
1368 struct ctf_named_field_class
, i
);
1370 ctf_field_class_variant_append_option(copy_fc
,
1371 named_fc
->name
->str
,
1372 ctf_field_class_copy(named_fc
->fc
));
1375 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1376 struct ctf_field_class_variant_range
*range
=
1377 &g_array_index(fc
->ranges
,
1378 struct ctf_field_class_variant_range
, i
);
1380 g_array_append_val(copy_fc
->ranges
, *range
);
1383 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1384 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1385 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1390 void ctf_field_class_array_base_copy_content(
1391 struct ctf_field_class_array_base
*dst_fc
,
1392 struct ctf_field_class_array_base
*src_fc
)
1396 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1397 dst_fc
->is_text
= src_fc
->is_text
;
1401 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1402 struct ctf_field_class_array
*fc
)
1404 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1407 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1408 copy_fc
->length
= fc
->length
;
1413 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1414 struct ctf_field_class_sequence
*fc
)
1416 struct ctf_field_class_sequence
*copy_fc
=
1417 ctf_field_class_sequence_create();
1420 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1421 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1422 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1423 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1428 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1430 struct ctf_field_class
*copy_fc
= NULL
;
1437 * Translation should not have happened yet.
1439 BT_ASSERT(!fc
->ir_fc
);
1442 case CTF_FIELD_CLASS_TYPE_INT
:
1443 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1445 case CTF_FIELD_CLASS_TYPE_ENUM
:
1446 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1448 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1449 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1451 case CTF_FIELD_CLASS_TYPE_STRING
:
1452 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1454 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1455 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1457 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1458 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1460 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1461 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1463 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1464 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1470 copy_fc
->type
= fc
->type
;
1471 copy_fc
->alignment
= fc
->alignment
;
1472 copy_fc
->in_ir
= fc
->in_ir
;
1479 struct ctf_event_class
*ctf_event_class_create(void)
1481 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1484 ec
->name
= g_string_new(NULL
);
1485 BT_ASSERT(ec
->name
);
1486 ec
->emf_uri
= g_string_new(NULL
);
1487 BT_ASSERT(ec
->emf_uri
);
1493 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1500 g_string_free(ec
->name
, TRUE
);
1504 g_string_free(ec
->emf_uri
, TRUE
);
1507 ctf_field_class_destroy(ec
->spec_context_fc
);
1508 ctf_field_class_destroy(ec
->payload_fc
);
1513 struct ctf_stream_class
*ctf_stream_class_create(void)
1515 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1518 sc
->event_classes
= g_ptr_array_new_with_free_func(
1519 (GDestroyNotify
) ctf_event_class_destroy
);
1520 BT_ASSERT(sc
->event_classes
);
1521 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1523 BT_ASSERT(sc
->event_classes_by_id
);
1528 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1534 if (sc
->event_classes
) {
1535 g_ptr_array_free(sc
->event_classes
, TRUE
);
1538 if (sc
->event_classes_by_id
) {
1539 g_hash_table_destroy(sc
->event_classes_by_id
);
1542 ctf_field_class_destroy(sc
->packet_context_fc
);
1543 ctf_field_class_destroy(sc
->event_header_fc
);
1544 ctf_field_class_destroy(sc
->event_common_context_fc
);
1549 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1550 struct ctf_event_class
*ec
)
1552 g_ptr_array_add(sc
->event_classes
, ec
);
1553 g_hash_table_insert(sc
->event_classes_by_id
,
1554 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1558 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1559 struct ctf_stream_class
*sc
, uint64_t type
)
1562 return g_hash_table_lookup(sc
->event_classes_by_id
,
1563 GUINT_TO_POINTER((guint
) type
));
1567 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1570 entry
->name
= g_string_new(NULL
);
1571 BT_ASSERT(entry
->name
);
1572 entry
->value
.str
= g_string_new(NULL
);
1573 BT_ASSERT(entry
->value
.str
);
1577 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1582 g_string_free(entry
->name
, TRUE
);
1585 if (entry
->value
.str
) {
1586 g_string_free(entry
->value
.str
, TRUE
);
1591 struct ctf_clock_class
*ctf_clock_class_create(void)
1593 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1596 cc
->name
= g_string_new(NULL
);
1597 BT_ASSERT(cc
->name
);
1598 cc
->description
= g_string_new(NULL
);
1599 BT_ASSERT(cc
->description
);
1604 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1611 g_string_free(cc
->name
, TRUE
);
1614 if (cc
->description
) {
1615 g_string_free(cc
->description
, TRUE
);
1618 bt_clock_class_put_ref(cc
->ir_cc
);
1623 struct ctf_trace_class
*ctf_trace_class_create(void)
1625 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1628 tc
->default_byte_order
= -1;
1629 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1630 (GDestroyNotify
) ctf_clock_class_destroy
);
1631 BT_ASSERT(tc
->clock_classes
);
1632 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1633 (GDestroyNotify
) ctf_stream_class_destroy
);
1634 BT_ASSERT(tc
->stream_classes
);
1635 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1636 sizeof(struct ctf_trace_class_env_entry
));
1641 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1647 ctf_field_class_destroy(tc
->packet_header_fc
);
1649 if (tc
->clock_classes
) {
1650 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1653 if (tc
->stream_classes
) {
1654 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1657 if (tc
->env_entries
) {
1660 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1661 struct ctf_trace_class_env_entry
*entry
=
1662 &g_array_index(tc
->env_entries
,
1663 struct ctf_trace_class_env_entry
, i
);
1665 _ctf_trace_class_env_entry_fini(entry
);
1668 g_array_free(tc
->env_entries
, TRUE
);
1675 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1676 const char *name
, enum ctf_trace_class_env_entry_type type
,
1677 const char *str_value
, int64_t i_value
)
1679 struct ctf_trace_class_env_entry
*entry
;
1683 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1685 entry
= &g_array_index(tc
->env_entries
,
1686 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1688 _ctf_trace_class_env_entry_init(entry
);
1689 g_string_assign(entry
->name
, name
);
1692 g_string_assign(entry
->value
.str
, str_value
);
1695 entry
->value
.i
= i_value
;
1699 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1700 struct ctf_trace_class
*tc
, uint64_t id
)
1703 struct ctf_stream_class
*ret_sc
= NULL
;
1707 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1708 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1721 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1722 struct ctf_trace_class
*tc
, const char *name
)
1725 struct ctf_clock_class
*ret_cc
= NULL
;
1730 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1731 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1733 BT_ASSERT(cc
->name
);
1734 if (strcmp(cc
->name
->str
, name
) == 0) {
1745 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1746 struct ctf_trace_class
*tc
, uint64_t index
)
1749 BT_ASSERT(index
< tc
->env_entries
->len
);
1750 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1755 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1756 struct ctf_trace_class
*tc
, const char *name
)
1758 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1764 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1765 struct ctf_trace_class_env_entry
*env_entry
=
1766 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1768 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1769 ret_entry
= env_entry
;
1778 #endif /* _CTF_META_H */