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"
27 enum ctf_field_class_type
{
28 CTF_FIELD_CLASS_TYPE_INT
,
29 CTF_FIELD_CLASS_TYPE_ENUM
,
30 CTF_FIELD_CLASS_TYPE_FLOAT
,
31 CTF_FIELD_CLASS_TYPE_STRING
,
32 CTF_FIELD_CLASS_TYPE_STRUCT
,
33 CTF_FIELD_CLASS_TYPE_ARRAY
,
34 CTF_FIELD_CLASS_TYPE_SEQUENCE
,
35 CTF_FIELD_CLASS_TYPE_VARIANT
,
38 enum ctf_field_class_meaning
{
39 CTF_FIELD_CLASS_MEANING_NONE
,
40 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
,
41 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
,
42 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
,
43 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
,
44 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
,
45 CTF_FIELD_CLASS_MEANING_MAGIC
,
46 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
,
47 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
,
48 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
,
49 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
,
50 CTF_FIELD_CLASS_MEANING_UUID
,
54 CTF_BYTE_ORDER_UNKNOWN
,
55 CTF_BYTE_ORDER_DEFAULT
,
56 CTF_BYTE_ORDER_LITTLE
,
66 CTF_SCOPE_PACKET_UNKNOWN
= -1,
67 CTF_SCOPE_PACKET_HEADER
= 0,
68 CTF_SCOPE_PACKET_CONTEXT
,
69 CTF_SCOPE_EVENT_HEADER
,
70 CTF_SCOPE_EVENT_COMMON_CONTEXT
,
71 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
,
72 CTF_SCOPE_EVENT_PAYLOAD
,
75 struct ctf_clock_class
{
80 int64_t offset_seconds
;
81 uint64_t offset_cycles
;
86 /* Weak, set during translation */
87 bt_clock_class
*ir_cc
;
90 struct ctf_field_class
{
91 enum ctf_field_class_type type
;
92 unsigned int alignment
;
96 /* Weak, set during translation. NULL if `in_ir` is false below. */
97 bt_field_class
*ir_fc
;
100 struct ctf_field_class_bit_array
{
101 struct ctf_field_class base
;
102 enum ctf_byte_order byte_order
;
106 struct ctf_field_class_int
{
107 struct ctf_field_class_bit_array base
;
108 enum ctf_field_class_meaning meaning
;
110 bt_field_class_integer_preferred_display_base disp_base
;
111 enum ctf_encoding encoding
;
112 int64_t storing_index
;
115 struct ctf_clock_class
*mapped_clock_class
;
130 struct ctf_field_class_enum_mapping
{
133 /* Array of `struct ctf_range` */
137 struct ctf_field_class_enum
{
138 struct ctf_field_class_int base
;
140 /* Array of `struct ctf_field_class_enum_mapping` */
144 struct ctf_field_class_float
{
145 struct ctf_field_class_bit_array base
;
148 struct ctf_field_class_string
{
149 struct ctf_field_class base
;
150 enum ctf_encoding encoding
;
153 struct ctf_named_field_class
{
154 /* Original name which can include a leading `_` */
157 /* Name as translated to trace IR (leading `_` removed) */
161 struct ctf_field_class
*fc
;
164 struct ctf_field_class_struct
{
165 struct ctf_field_class base
;
167 /* Array of `struct ctf_named_field_class` */
171 struct ctf_field_path
{
174 /* Array of `int64_t` */
178 struct ctf_field_class_variant_range
{
179 struct ctf_range range
;
180 uint64_t option_index
;
183 struct ctf_field_class_variant
{
184 struct ctf_field_class base
;
186 struct ctf_field_path tag_path
;
187 uint64_t stored_tag_index
;
189 /* Array of `struct ctf_named_field_class` */
192 /* Array of `struct ctf_field_class_variant_range` */
196 struct ctf_field_class_enum
*tag_fc
;
199 struct ctf_field_class_array_base
{
200 struct ctf_field_class base
;
201 struct ctf_field_class
*elem_fc
;
205 struct ctf_field_class_array
{
206 struct ctf_field_class_array_base base
;
207 enum ctf_field_class_meaning meaning
;
211 struct ctf_field_class_sequence
{
212 struct ctf_field_class_array_base base
;
214 struct ctf_field_path length_path
;
215 uint64_t stored_length_index
;
218 struct ctf_field_class_int
*length_fc
;
221 struct ctf_event_class
{
225 bt_event_class_log_level log_level
;
227 bool is_log_level_set
;
230 struct ctf_field_class
*spec_context_fc
;
233 struct ctf_field_class
*payload_fc
;
235 /* Weak, set during translation */
236 bt_event_class
*ir_ec
;
239 struct ctf_stream_class
{
242 bool packets_have_ts_begin
;
243 bool packets_have_ts_end
;
244 bool has_discarded_events
;
245 bool has_discarded_packets
;
246 bool discarded_events_have_default_cs
;
247 bool discarded_packets_have_default_cs
;
250 struct ctf_field_class
*packet_context_fc
;
253 struct ctf_field_class
*event_header_fc
;
256 struct ctf_field_class
*event_common_context_fc
;
258 /* Array of `struct ctf_event_class *`, owned by this */
259 GPtrArray
*event_classes
;
262 * Hash table mapping event class IDs to `struct ctf_event_class *`,
265 GHashTable
*event_classes_by_id
;
268 struct ctf_clock_class
*default_clock_class
;
270 /* Weak, set during translation */
271 bt_stream_class
*ir_sc
;
274 enum ctf_trace_class_env_entry_type
{
275 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
276 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
279 struct ctf_trace_class_env_entry
{
280 enum ctf_trace_class_env_entry_type type
;
289 struct ctf_trace_class
{
294 enum ctf_byte_order default_byte_order
;
297 struct ctf_field_class
*packet_header_fc
;
299 uint64_t stored_value_count
;
301 /* Array of `struct ctf_clock_class *` (owned by this) */
302 GPtrArray
*clock_classes
;
304 /* Array of `struct ctf_stream_class *` */
305 GPtrArray
*stream_classes
;
307 /* Array of `struct ctf_trace_class_env_entry` */
312 /* Weak, set during translation */
313 bt_trace_class
*ir_tc
;
317 bool lttng_event_after_packet
;
318 bool barectf_event_before_packet
;
323 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
326 void _ctf_field_class_init(struct ctf_field_class
*fc
,
327 enum ctf_field_class_type type
, unsigned int alignment
)
331 fc
->alignment
= alignment
;
336 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
337 enum ctf_field_class_type type
)
339 _ctf_field_class_init((void *) fc
, type
, 1);
343 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
344 enum ctf_field_class_type type
)
346 _ctf_field_class_bit_array_init((void *) fc
, type
);
347 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
348 fc
->storing_index
= -1;
352 void ctf_field_path_init(struct ctf_field_path
*field_path
)
354 BT_ASSERT(field_path
);
355 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
356 BT_ASSERT(field_path
->path
);
360 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
362 BT_ASSERT(field_path
);
364 if (field_path
->path
) {
365 g_array_free(field_path
->path
, TRUE
);
370 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
373 named_fc
->name
= g_string_new(NULL
);
374 BT_ASSERT(named_fc
->name
);
375 named_fc
->orig_name
= g_string_new(NULL
);
376 BT_ASSERT(named_fc
->orig_name
);
380 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
384 if (named_fc
->name
) {
385 g_string_free(named_fc
->name
, TRUE
);
388 if (named_fc
->orig_name
) {
389 g_string_free(named_fc
->orig_name
, TRUE
);
392 ctf_field_class_destroy(named_fc
->fc
);
396 void _ctf_field_class_enum_mapping_init(
397 struct ctf_field_class_enum_mapping
*mapping
)
400 mapping
->label
= g_string_new(NULL
);
401 BT_ASSERT(mapping
->label
);
402 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
403 BT_ASSERT(mapping
->ranges
);
407 void _ctf_field_class_enum_mapping_fini(
408 struct ctf_field_class_enum_mapping
*mapping
)
412 if (mapping
->label
) {
413 g_string_free(mapping
->label
, TRUE
);
416 if (mapping
->ranges
) {
417 g_array_free(mapping
->ranges
, TRUE
);
422 struct ctf_field_class_int
*ctf_field_class_int_create(void)
424 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
427 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
432 struct ctf_field_class_float
*ctf_field_class_float_create(void)
434 struct ctf_field_class_float
*fc
=
435 g_new0(struct ctf_field_class_float
, 1);
438 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
443 struct ctf_field_class_string
*ctf_field_class_string_create(void)
445 struct ctf_field_class_string
*fc
=
446 g_new0(struct ctf_field_class_string
, 1);
449 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
454 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
456 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
459 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
460 fc
->mappings
= g_array_new(FALSE
, TRUE
,
461 sizeof(struct ctf_field_class_enum_mapping
));
462 BT_ASSERT(fc
->mappings
);
467 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
469 struct ctf_field_class_struct
*fc
=
470 g_new0(struct ctf_field_class_struct
, 1);
473 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
474 fc
->members
= g_array_new(FALSE
, TRUE
,
475 sizeof(struct ctf_named_field_class
));
476 BT_ASSERT(fc
->members
);
477 fc
->base
.is_compound
= true;
482 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
484 struct ctf_field_class_variant
*fc
=
485 g_new0(struct ctf_field_class_variant
, 1);
488 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
489 fc
->options
= g_array_new(FALSE
, TRUE
,
490 sizeof(struct ctf_named_field_class
));
491 BT_ASSERT(fc
->options
);
492 fc
->ranges
= g_array_new(FALSE
, TRUE
,
493 sizeof(struct ctf_field_class_variant_range
));
494 BT_ASSERT(fc
->ranges
);
495 fc
->tag_ref
= g_string_new(NULL
);
496 BT_ASSERT(fc
->tag_ref
);
497 ctf_field_path_init(&fc
->tag_path
);
498 fc
->base
.is_compound
= true;
503 struct ctf_field_class_array
*ctf_field_class_array_create(void)
505 struct ctf_field_class_array
*fc
=
506 g_new0(struct ctf_field_class_array
, 1);
509 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
510 fc
->base
.base
.is_compound
= true;
515 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
517 struct ctf_field_class_sequence
*fc
=
518 g_new0(struct ctf_field_class_sequence
, 1);
521 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
522 fc
->length_ref
= g_string_new(NULL
);
523 BT_ASSERT(fc
->length_ref
);
524 ctf_field_path_init(&fc
->length_path
);
525 fc
->base
.base
.is_compound
= true;
530 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
537 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
544 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
545 struct ctf_field_class_enum_mapping
*mapping
=
546 &g_array_index(fc
->mappings
,
547 struct ctf_field_class_enum_mapping
, i
);
549 _ctf_field_class_enum_mapping_fini(mapping
);
552 g_array_free(fc
->mappings
, TRUE
);
559 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
566 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
573 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
580 for (i
= 0; i
< fc
->members
->len
; i
++) {
581 struct ctf_named_field_class
*named_fc
=
582 &g_array_index(fc
->members
,
583 struct ctf_named_field_class
, i
);
585 _ctf_named_field_class_fini(named_fc
);
588 g_array_free(fc
->members
, TRUE
);
595 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
598 ctf_field_class_destroy(fc
->elem_fc
);
602 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
605 _ctf_field_class_array_base_fini((void *) fc
);
610 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
613 _ctf_field_class_array_base_fini((void *) fc
);
615 if (fc
->length_ref
) {
616 g_string_free(fc
->length_ref
, TRUE
);
619 ctf_field_path_fini(&fc
->length_path
);
624 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
631 for (i
= 0; i
< fc
->options
->len
; i
++) {
632 struct ctf_named_field_class
*named_fc
=
633 &g_array_index(fc
->options
,
634 struct ctf_named_field_class
, i
);
636 _ctf_named_field_class_fini(named_fc
);
639 g_array_free(fc
->options
, TRUE
);
643 g_array_free(fc
->ranges
, TRUE
);
647 g_string_free(fc
->tag_ref
, TRUE
);
650 ctf_field_path_fini(&fc
->tag_path
);
655 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
662 case CTF_FIELD_CLASS_TYPE_INT
:
663 _ctf_field_class_int_destroy((void *) fc
);
665 case CTF_FIELD_CLASS_TYPE_ENUM
:
666 _ctf_field_class_enum_destroy((void *) fc
);
668 case CTF_FIELD_CLASS_TYPE_FLOAT
:
669 _ctf_field_class_float_destroy((void *) fc
);
671 case CTF_FIELD_CLASS_TYPE_STRING
:
672 _ctf_field_class_string_destroy((void *) fc
);
674 case CTF_FIELD_CLASS_TYPE_STRUCT
:
675 _ctf_field_class_struct_destroy((void *) fc
);
677 case CTF_FIELD_CLASS_TYPE_ARRAY
:
678 _ctf_field_class_array_destroy((void *) fc
);
680 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
681 _ctf_field_class_sequence_destroy((void *) fc
);
683 case CTF_FIELD_CLASS_TYPE_VARIANT
:
684 _ctf_field_class_variant_destroy((void *) fc
);
692 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
693 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
695 BT_ASSERT_DBG(mapping
);
696 BT_ASSERT_DBG(index
< mapping
->ranges
->len
);
697 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
701 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
702 struct ctf_field_class_enum
*fc
, uint64_t index
)
705 BT_ASSERT_DBG(index
< fc
->mappings
->len
);
706 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
711 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
712 struct ctf_field_class_enum
*fc
, const char *label
)
714 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
718 BT_ASSERT_DBG(label
);
720 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
721 struct ctf_field_class_enum_mapping
*mapping
=
722 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
724 if (strcmp(mapping
->label
->str
, label
) == 0) {
725 ret_mapping
= mapping
;
735 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
736 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
738 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
739 struct ctf_range range
= {
748 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
749 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
752 if (strcmp(mapping
->label
->str
, label
) == 0) {
757 if (i
== fc
->mappings
->len
) {
762 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
763 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
764 fc
, fc
->mappings
->len
- 1);
765 _ctf_field_class_enum_mapping_init(mapping
);
766 g_string_assign(mapping
->label
, label
);
769 g_array_append_val(mapping
->ranges
, range
);
773 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
774 struct ctf_field_class_struct
*fc
, uint64_t index
)
777 BT_ASSERT_DBG(index
< fc
->members
->len
);
778 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
783 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
784 struct ctf_field_class_struct
*fc
, const char *name
)
787 struct ctf_named_field_class
*ret_named_fc
= NULL
;
792 for (i
= 0; i
< fc
->members
->len
; i
++) {
793 struct ctf_named_field_class
*named_fc
=
794 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
796 if (strcmp(name
, named_fc
->name
->str
) == 0) {
797 ret_named_fc
= named_fc
;
807 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
808 struct ctf_field_class_struct
*struct_fc
, const char *name
)
810 struct ctf_named_field_class
*named_fc
= NULL
;
811 struct ctf_field_class
*fc
= NULL
;
817 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
829 struct ctf_field_class_int
*
830 ctf_field_class_struct_borrow_member_int_field_class_by_name(
831 struct ctf_field_class_struct
*struct_fc
, const char *name
)
833 struct ctf_field_class_int
*int_fc
= NULL
;
836 ctf_field_class_struct_borrow_member_field_class_by_name(
842 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
843 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
853 void _ctf_named_field_class_unescape_orig_name(
854 struct ctf_named_field_class
*named_fc
)
856 const char *name
= named_fc
->orig_name
->str
;
858 if (name
[0] == '_') {
862 g_string_assign(named_fc
->name
, name
);
866 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
867 const char *orig_name
, struct ctf_field_class
*member_fc
)
869 struct ctf_named_field_class
*named_fc
;
872 BT_ASSERT(orig_name
);
873 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
875 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
876 fc
->members
->len
- 1);
877 _ctf_named_field_class_init(named_fc
);
878 g_string_assign(named_fc
->orig_name
, orig_name
);
879 _ctf_named_field_class_unescape_orig_name(named_fc
);
880 named_fc
->fc
= member_fc
;
882 if (member_fc
->alignment
> fc
->base
.alignment
) {
883 fc
->base
.alignment
= member_fc
->alignment
;
888 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
889 struct ctf_field_class_variant
*fc
, uint64_t index
)
892 BT_ASSERT_DBG(index
< fc
->options
->len
);
893 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
898 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
899 struct ctf_field_class_variant
*fc
, const char *name
)
902 struct ctf_named_field_class
*ret_named_fc
= NULL
;
907 for (i
= 0; i
< fc
->options
->len
; i
++) {
908 struct ctf_named_field_class
*named_fc
=
909 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
911 if (strcmp(name
, named_fc
->name
->str
) == 0) {
912 ret_named_fc
= named_fc
;
922 struct ctf_field_class_variant_range
*
923 ctf_field_class_variant_borrow_range_by_index(
924 struct ctf_field_class_variant
*fc
, uint64_t index
)
927 BT_ASSERT_DBG(index
< fc
->ranges
->len
);
928 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
933 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
934 const char *orig_name
, struct ctf_field_class
*option_fc
)
936 struct ctf_named_field_class
*named_fc
;
939 BT_ASSERT(orig_name
);
940 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
942 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
943 fc
->options
->len
- 1);
944 _ctf_named_field_class_init(named_fc
);
945 g_string_assign(named_fc
->orig_name
, orig_name
);
946 _ctf_named_field_class_unescape_orig_name(named_fc
);
947 named_fc
->fc
= option_fc
;
951 void ctf_field_class_variant_set_tag_field_class(
952 struct ctf_field_class_variant
*fc
,
953 struct ctf_field_class_enum
*tag_fc
)
961 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
963 struct ctf_named_field_class
*named_fc
=
964 ctf_field_class_variant_borrow_option_by_index(
966 struct ctf_field_class_enum_mapping
*mapping
;
968 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
969 tag_fc
, named_fc
->orig_name
->str
);
974 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
976 struct ctf_range
*range
=
977 ctf_field_class_enum_mapping_borrow_range_by_index(
979 struct ctf_field_class_variant_range var_range
;
981 var_range
.range
= *range
;
982 var_range
.option_index
= option_i
;
983 g_array_append_val(fc
->ranges
, var_range
);
989 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
990 struct ctf_field_class
*comp_fc
, uint64_t index
)
992 struct ctf_field_class
*fc
= NULL
;
994 switch (comp_fc
->type
) {
995 case CTF_FIELD_CLASS_TYPE_STRUCT
:
997 struct ctf_named_field_class
*named_fc
=
998 ctf_field_class_struct_borrow_member_by_index(
999 (void *) comp_fc
, index
);
1001 BT_ASSERT_DBG(named_fc
);
1005 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1007 struct ctf_named_field_class
*named_fc
=
1008 ctf_field_class_variant_borrow_option_by_index(
1009 (void *) comp_fc
, index
);
1011 BT_ASSERT_DBG(named_fc
);
1015 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1016 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1018 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1020 fc
= array_fc
->elem_fc
;
1031 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1033 uint64_t field_count
;
1036 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1038 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1040 field_count
= struct_fc
->members
->len
;
1043 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1045 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1047 field_count
= var_fc
->options
->len
;
1050 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1051 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1053 * Array and sequence types always contain a single
1054 * member (the element type).
1066 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1067 struct ctf_field_class
*fc
, const char *orig_name
)
1069 int64_t ret_index
= -1;
1073 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1075 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1077 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1078 struct ctf_named_field_class
*named_fc
=
1079 ctf_field_class_struct_borrow_member_by_index(
1082 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1083 ret_index
= (int64_t) i
;
1090 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1092 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1094 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1095 struct ctf_named_field_class
*named_fc
=
1096 ctf_field_class_variant_borrow_option_by_index(
1099 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1100 ret_index
= (int64_t) i
;
1116 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1119 g_array_append_val(fp
->path
, index
);
1123 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1127 BT_ASSERT_DBG(index
< fp
->path
->len
);
1128 return g_array_index(fp
->path
, int64_t, index
);
1132 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1135 g_array_set_size(fp
->path
, 0);
1139 const char *ctf_scope_string(enum ctf_scope scope
)
1142 case CTF_SCOPE_PACKET_HEADER
:
1143 return "PACKET_HEADER";
1144 case CTF_SCOPE_PACKET_CONTEXT
:
1145 return "PACKET_CONTEXT";
1146 case CTF_SCOPE_EVENT_HEADER
:
1147 return "EVENT_HEADER";
1148 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1149 return "EVENT_COMMON_CONTEXT";
1150 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1151 return "EVENT_SPECIFIC_CONTEXT";
1152 case CTF_SCOPE_EVENT_PAYLOAD
:
1153 return "EVENT_PAYLOAD";
1160 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1162 GString
*str
= g_string_new(NULL
);
1171 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1173 for (i
= 0; i
< path
->path
->len
; i
++) {
1174 g_string_append_printf(str
, ", %" PRId64
,
1175 ctf_field_path_borrow_index_by_index(path
, i
));
1178 g_string_append(str
, "]");
1185 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1186 struct ctf_field_path
*field_path
,
1187 struct ctf_trace_class
*tc
,
1188 struct ctf_stream_class
*sc
,
1189 struct ctf_event_class
*ec
)
1192 struct ctf_field_class
*fc
;
1194 switch (field_path
->root
) {
1195 case CTF_SCOPE_PACKET_HEADER
:
1196 fc
= tc
->packet_header_fc
;
1198 case CTF_SCOPE_PACKET_CONTEXT
:
1199 fc
= sc
->packet_context_fc
;
1201 case CTF_SCOPE_EVENT_HEADER
:
1202 fc
= sc
->event_header_fc
;
1204 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1205 fc
= sc
->event_common_context_fc
;
1207 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1208 fc
= ec
->spec_context_fc
;
1210 case CTF_SCOPE_EVENT_PAYLOAD
:
1211 fc
= ec
->payload_fc
;
1219 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1220 int64_t child_index
=
1221 ctf_field_path_borrow_index_by_index(field_path
, i
);
1222 struct ctf_field_class
*child_fc
=
1223 ctf_field_class_compound_borrow_field_class_by_index(
1225 BT_ASSERT_DBG(child_fc
);
1234 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1237 void ctf_field_class_bit_array_copy_content(
1238 struct ctf_field_class_bit_array
*dst_fc
,
1239 struct ctf_field_class_bit_array
*src_fc
)
1243 dst_fc
->byte_order
= src_fc
->byte_order
;
1244 dst_fc
->size
= src_fc
->size
;
1248 void ctf_field_class_int_copy_content(
1249 struct ctf_field_class_int
*dst_fc
,
1250 struct ctf_field_class_int
*src_fc
)
1252 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1253 dst_fc
->meaning
= src_fc
->meaning
;
1254 dst_fc
->is_signed
= src_fc
->is_signed
;
1255 dst_fc
->disp_base
= src_fc
->disp_base
;
1256 dst_fc
->encoding
= src_fc
->encoding
;
1257 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1258 dst_fc
->storing_index
= src_fc
->storing_index
;
1262 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1263 struct ctf_field_class_int
*fc
)
1265 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1268 ctf_field_class_int_copy_content(copy_fc
, fc
);
1273 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1274 struct ctf_field_class_enum
*fc
)
1276 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1280 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1282 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1285 struct ctf_field_class_enum_mapping
*mapping
=
1286 &g_array_index(fc
->mappings
,
1287 struct ctf_field_class_enum_mapping
, i
);
1289 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1290 struct ctf_range
*range
=
1291 &g_array_index(mapping
->ranges
,
1292 struct ctf_range
, range_i
);
1294 ctf_field_class_enum_map_range(copy_fc
,
1295 mapping
->label
->str
, range
->lower
.u
,
1304 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1305 struct ctf_field_class_float
*fc
)
1307 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1310 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1315 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1316 struct ctf_field_class_string
*fc
)
1318 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1325 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1326 struct ctf_field_class_struct
*fc
)
1328 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1333 for (i
= 0; i
< fc
->members
->len
; i
++) {
1334 struct ctf_named_field_class
*named_fc
=
1335 &g_array_index(fc
->members
,
1336 struct ctf_named_field_class
, i
);
1338 ctf_field_class_struct_append_member(copy_fc
,
1339 named_fc
->name
->str
,
1340 ctf_field_class_copy(named_fc
->fc
));
1347 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1348 struct ctf_field_path
*src_fp
)
1354 dst_fp
->root
= src_fp
->root
;
1355 ctf_field_path_clear(dst_fp
);
1357 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1358 int64_t index
= ctf_field_path_borrow_index_by_index(
1361 ctf_field_path_append_index(dst_fp
, index
);
1366 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1367 struct ctf_field_class_variant
*fc
)
1369 struct ctf_field_class_variant
*copy_fc
=
1370 ctf_field_class_variant_create();
1375 for (i
= 0; i
< fc
->options
->len
; i
++) {
1376 struct ctf_named_field_class
*named_fc
=
1377 &g_array_index(fc
->options
,
1378 struct ctf_named_field_class
, i
);
1380 ctf_field_class_variant_append_option(copy_fc
,
1381 named_fc
->name
->str
,
1382 ctf_field_class_copy(named_fc
->fc
));
1385 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1386 struct ctf_field_class_variant_range
*range
=
1387 &g_array_index(fc
->ranges
,
1388 struct ctf_field_class_variant_range
, i
);
1390 g_array_append_val(copy_fc
->ranges
, *range
);
1393 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1394 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1395 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1400 void ctf_field_class_array_base_copy_content(
1401 struct ctf_field_class_array_base
*dst_fc
,
1402 struct ctf_field_class_array_base
*src_fc
)
1406 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1407 dst_fc
->is_text
= src_fc
->is_text
;
1411 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1412 struct ctf_field_class_array
*fc
)
1414 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1417 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1418 copy_fc
->length
= fc
->length
;
1423 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1424 struct ctf_field_class_sequence
*fc
)
1426 struct ctf_field_class_sequence
*copy_fc
=
1427 ctf_field_class_sequence_create();
1430 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1431 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1432 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1433 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1438 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1440 struct ctf_field_class
*copy_fc
= NULL
;
1447 * Translation should not have happened yet.
1449 BT_ASSERT(!fc
->ir_fc
);
1452 case CTF_FIELD_CLASS_TYPE_INT
:
1453 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1455 case CTF_FIELD_CLASS_TYPE_ENUM
:
1456 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1458 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1459 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1461 case CTF_FIELD_CLASS_TYPE_STRING
:
1462 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1464 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1465 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1467 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1468 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1470 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1471 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1473 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1474 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1480 copy_fc
->type
= fc
->type
;
1481 copy_fc
->alignment
= fc
->alignment
;
1482 copy_fc
->in_ir
= fc
->in_ir
;
1489 struct ctf_event_class
*ctf_event_class_create(void)
1491 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1494 ec
->name
= g_string_new(NULL
);
1495 BT_ASSERT(ec
->name
);
1496 ec
->emf_uri
= g_string_new(NULL
);
1497 BT_ASSERT(ec
->emf_uri
);
1498 ec
->is_log_level_set
= false;
1503 void ctf_event_class_set_log_level(struct ctf_event_class
*ec
,
1504 enum bt_event_class_log_level log_level
)
1507 ec
->log_level
= log_level
;
1508 ec
->is_log_level_set
= true;
1512 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1519 g_string_free(ec
->name
, TRUE
);
1523 g_string_free(ec
->emf_uri
, TRUE
);
1526 ctf_field_class_destroy(ec
->spec_context_fc
);
1527 ctf_field_class_destroy(ec
->payload_fc
);
1532 struct ctf_stream_class
*ctf_stream_class_create(void)
1534 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1537 sc
->event_classes
= g_ptr_array_new_with_free_func(
1538 (GDestroyNotify
) ctf_event_class_destroy
);
1539 BT_ASSERT(sc
->event_classes
);
1540 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1542 BT_ASSERT(sc
->event_classes_by_id
);
1547 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1553 if (sc
->event_classes
) {
1554 g_ptr_array_free(sc
->event_classes
, TRUE
);
1557 if (sc
->event_classes_by_id
) {
1558 g_hash_table_destroy(sc
->event_classes_by_id
);
1561 ctf_field_class_destroy(sc
->packet_context_fc
);
1562 ctf_field_class_destroy(sc
->event_header_fc
);
1563 ctf_field_class_destroy(sc
->event_common_context_fc
);
1568 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1569 struct ctf_event_class
*ec
)
1571 g_ptr_array_add(sc
->event_classes
, ec
);
1572 g_hash_table_insert(sc
->event_classes_by_id
,
1573 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1577 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1578 struct ctf_stream_class
*sc
, uint64_t type
)
1581 return g_hash_table_lookup(sc
->event_classes_by_id
,
1582 GUINT_TO_POINTER((guint
) type
));
1586 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1589 entry
->name
= g_string_new(NULL
);
1590 BT_ASSERT(entry
->name
);
1591 entry
->value
.str
= g_string_new(NULL
);
1592 BT_ASSERT(entry
->value
.str
);
1596 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1601 g_string_free(entry
->name
, TRUE
);
1604 if (entry
->value
.str
) {
1605 g_string_free(entry
->value
.str
, TRUE
);
1610 struct ctf_clock_class
*ctf_clock_class_create(void)
1612 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1615 cc
->name
= g_string_new(NULL
);
1616 BT_ASSERT(cc
->name
);
1617 cc
->description
= g_string_new(NULL
);
1618 BT_ASSERT(cc
->description
);
1623 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1630 g_string_free(cc
->name
, TRUE
);
1633 if (cc
->description
) {
1634 g_string_free(cc
->description
, TRUE
);
1637 bt_clock_class_put_ref(cc
->ir_cc
);
1642 struct ctf_trace_class
*ctf_trace_class_create(void)
1644 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1647 tc
->default_byte_order
= CTF_BYTE_ORDER_UNKNOWN
;
1648 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1649 (GDestroyNotify
) ctf_clock_class_destroy
);
1650 BT_ASSERT(tc
->clock_classes
);
1651 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1652 (GDestroyNotify
) ctf_stream_class_destroy
);
1653 BT_ASSERT(tc
->stream_classes
);
1654 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1655 sizeof(struct ctf_trace_class_env_entry
));
1660 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1666 ctf_field_class_destroy(tc
->packet_header_fc
);
1668 if (tc
->clock_classes
) {
1669 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1672 if (tc
->stream_classes
) {
1673 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1676 if (tc
->env_entries
) {
1679 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1680 struct ctf_trace_class_env_entry
*entry
=
1681 &g_array_index(tc
->env_entries
,
1682 struct ctf_trace_class_env_entry
, i
);
1684 _ctf_trace_class_env_entry_fini(entry
);
1687 g_array_free(tc
->env_entries
, TRUE
);
1694 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1695 const char *name
, enum ctf_trace_class_env_entry_type type
,
1696 const char *str_value
, int64_t i_value
)
1698 struct ctf_trace_class_env_entry
*entry
;
1702 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1704 entry
= &g_array_index(tc
->env_entries
,
1705 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1707 _ctf_trace_class_env_entry_init(entry
);
1708 g_string_assign(entry
->name
, name
);
1711 g_string_assign(entry
->value
.str
, str_value
);
1714 entry
->value
.i
= i_value
;
1718 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1719 struct ctf_trace_class
*tc
, uint64_t id
)
1722 struct ctf_stream_class
*ret_sc
= NULL
;
1726 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1727 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1740 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1741 struct ctf_trace_class
*tc
, const char *name
)
1744 struct ctf_clock_class
*ret_cc
= NULL
;
1747 BT_ASSERT_DBG(name
);
1749 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1750 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1752 BT_ASSERT_DBG(cc
->name
);
1753 if (strcmp(cc
->name
->str
, name
) == 0) {
1764 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1765 struct ctf_trace_class
*tc
, uint64_t index
)
1768 BT_ASSERT_DBG(index
< tc
->env_entries
->len
);
1769 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1774 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1775 struct ctf_trace_class
*tc
, const char *name
)
1777 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1781 BT_ASSERT_DBG(name
);
1783 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1784 struct ctf_trace_class_env_entry
*env_entry
=
1785 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1787 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1788 ret_entry
= env_entry
;
1797 #endif /* _CTF_META_H */