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
;
224 bool is_log_level_set
;
227 struct ctf_field_class
*spec_context_fc
;
230 struct ctf_field_class
*payload_fc
;
232 /* Weak, set during translation */
233 bt_event_class
*ir_ec
;
236 struct ctf_stream_class
{
239 bool packets_have_ts_begin
;
240 bool packets_have_ts_end
;
241 bool has_discarded_events
;
242 bool has_discarded_packets
;
243 bool discarded_events_have_default_cs
;
244 bool discarded_packets_have_default_cs
;
247 struct ctf_field_class
*packet_context_fc
;
250 struct ctf_field_class
*event_header_fc
;
253 struct ctf_field_class
*event_common_context_fc
;
255 /* Array of `struct ctf_event_class *`, owned by this */
256 GPtrArray
*event_classes
;
259 * Hash table mapping event class IDs to `struct ctf_event_class *`,
262 GHashTable
*event_classes_by_id
;
265 struct ctf_clock_class
*default_clock_class
;
267 /* Weak, set during translation */
268 bt_stream_class
*ir_sc
;
271 enum ctf_trace_class_env_entry_type
{
272 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
273 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
276 struct ctf_trace_class_env_entry
{
277 enum ctf_trace_class_env_entry_type type
;
286 struct ctf_trace_class
{
291 enum ctf_byte_order default_byte_order
;
294 struct ctf_field_class
*packet_header_fc
;
296 uint64_t stored_value_count
;
298 /* Array of `struct ctf_clock_class *` (owned by this) */
299 GPtrArray
*clock_classes
;
301 /* Array of `struct ctf_stream_class *` */
302 GPtrArray
*stream_classes
;
304 /* Array of `struct ctf_trace_class_env_entry` */
309 /* Weak, set during translation */
310 bt_trace_class
*ir_tc
;
314 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
317 void _ctf_field_class_init(struct ctf_field_class
*fc
,
318 enum ctf_field_class_type type
, unsigned int alignment
)
322 fc
->alignment
= alignment
;
327 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
328 enum ctf_field_class_type type
)
330 _ctf_field_class_init((void *) fc
, type
, 1);
334 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
335 enum ctf_field_class_type type
)
337 _ctf_field_class_bit_array_init((void *) fc
, type
);
338 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
339 fc
->storing_index
= -1;
343 void ctf_field_path_init(struct ctf_field_path
*field_path
)
345 BT_ASSERT(field_path
);
346 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
347 BT_ASSERT(field_path
->path
);
351 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
353 BT_ASSERT(field_path
);
355 if (field_path
->path
) {
356 g_array_free(field_path
->path
, TRUE
);
361 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
364 named_fc
->name
= g_string_new(NULL
);
365 BT_ASSERT(named_fc
->name
);
366 named_fc
->orig_name
= g_string_new(NULL
);
367 BT_ASSERT(named_fc
->orig_name
);
371 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
375 if (named_fc
->name
) {
376 g_string_free(named_fc
->name
, TRUE
);
379 if (named_fc
->orig_name
) {
380 g_string_free(named_fc
->orig_name
, TRUE
);
383 ctf_field_class_destroy(named_fc
->fc
);
387 void _ctf_field_class_enum_mapping_init(
388 struct ctf_field_class_enum_mapping
*mapping
)
391 mapping
->label
= g_string_new(NULL
);
392 BT_ASSERT(mapping
->label
);
393 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
394 BT_ASSERT(mapping
->ranges
);
398 void _ctf_field_class_enum_mapping_fini(
399 struct ctf_field_class_enum_mapping
*mapping
)
403 if (mapping
->label
) {
404 g_string_free(mapping
->label
, TRUE
);
407 if (mapping
->ranges
) {
408 g_array_free(mapping
->ranges
, TRUE
);
413 struct ctf_field_class_int
*ctf_field_class_int_create(void)
415 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
418 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
423 struct ctf_field_class_float
*ctf_field_class_float_create(void)
425 struct ctf_field_class_float
*fc
=
426 g_new0(struct ctf_field_class_float
, 1);
429 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
434 struct ctf_field_class_string
*ctf_field_class_string_create(void)
436 struct ctf_field_class_string
*fc
=
437 g_new0(struct ctf_field_class_string
, 1);
440 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
445 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
447 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
450 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
451 fc
->mappings
= g_array_new(FALSE
, TRUE
,
452 sizeof(struct ctf_field_class_enum_mapping
));
453 BT_ASSERT(fc
->mappings
);
458 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
460 struct ctf_field_class_struct
*fc
=
461 g_new0(struct ctf_field_class_struct
, 1);
464 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
465 fc
->members
= g_array_new(FALSE
, TRUE
,
466 sizeof(struct ctf_named_field_class
));
467 BT_ASSERT(fc
->members
);
468 fc
->base
.is_compound
= true;
473 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
475 struct ctf_field_class_variant
*fc
=
476 g_new0(struct ctf_field_class_variant
, 1);
479 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
480 fc
->options
= g_array_new(FALSE
, TRUE
,
481 sizeof(struct ctf_named_field_class
));
482 BT_ASSERT(fc
->options
);
483 fc
->ranges
= g_array_new(FALSE
, TRUE
,
484 sizeof(struct ctf_field_class_variant_range
));
485 BT_ASSERT(fc
->ranges
);
486 fc
->tag_ref
= g_string_new(NULL
);
487 BT_ASSERT(fc
->tag_ref
);
488 ctf_field_path_init(&fc
->tag_path
);
489 fc
->base
.is_compound
= true;
494 struct ctf_field_class_array
*ctf_field_class_array_create(void)
496 struct ctf_field_class_array
*fc
=
497 g_new0(struct ctf_field_class_array
, 1);
500 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
501 fc
->base
.base
.is_compound
= true;
506 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
508 struct ctf_field_class_sequence
*fc
=
509 g_new0(struct ctf_field_class_sequence
, 1);
512 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
513 fc
->length_ref
= g_string_new(NULL
);
514 BT_ASSERT(fc
->length_ref
);
515 ctf_field_path_init(&fc
->length_path
);
516 fc
->base
.base
.is_compound
= true;
521 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
528 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
535 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
536 struct ctf_field_class_enum_mapping
*mapping
=
537 &g_array_index(fc
->mappings
,
538 struct ctf_field_class_enum_mapping
, i
);
540 _ctf_field_class_enum_mapping_fini(mapping
);
543 g_array_free(fc
->mappings
, TRUE
);
550 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
557 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
564 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
571 for (i
= 0; i
< fc
->members
->len
; i
++) {
572 struct ctf_named_field_class
*named_fc
=
573 &g_array_index(fc
->members
,
574 struct ctf_named_field_class
, i
);
576 _ctf_named_field_class_fini(named_fc
);
579 g_array_free(fc
->members
, TRUE
);
586 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
589 ctf_field_class_destroy(fc
->elem_fc
);
593 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
596 _ctf_field_class_array_base_fini((void *) fc
);
601 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
604 _ctf_field_class_array_base_fini((void *) fc
);
606 if (fc
->length_ref
) {
607 g_string_free(fc
->length_ref
, TRUE
);
610 ctf_field_path_fini(&fc
->length_path
);
615 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
622 for (i
= 0; i
< fc
->options
->len
; i
++) {
623 struct ctf_named_field_class
*named_fc
=
624 &g_array_index(fc
->options
,
625 struct ctf_named_field_class
, i
);
627 _ctf_named_field_class_fini(named_fc
);
630 g_array_free(fc
->options
, TRUE
);
634 g_array_free(fc
->ranges
, TRUE
);
638 g_string_free(fc
->tag_ref
, TRUE
);
641 ctf_field_path_fini(&fc
->tag_path
);
646 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
653 case CTF_FIELD_CLASS_TYPE_INT
:
654 _ctf_field_class_int_destroy((void *) fc
);
656 case CTF_FIELD_CLASS_TYPE_ENUM
:
657 _ctf_field_class_enum_destroy((void *) fc
);
659 case CTF_FIELD_CLASS_TYPE_FLOAT
:
660 _ctf_field_class_float_destroy((void *) fc
);
662 case CTF_FIELD_CLASS_TYPE_STRING
:
663 _ctf_field_class_string_destroy((void *) fc
);
665 case CTF_FIELD_CLASS_TYPE_STRUCT
:
666 _ctf_field_class_struct_destroy((void *) fc
);
668 case CTF_FIELD_CLASS_TYPE_ARRAY
:
669 _ctf_field_class_array_destroy((void *) fc
);
671 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
672 _ctf_field_class_sequence_destroy((void *) fc
);
674 case CTF_FIELD_CLASS_TYPE_VARIANT
:
675 _ctf_field_class_variant_destroy((void *) fc
);
683 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
684 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
687 BT_ASSERT(index
< mapping
->ranges
->len
);
688 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
692 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
693 struct ctf_field_class_enum
*fc
, uint64_t index
)
696 BT_ASSERT(index
< fc
->mappings
->len
);
697 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
702 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
703 struct ctf_field_class_enum
*fc
, const char *label
)
705 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
711 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
712 struct ctf_field_class_enum_mapping
*mapping
=
713 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
715 if (strcmp(mapping
->label
->str
, label
) == 0) {
716 ret_mapping
= mapping
;
726 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
727 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
729 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
730 struct ctf_range range
= {
739 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
740 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
743 if (strcmp(mapping
->label
->str
, label
) == 0) {
748 if (i
== fc
->mappings
->len
) {
753 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
754 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
755 fc
, fc
->mappings
->len
- 1);
756 _ctf_field_class_enum_mapping_init(mapping
);
757 g_string_assign(mapping
->label
, label
);
760 g_array_append_val(mapping
->ranges
, range
);
764 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
765 struct ctf_field_class_struct
*fc
, uint64_t index
)
768 BT_ASSERT(index
< fc
->members
->len
);
769 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
774 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
775 struct ctf_field_class_struct
*fc
, const char *name
)
778 struct ctf_named_field_class
*ret_named_fc
= NULL
;
783 for (i
= 0; i
< fc
->members
->len
; i
++) {
784 struct ctf_named_field_class
*named_fc
=
785 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
787 if (strcmp(name
, named_fc
->name
->str
) == 0) {
788 ret_named_fc
= named_fc
;
798 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
799 struct ctf_field_class_struct
*struct_fc
, const char *name
)
801 struct ctf_named_field_class
*named_fc
= NULL
;
802 struct ctf_field_class
*fc
= NULL
;
808 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
820 struct ctf_field_class_int
*
821 ctf_field_class_struct_borrow_member_int_field_class_by_name(
822 struct ctf_field_class_struct
*struct_fc
, const char *name
)
824 struct ctf_field_class_int
*int_fc
= NULL
;
827 ctf_field_class_struct_borrow_member_field_class_by_name(
833 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
834 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
844 void _ctf_named_field_class_unescape_orig_name(
845 struct ctf_named_field_class
*named_fc
)
847 const char *name
= named_fc
->orig_name
->str
;
849 if (name
[0] == '_') {
853 g_string_assign(named_fc
->name
, name
);
857 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
858 const char *orig_name
, struct ctf_field_class
*member_fc
)
860 struct ctf_named_field_class
*named_fc
;
863 BT_ASSERT(orig_name
);
864 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
866 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
867 fc
->members
->len
- 1);
868 _ctf_named_field_class_init(named_fc
);
869 g_string_assign(named_fc
->orig_name
, orig_name
);
870 _ctf_named_field_class_unescape_orig_name(named_fc
);
871 named_fc
->fc
= member_fc
;
873 if (member_fc
->alignment
> fc
->base
.alignment
) {
874 fc
->base
.alignment
= member_fc
->alignment
;
879 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
880 struct ctf_field_class_variant
*fc
, uint64_t index
)
883 BT_ASSERT(index
< fc
->options
->len
);
884 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
889 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
890 struct ctf_field_class_variant
*fc
, const char *name
)
893 struct ctf_named_field_class
*ret_named_fc
= NULL
;
898 for (i
= 0; i
< fc
->options
->len
; i
++) {
899 struct ctf_named_field_class
*named_fc
=
900 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
902 if (strcmp(name
, named_fc
->name
->str
) == 0) {
903 ret_named_fc
= named_fc
;
913 struct ctf_field_class_variant_range
*
914 ctf_field_class_variant_borrow_range_by_index(
915 struct ctf_field_class_variant
*fc
, uint64_t index
)
918 BT_ASSERT(index
< fc
->ranges
->len
);
919 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
924 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
925 const char *orig_name
, struct ctf_field_class
*option_fc
)
927 struct ctf_named_field_class
*named_fc
;
930 BT_ASSERT(orig_name
);
931 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
933 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
934 fc
->options
->len
- 1);
935 _ctf_named_field_class_init(named_fc
);
936 g_string_assign(named_fc
->orig_name
, orig_name
);
937 _ctf_named_field_class_unescape_orig_name(named_fc
);
938 named_fc
->fc
= option_fc
;
942 void ctf_field_class_variant_set_tag_field_class(
943 struct ctf_field_class_variant
*fc
,
944 struct ctf_field_class_enum
*tag_fc
)
952 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
954 struct ctf_named_field_class
*named_fc
=
955 ctf_field_class_variant_borrow_option_by_index(
957 struct ctf_field_class_enum_mapping
*mapping
;
959 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
960 tag_fc
, named_fc
->orig_name
->str
);
965 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
967 struct ctf_range
*range
=
968 ctf_field_class_enum_mapping_borrow_range_by_index(
970 struct ctf_field_class_variant_range var_range
;
972 var_range
.range
= *range
;
973 var_range
.option_index
= option_i
;
974 g_array_append_val(fc
->ranges
, var_range
);
980 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
981 struct ctf_field_class
*comp_fc
, uint64_t index
)
983 struct ctf_field_class
*fc
= NULL
;
985 switch (comp_fc
->type
) {
986 case CTF_FIELD_CLASS_TYPE_STRUCT
:
988 struct ctf_named_field_class
*named_fc
=
989 ctf_field_class_struct_borrow_member_by_index(
990 (void *) comp_fc
, index
);
996 case CTF_FIELD_CLASS_TYPE_VARIANT
:
998 struct ctf_named_field_class
*named_fc
=
999 ctf_field_class_variant_borrow_option_by_index(
1000 (void *) comp_fc
, index
);
1002 BT_ASSERT(named_fc
);
1006 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1007 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1009 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1011 fc
= array_fc
->elem_fc
;
1022 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1024 uint64_t field_count
;
1027 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1029 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1031 field_count
= struct_fc
->members
->len
;
1034 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1036 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1038 field_count
= var_fc
->options
->len
;
1041 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1042 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1044 * Array and sequence types always contain a single
1045 * member (the element type).
1057 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1058 struct ctf_field_class
*fc
, const char *orig_name
)
1060 int64_t ret_index
= -1;
1064 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1066 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1068 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1069 struct ctf_named_field_class
*named_fc
=
1070 ctf_field_class_struct_borrow_member_by_index(
1073 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1074 ret_index
= (int64_t) i
;
1081 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1083 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1085 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1086 struct ctf_named_field_class
*named_fc
=
1087 ctf_field_class_variant_borrow_option_by_index(
1090 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1091 ret_index
= (int64_t) i
;
1107 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1110 g_array_append_val(fp
->path
, index
);
1114 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1118 BT_ASSERT(index
< fp
->path
->len
);
1119 return g_array_index(fp
->path
, int64_t, index
);
1123 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1126 g_array_set_size(fp
->path
, 0);
1130 const char *ctf_scope_string(enum ctf_scope scope
)
1133 case CTF_SCOPE_PACKET_HEADER
:
1134 return "PACKET_HEADER";
1135 case CTF_SCOPE_PACKET_CONTEXT
:
1136 return "PACKET_CONTEXT";
1137 case CTF_SCOPE_EVENT_HEADER
:
1138 return "EVENT_HEADER";
1139 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1140 return "EVENT_COMMON_CONTEXT";
1141 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1142 return "EVENT_SPECIFIC_CONTEXT";
1143 case CTF_SCOPE_EVENT_PAYLOAD
:
1144 return "EVENT_PAYLOAD";
1151 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1153 GString
*str
= g_string_new(NULL
);
1162 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1164 for (i
= 0; i
< path
->path
->len
; i
++) {
1165 g_string_append_printf(str
, ", %" PRId64
,
1166 ctf_field_path_borrow_index_by_index(path
, i
));
1169 g_string_append(str
, "]");
1176 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1177 struct ctf_field_path
*field_path
,
1178 struct ctf_trace_class
*tc
,
1179 struct ctf_stream_class
*sc
,
1180 struct ctf_event_class
*ec
)
1183 struct ctf_field_class
*fc
;
1185 switch (field_path
->root
) {
1186 case CTF_SCOPE_PACKET_HEADER
:
1187 fc
= tc
->packet_header_fc
;
1189 case CTF_SCOPE_PACKET_CONTEXT
:
1190 fc
= sc
->packet_context_fc
;
1192 case CTF_SCOPE_EVENT_HEADER
:
1193 fc
= sc
->event_header_fc
;
1195 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1196 fc
= sc
->event_common_context_fc
;
1198 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1199 fc
= ec
->spec_context_fc
;
1201 case CTF_SCOPE_EVENT_PAYLOAD
:
1202 fc
= ec
->payload_fc
;
1210 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1211 int64_t child_index
=
1212 ctf_field_path_borrow_index_by_index(field_path
, i
);
1213 struct ctf_field_class
*child_fc
=
1214 ctf_field_class_compound_borrow_field_class_by_index(
1216 BT_ASSERT(child_fc
);
1225 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1228 void ctf_field_class_bit_array_copy_content(
1229 struct ctf_field_class_bit_array
*dst_fc
,
1230 struct ctf_field_class_bit_array
*src_fc
)
1234 dst_fc
->byte_order
= src_fc
->byte_order
;
1235 dst_fc
->size
= src_fc
->size
;
1239 void ctf_field_class_int_copy_content(
1240 struct ctf_field_class_int
*dst_fc
,
1241 struct ctf_field_class_int
*src_fc
)
1243 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1244 dst_fc
->meaning
= src_fc
->meaning
;
1245 dst_fc
->is_signed
= src_fc
->is_signed
;
1246 dst_fc
->disp_base
= src_fc
->disp_base
;
1247 dst_fc
->encoding
= src_fc
->encoding
;
1248 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1249 dst_fc
->storing_index
= src_fc
->storing_index
;
1253 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1254 struct ctf_field_class_int
*fc
)
1256 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1259 ctf_field_class_int_copy_content(copy_fc
, fc
);
1264 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1265 struct ctf_field_class_enum
*fc
)
1267 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1271 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1273 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1276 struct ctf_field_class_enum_mapping
*mapping
=
1277 &g_array_index(fc
->mappings
,
1278 struct ctf_field_class_enum_mapping
, i
);
1280 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1281 struct ctf_range
*range
=
1282 &g_array_index(mapping
->ranges
,
1283 struct ctf_range
, range_i
);
1285 ctf_field_class_enum_map_range(copy_fc
,
1286 mapping
->label
->str
, range
->lower
.u
,
1295 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1296 struct ctf_field_class_float
*fc
)
1298 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1301 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1306 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1307 struct ctf_field_class_string
*fc
)
1309 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1316 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1317 struct ctf_field_class_struct
*fc
)
1319 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1324 for (i
= 0; i
< fc
->members
->len
; i
++) {
1325 struct ctf_named_field_class
*named_fc
=
1326 &g_array_index(fc
->members
,
1327 struct ctf_named_field_class
, i
);
1329 ctf_field_class_struct_append_member(copy_fc
,
1330 named_fc
->name
->str
,
1331 ctf_field_class_copy(named_fc
->fc
));
1338 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1339 struct ctf_field_path
*src_fp
)
1345 dst_fp
->root
= src_fp
->root
;
1346 ctf_field_path_clear(dst_fp
);
1348 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1349 int64_t index
= ctf_field_path_borrow_index_by_index(
1352 ctf_field_path_append_index(dst_fp
, index
);
1357 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1358 struct ctf_field_class_variant
*fc
)
1360 struct ctf_field_class_variant
*copy_fc
=
1361 ctf_field_class_variant_create();
1366 for (i
= 0; i
< fc
->options
->len
; i
++) {
1367 struct ctf_named_field_class
*named_fc
=
1368 &g_array_index(fc
->options
,
1369 struct ctf_named_field_class
, i
);
1371 ctf_field_class_variant_append_option(copy_fc
,
1372 named_fc
->name
->str
,
1373 ctf_field_class_copy(named_fc
->fc
));
1376 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1377 struct ctf_field_class_variant_range
*range
=
1378 &g_array_index(fc
->ranges
,
1379 struct ctf_field_class_variant_range
, i
);
1381 g_array_append_val(copy_fc
->ranges
, *range
);
1384 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1385 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1386 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1391 void ctf_field_class_array_base_copy_content(
1392 struct ctf_field_class_array_base
*dst_fc
,
1393 struct ctf_field_class_array_base
*src_fc
)
1397 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1398 dst_fc
->is_text
= src_fc
->is_text
;
1402 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1403 struct ctf_field_class_array
*fc
)
1405 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1408 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1409 copy_fc
->length
= fc
->length
;
1414 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1415 struct ctf_field_class_sequence
*fc
)
1417 struct ctf_field_class_sequence
*copy_fc
=
1418 ctf_field_class_sequence_create();
1421 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1422 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1423 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1424 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1429 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1431 struct ctf_field_class
*copy_fc
= NULL
;
1438 * Translation should not have happened yet.
1440 BT_ASSERT(!fc
->ir_fc
);
1443 case CTF_FIELD_CLASS_TYPE_INT
:
1444 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1446 case CTF_FIELD_CLASS_TYPE_ENUM
:
1447 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1449 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1450 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1452 case CTF_FIELD_CLASS_TYPE_STRING
:
1453 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1455 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1456 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1458 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1459 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1461 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1462 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1464 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1465 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1471 copy_fc
->type
= fc
->type
;
1472 copy_fc
->alignment
= fc
->alignment
;
1473 copy_fc
->in_ir
= fc
->in_ir
;
1480 struct ctf_event_class
*ctf_event_class_create(void)
1482 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1485 ec
->name
= g_string_new(NULL
);
1486 BT_ASSERT(ec
->name
);
1487 ec
->emf_uri
= g_string_new(NULL
);
1488 BT_ASSERT(ec
->emf_uri
);
1489 ec
->is_log_level_set
= false;
1494 void ctf_event_class_set_log_level(struct ctf_event_class
*ec
,
1495 enum bt_event_class_log_level log_level
)
1498 ec
->log_level
= log_level
;
1499 ec
->is_log_level_set
= true;
1503 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1510 g_string_free(ec
->name
, TRUE
);
1514 g_string_free(ec
->emf_uri
, TRUE
);
1517 ctf_field_class_destroy(ec
->spec_context_fc
);
1518 ctf_field_class_destroy(ec
->payload_fc
);
1523 struct ctf_stream_class
*ctf_stream_class_create(void)
1525 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1528 sc
->event_classes
= g_ptr_array_new_with_free_func(
1529 (GDestroyNotify
) ctf_event_class_destroy
);
1530 BT_ASSERT(sc
->event_classes
);
1531 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1533 BT_ASSERT(sc
->event_classes_by_id
);
1538 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1544 if (sc
->event_classes
) {
1545 g_ptr_array_free(sc
->event_classes
, TRUE
);
1548 if (sc
->event_classes_by_id
) {
1549 g_hash_table_destroy(sc
->event_classes_by_id
);
1552 ctf_field_class_destroy(sc
->packet_context_fc
);
1553 ctf_field_class_destroy(sc
->event_header_fc
);
1554 ctf_field_class_destroy(sc
->event_common_context_fc
);
1559 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1560 struct ctf_event_class
*ec
)
1562 g_ptr_array_add(sc
->event_classes
, ec
);
1563 g_hash_table_insert(sc
->event_classes_by_id
,
1564 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1568 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1569 struct ctf_stream_class
*sc
, uint64_t type
)
1572 return g_hash_table_lookup(sc
->event_classes_by_id
,
1573 GUINT_TO_POINTER((guint
) type
));
1577 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1580 entry
->name
= g_string_new(NULL
);
1581 BT_ASSERT(entry
->name
);
1582 entry
->value
.str
= g_string_new(NULL
);
1583 BT_ASSERT(entry
->value
.str
);
1587 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1592 g_string_free(entry
->name
, TRUE
);
1595 if (entry
->value
.str
) {
1596 g_string_free(entry
->value
.str
, TRUE
);
1601 struct ctf_clock_class
*ctf_clock_class_create(void)
1603 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1606 cc
->name
= g_string_new(NULL
);
1607 BT_ASSERT(cc
->name
);
1608 cc
->description
= g_string_new(NULL
);
1609 BT_ASSERT(cc
->description
);
1614 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1621 g_string_free(cc
->name
, TRUE
);
1624 if (cc
->description
) {
1625 g_string_free(cc
->description
, TRUE
);
1628 bt_clock_class_put_ref(cc
->ir_cc
);
1633 struct ctf_trace_class
*ctf_trace_class_create(void)
1635 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1638 tc
->default_byte_order
= -1;
1639 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1640 (GDestroyNotify
) ctf_clock_class_destroy
);
1641 BT_ASSERT(tc
->clock_classes
);
1642 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1643 (GDestroyNotify
) ctf_stream_class_destroy
);
1644 BT_ASSERT(tc
->stream_classes
);
1645 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1646 sizeof(struct ctf_trace_class_env_entry
));
1651 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1657 ctf_field_class_destroy(tc
->packet_header_fc
);
1659 if (tc
->clock_classes
) {
1660 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1663 if (tc
->stream_classes
) {
1664 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1667 if (tc
->env_entries
) {
1670 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1671 struct ctf_trace_class_env_entry
*entry
=
1672 &g_array_index(tc
->env_entries
,
1673 struct ctf_trace_class_env_entry
, i
);
1675 _ctf_trace_class_env_entry_fini(entry
);
1678 g_array_free(tc
->env_entries
, TRUE
);
1685 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1686 const char *name
, enum ctf_trace_class_env_entry_type type
,
1687 const char *str_value
, int64_t i_value
)
1689 struct ctf_trace_class_env_entry
*entry
;
1693 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1695 entry
= &g_array_index(tc
->env_entries
,
1696 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1698 _ctf_trace_class_env_entry_init(entry
);
1699 g_string_assign(entry
->name
, name
);
1702 g_string_assign(entry
->value
.str
, str_value
);
1705 entry
->value
.i
= i_value
;
1709 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1710 struct ctf_trace_class
*tc
, uint64_t id
)
1713 struct ctf_stream_class
*ret_sc
= NULL
;
1717 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1718 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1731 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1732 struct ctf_trace_class
*tc
, const char *name
)
1735 struct ctf_clock_class
*ret_cc
= NULL
;
1740 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1741 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1743 BT_ASSERT(cc
->name
);
1744 if (strcmp(cc
->name
->str
, name
) == 0) {
1755 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1756 struct ctf_trace_class
*tc
, uint64_t index
)
1759 BT_ASSERT(index
< tc
->env_entries
->len
);
1760 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1765 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1766 struct ctf_trace_class
*tc
, const char *name
)
1768 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1774 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1775 struct ctf_trace_class_env_entry
*env_entry
=
1776 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1778 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1779 ret_entry
= env_entry
;
1788 #endif /* _CTF_META_H */