5 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
18 #include <babeltrace2/babeltrace.h>
19 #include "common/common.h"
20 #include "common/uuid.h"
21 #include "common/assert.h"
26 enum ctf_field_class_type
{
27 CTF_FIELD_CLASS_TYPE_INT
,
28 CTF_FIELD_CLASS_TYPE_ENUM
,
29 CTF_FIELD_CLASS_TYPE_FLOAT
,
30 CTF_FIELD_CLASS_TYPE_STRING
,
31 CTF_FIELD_CLASS_TYPE_STRUCT
,
32 CTF_FIELD_CLASS_TYPE_ARRAY
,
33 CTF_FIELD_CLASS_TYPE_SEQUENCE
,
34 CTF_FIELD_CLASS_TYPE_VARIANT
,
37 enum ctf_field_class_meaning
{
38 CTF_FIELD_CLASS_MEANING_NONE
,
39 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
,
40 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
,
41 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
,
42 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
,
43 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
,
44 CTF_FIELD_CLASS_MEANING_MAGIC
,
45 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
,
46 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
,
47 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
,
48 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
,
49 CTF_FIELD_CLASS_MEANING_UUID
,
53 CTF_BYTE_ORDER_UNKNOWN
,
54 CTF_BYTE_ORDER_DEFAULT
,
55 CTF_BYTE_ORDER_LITTLE
,
65 CTF_SCOPE_PACKET_UNKNOWN
= -1,
66 CTF_SCOPE_PACKET_HEADER
= 0,
67 CTF_SCOPE_PACKET_CONTEXT
,
68 CTF_SCOPE_EVENT_HEADER
,
69 CTF_SCOPE_EVENT_COMMON_CONTEXT
,
70 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
,
71 CTF_SCOPE_EVENT_PAYLOAD
,
74 struct ctf_clock_class
{
79 int64_t offset_seconds
;
80 uint64_t offset_cycles
;
85 /* Weak, set during translation */
86 bt_clock_class
*ir_cc
;
89 struct ctf_field_class
{
90 enum ctf_field_class_type type
;
91 unsigned int alignment
;
95 /* Weak, set during translation. NULL if `in_ir` is false below. */
96 bt_field_class
*ir_fc
;
99 struct ctf_field_class_bit_array
{
100 struct ctf_field_class base
;
101 enum ctf_byte_order byte_order
;
105 struct ctf_field_class_int
{
106 struct ctf_field_class_bit_array base
;
107 enum ctf_field_class_meaning meaning
;
109 bt_field_class_integer_preferred_display_base disp_base
;
110 enum ctf_encoding encoding
;
111 int64_t storing_index
;
114 struct ctf_clock_class
*mapped_clock_class
;
129 struct ctf_field_class_enum_mapping
{
132 /* Array of `struct ctf_range` */
136 struct ctf_field_class_enum
{
137 struct ctf_field_class_int base
;
139 /* Array of `struct ctf_field_class_enum_mapping` */
143 struct ctf_field_class_float
{
144 struct ctf_field_class_bit_array base
;
147 struct ctf_field_class_string
{
148 struct ctf_field_class base
;
149 enum ctf_encoding encoding
;
152 struct ctf_named_field_class
{
153 /* Original name which can include a leading `_` */
156 /* Name as translated to trace IR (leading `_` removed) */
160 struct ctf_field_class
*fc
;
163 struct ctf_field_class_struct
{
164 struct ctf_field_class base
;
166 /* Array of `struct ctf_named_field_class` */
170 struct ctf_field_path
{
173 /* Array of `int64_t` */
177 struct ctf_field_class_variant_range
{
178 struct ctf_range range
;
179 uint64_t option_index
;
182 struct ctf_field_class_variant
{
183 struct ctf_field_class base
;
185 struct ctf_field_path tag_path
;
186 uint64_t stored_tag_index
;
188 /* Array of `struct ctf_named_field_class` */
191 /* Array of `struct ctf_field_class_variant_range` */
195 struct ctf_field_class_enum
*tag_fc
;
198 struct ctf_field_class_array_base
{
199 struct ctf_field_class base
;
200 struct ctf_field_class
*elem_fc
;
204 struct ctf_field_class_array
{
205 struct ctf_field_class_array_base base
;
206 enum ctf_field_class_meaning meaning
;
210 struct ctf_field_class_sequence
{
211 struct ctf_field_class_array_base base
;
213 struct ctf_field_path length_path
;
214 uint64_t stored_length_index
;
217 struct ctf_field_class_int
*length_fc
;
220 struct ctf_event_class
{
224 bt_event_class_log_level log_level
;
226 bool is_log_level_set
;
229 struct ctf_field_class
*spec_context_fc
;
232 struct ctf_field_class
*payload_fc
;
234 /* Weak, set during translation */
235 bt_event_class
*ir_ec
;
238 struct ctf_stream_class
{
241 bool packets_have_ts_begin
;
242 bool packets_have_ts_end
;
243 bool has_discarded_events
;
244 bool has_discarded_packets
;
245 bool discarded_events_have_default_cs
;
246 bool discarded_packets_have_default_cs
;
249 struct ctf_field_class
*packet_context_fc
;
252 struct ctf_field_class
*event_header_fc
;
255 struct ctf_field_class
*event_common_context_fc
;
257 /* Array of `struct ctf_event_class *`, owned by this */
258 GPtrArray
*event_classes
;
261 * Hash table mapping event class IDs to `struct ctf_event_class *`,
264 GHashTable
*event_classes_by_id
;
267 struct ctf_clock_class
*default_clock_class
;
269 /* Weak, set during translation */
270 bt_stream_class
*ir_sc
;
273 enum ctf_trace_class_env_entry_type
{
274 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
275 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
278 struct ctf_trace_class_env_entry
{
279 enum ctf_trace_class_env_entry_type type
;
288 struct ctf_trace_class
{
293 enum ctf_byte_order default_byte_order
;
296 struct ctf_field_class
*packet_header_fc
;
298 uint64_t stored_value_count
;
300 /* Array of `struct ctf_clock_class *` (owned by this) */
301 GPtrArray
*clock_classes
;
303 /* Array of `struct ctf_stream_class *` */
304 GPtrArray
*stream_classes
;
306 /* Array of `struct ctf_trace_class_env_entry` */
311 /* Weak, set during translation */
312 bt_trace_class
*ir_tc
;
316 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
319 void _ctf_field_class_init(struct ctf_field_class
*fc
,
320 enum ctf_field_class_type type
, unsigned int alignment
)
324 fc
->alignment
= alignment
;
329 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
330 enum ctf_field_class_type type
)
332 _ctf_field_class_init((void *) fc
, type
, 1);
336 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
337 enum ctf_field_class_type type
)
339 _ctf_field_class_bit_array_init((void *) fc
, type
);
340 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
341 fc
->storing_index
= -1;
345 void ctf_field_path_init(struct ctf_field_path
*field_path
)
347 BT_ASSERT(field_path
);
348 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
349 BT_ASSERT(field_path
->path
);
353 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
355 BT_ASSERT(field_path
);
357 if (field_path
->path
) {
358 g_array_free(field_path
->path
, TRUE
);
363 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
366 named_fc
->name
= g_string_new(NULL
);
367 BT_ASSERT(named_fc
->name
);
368 named_fc
->orig_name
= g_string_new(NULL
);
369 BT_ASSERT(named_fc
->orig_name
);
373 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
377 if (named_fc
->name
) {
378 g_string_free(named_fc
->name
, TRUE
);
381 if (named_fc
->orig_name
) {
382 g_string_free(named_fc
->orig_name
, TRUE
);
385 ctf_field_class_destroy(named_fc
->fc
);
389 void _ctf_field_class_enum_mapping_init(
390 struct ctf_field_class_enum_mapping
*mapping
)
393 mapping
->label
= g_string_new(NULL
);
394 BT_ASSERT(mapping
->label
);
395 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
396 BT_ASSERT(mapping
->ranges
);
400 void _ctf_field_class_enum_mapping_fini(
401 struct ctf_field_class_enum_mapping
*mapping
)
405 if (mapping
->label
) {
406 g_string_free(mapping
->label
, TRUE
);
409 if (mapping
->ranges
) {
410 g_array_free(mapping
->ranges
, TRUE
);
415 struct ctf_field_class_int
*ctf_field_class_int_create(void)
417 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
420 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
425 struct ctf_field_class_float
*ctf_field_class_float_create(void)
427 struct ctf_field_class_float
*fc
=
428 g_new0(struct ctf_field_class_float
, 1);
431 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
436 struct ctf_field_class_string
*ctf_field_class_string_create(void)
438 struct ctf_field_class_string
*fc
=
439 g_new0(struct ctf_field_class_string
, 1);
442 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
447 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
449 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
452 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
453 fc
->mappings
= g_array_new(FALSE
, TRUE
,
454 sizeof(struct ctf_field_class_enum_mapping
));
455 BT_ASSERT(fc
->mappings
);
460 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
462 struct ctf_field_class_struct
*fc
=
463 g_new0(struct ctf_field_class_struct
, 1);
466 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
467 fc
->members
= g_array_new(FALSE
, TRUE
,
468 sizeof(struct ctf_named_field_class
));
469 BT_ASSERT(fc
->members
);
470 fc
->base
.is_compound
= true;
475 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
477 struct ctf_field_class_variant
*fc
=
478 g_new0(struct ctf_field_class_variant
, 1);
481 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
482 fc
->options
= g_array_new(FALSE
, TRUE
,
483 sizeof(struct ctf_named_field_class
));
484 BT_ASSERT(fc
->options
);
485 fc
->ranges
= g_array_new(FALSE
, TRUE
,
486 sizeof(struct ctf_field_class_variant_range
));
487 BT_ASSERT(fc
->ranges
);
488 fc
->tag_ref
= g_string_new(NULL
);
489 BT_ASSERT(fc
->tag_ref
);
490 ctf_field_path_init(&fc
->tag_path
);
491 fc
->base
.is_compound
= true;
496 struct ctf_field_class_array
*ctf_field_class_array_create(void)
498 struct ctf_field_class_array
*fc
=
499 g_new0(struct ctf_field_class_array
, 1);
502 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
503 fc
->base
.base
.is_compound
= true;
508 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
510 struct ctf_field_class_sequence
*fc
=
511 g_new0(struct ctf_field_class_sequence
, 1);
514 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
515 fc
->length_ref
= g_string_new(NULL
);
516 BT_ASSERT(fc
->length_ref
);
517 ctf_field_path_init(&fc
->length_path
);
518 fc
->base
.base
.is_compound
= true;
523 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
530 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
537 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
538 struct ctf_field_class_enum_mapping
*mapping
=
539 &g_array_index(fc
->mappings
,
540 struct ctf_field_class_enum_mapping
, i
);
542 _ctf_field_class_enum_mapping_fini(mapping
);
545 g_array_free(fc
->mappings
, TRUE
);
552 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
559 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
566 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
573 for (i
= 0; i
< fc
->members
->len
; i
++) {
574 struct ctf_named_field_class
*named_fc
=
575 &g_array_index(fc
->members
,
576 struct ctf_named_field_class
, i
);
578 _ctf_named_field_class_fini(named_fc
);
581 g_array_free(fc
->members
, TRUE
);
588 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
591 ctf_field_class_destroy(fc
->elem_fc
);
595 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
598 _ctf_field_class_array_base_fini((void *) fc
);
603 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
606 _ctf_field_class_array_base_fini((void *) fc
);
608 if (fc
->length_ref
) {
609 g_string_free(fc
->length_ref
, TRUE
);
612 ctf_field_path_fini(&fc
->length_path
);
617 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
624 for (i
= 0; i
< fc
->options
->len
; i
++) {
625 struct ctf_named_field_class
*named_fc
=
626 &g_array_index(fc
->options
,
627 struct ctf_named_field_class
, i
);
629 _ctf_named_field_class_fini(named_fc
);
632 g_array_free(fc
->options
, TRUE
);
636 g_array_free(fc
->ranges
, TRUE
);
640 g_string_free(fc
->tag_ref
, TRUE
);
643 ctf_field_path_fini(&fc
->tag_path
);
648 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
655 case CTF_FIELD_CLASS_TYPE_INT
:
656 _ctf_field_class_int_destroy((void *) fc
);
658 case CTF_FIELD_CLASS_TYPE_ENUM
:
659 _ctf_field_class_enum_destroy((void *) fc
);
661 case CTF_FIELD_CLASS_TYPE_FLOAT
:
662 _ctf_field_class_float_destroy((void *) fc
);
664 case CTF_FIELD_CLASS_TYPE_STRING
:
665 _ctf_field_class_string_destroy((void *) fc
);
667 case CTF_FIELD_CLASS_TYPE_STRUCT
:
668 _ctf_field_class_struct_destroy((void *) fc
);
670 case CTF_FIELD_CLASS_TYPE_ARRAY
:
671 _ctf_field_class_array_destroy((void *) fc
);
673 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
674 _ctf_field_class_sequence_destroy((void *) fc
);
676 case CTF_FIELD_CLASS_TYPE_VARIANT
:
677 _ctf_field_class_variant_destroy((void *) fc
);
685 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
686 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
689 BT_ASSERT(index
< mapping
->ranges
->len
);
690 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
694 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
695 struct ctf_field_class_enum
*fc
, uint64_t index
)
698 BT_ASSERT(index
< fc
->mappings
->len
);
699 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
704 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
705 struct ctf_field_class_enum
*fc
, const char *label
)
707 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
713 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
714 struct ctf_field_class_enum_mapping
*mapping
=
715 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
717 if (strcmp(mapping
->label
->str
, label
) == 0) {
718 ret_mapping
= mapping
;
728 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
729 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
731 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
732 struct ctf_range range
= {
741 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
742 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
745 if (strcmp(mapping
->label
->str
, label
) == 0) {
750 if (i
== fc
->mappings
->len
) {
755 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
756 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
757 fc
, fc
->mappings
->len
- 1);
758 _ctf_field_class_enum_mapping_init(mapping
);
759 g_string_assign(mapping
->label
, label
);
762 g_array_append_val(mapping
->ranges
, range
);
766 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
767 struct ctf_field_class_struct
*fc
, uint64_t index
)
770 BT_ASSERT(index
< fc
->members
->len
);
771 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
776 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
777 struct ctf_field_class_struct
*fc
, const char *name
)
780 struct ctf_named_field_class
*ret_named_fc
= NULL
;
785 for (i
= 0; i
< fc
->members
->len
; i
++) {
786 struct ctf_named_field_class
*named_fc
=
787 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
789 if (strcmp(name
, named_fc
->name
->str
) == 0) {
790 ret_named_fc
= named_fc
;
800 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
801 struct ctf_field_class_struct
*struct_fc
, const char *name
)
803 struct ctf_named_field_class
*named_fc
= NULL
;
804 struct ctf_field_class
*fc
= NULL
;
810 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
822 struct ctf_field_class_int
*
823 ctf_field_class_struct_borrow_member_int_field_class_by_name(
824 struct ctf_field_class_struct
*struct_fc
, const char *name
)
826 struct ctf_field_class_int
*int_fc
= NULL
;
829 ctf_field_class_struct_borrow_member_field_class_by_name(
835 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
836 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
846 void _ctf_named_field_class_unescape_orig_name(
847 struct ctf_named_field_class
*named_fc
)
849 const char *name
= named_fc
->orig_name
->str
;
851 if (name
[0] == '_') {
855 g_string_assign(named_fc
->name
, name
);
859 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
860 const char *orig_name
, struct ctf_field_class
*member_fc
)
862 struct ctf_named_field_class
*named_fc
;
865 BT_ASSERT(orig_name
);
866 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
868 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
869 fc
->members
->len
- 1);
870 _ctf_named_field_class_init(named_fc
);
871 g_string_assign(named_fc
->orig_name
, orig_name
);
872 _ctf_named_field_class_unescape_orig_name(named_fc
);
873 named_fc
->fc
= member_fc
;
875 if (member_fc
->alignment
> fc
->base
.alignment
) {
876 fc
->base
.alignment
= member_fc
->alignment
;
881 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
882 struct ctf_field_class_variant
*fc
, uint64_t index
)
885 BT_ASSERT(index
< fc
->options
->len
);
886 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
891 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
892 struct ctf_field_class_variant
*fc
, const char *name
)
895 struct ctf_named_field_class
*ret_named_fc
= NULL
;
900 for (i
= 0; i
< fc
->options
->len
; i
++) {
901 struct ctf_named_field_class
*named_fc
=
902 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
904 if (strcmp(name
, named_fc
->name
->str
) == 0) {
905 ret_named_fc
= named_fc
;
915 struct ctf_field_class_variant_range
*
916 ctf_field_class_variant_borrow_range_by_index(
917 struct ctf_field_class_variant
*fc
, uint64_t index
)
920 BT_ASSERT(index
< fc
->ranges
->len
);
921 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
926 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
927 const char *orig_name
, struct ctf_field_class
*option_fc
)
929 struct ctf_named_field_class
*named_fc
;
932 BT_ASSERT(orig_name
);
933 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
935 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
936 fc
->options
->len
- 1);
937 _ctf_named_field_class_init(named_fc
);
938 g_string_assign(named_fc
->orig_name
, orig_name
);
939 _ctf_named_field_class_unescape_orig_name(named_fc
);
940 named_fc
->fc
= option_fc
;
944 void ctf_field_class_variant_set_tag_field_class(
945 struct ctf_field_class_variant
*fc
,
946 struct ctf_field_class_enum
*tag_fc
)
954 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
956 struct ctf_named_field_class
*named_fc
=
957 ctf_field_class_variant_borrow_option_by_index(
959 struct ctf_field_class_enum_mapping
*mapping
;
961 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
962 tag_fc
, named_fc
->orig_name
->str
);
967 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
969 struct ctf_range
*range
=
970 ctf_field_class_enum_mapping_borrow_range_by_index(
972 struct ctf_field_class_variant_range var_range
;
974 var_range
.range
= *range
;
975 var_range
.option_index
= option_i
;
976 g_array_append_val(fc
->ranges
, var_range
);
982 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
983 struct ctf_field_class
*comp_fc
, uint64_t index
)
985 struct ctf_field_class
*fc
= NULL
;
987 switch (comp_fc
->type
) {
988 case CTF_FIELD_CLASS_TYPE_STRUCT
:
990 struct ctf_named_field_class
*named_fc
=
991 ctf_field_class_struct_borrow_member_by_index(
992 (void *) comp_fc
, index
);
998 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1000 struct ctf_named_field_class
*named_fc
=
1001 ctf_field_class_variant_borrow_option_by_index(
1002 (void *) comp_fc
, index
);
1004 BT_ASSERT(named_fc
);
1008 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1009 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1011 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1013 fc
= array_fc
->elem_fc
;
1024 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1026 uint64_t field_count
;
1029 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1031 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1033 field_count
= struct_fc
->members
->len
;
1036 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1038 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1040 field_count
= var_fc
->options
->len
;
1043 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1044 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1046 * Array and sequence types always contain a single
1047 * member (the element type).
1059 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1060 struct ctf_field_class
*fc
, const char *orig_name
)
1062 int64_t ret_index
= -1;
1066 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1068 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1070 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1071 struct ctf_named_field_class
*named_fc
=
1072 ctf_field_class_struct_borrow_member_by_index(
1075 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1076 ret_index
= (int64_t) i
;
1083 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1085 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1087 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1088 struct ctf_named_field_class
*named_fc
=
1089 ctf_field_class_variant_borrow_option_by_index(
1092 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1093 ret_index
= (int64_t) i
;
1109 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1112 g_array_append_val(fp
->path
, index
);
1116 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1120 BT_ASSERT(index
< fp
->path
->len
);
1121 return g_array_index(fp
->path
, int64_t, index
);
1125 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1128 g_array_set_size(fp
->path
, 0);
1132 const char *ctf_scope_string(enum ctf_scope scope
)
1135 case CTF_SCOPE_PACKET_HEADER
:
1136 return "PACKET_HEADER";
1137 case CTF_SCOPE_PACKET_CONTEXT
:
1138 return "PACKET_CONTEXT";
1139 case CTF_SCOPE_EVENT_HEADER
:
1140 return "EVENT_HEADER";
1141 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1142 return "EVENT_COMMON_CONTEXT";
1143 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1144 return "EVENT_SPECIFIC_CONTEXT";
1145 case CTF_SCOPE_EVENT_PAYLOAD
:
1146 return "EVENT_PAYLOAD";
1153 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1155 GString
*str
= g_string_new(NULL
);
1164 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1166 for (i
= 0; i
< path
->path
->len
; i
++) {
1167 g_string_append_printf(str
, ", %" PRId64
,
1168 ctf_field_path_borrow_index_by_index(path
, i
));
1171 g_string_append(str
, "]");
1178 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1179 struct ctf_field_path
*field_path
,
1180 struct ctf_trace_class
*tc
,
1181 struct ctf_stream_class
*sc
,
1182 struct ctf_event_class
*ec
)
1185 struct ctf_field_class
*fc
;
1187 switch (field_path
->root
) {
1188 case CTF_SCOPE_PACKET_HEADER
:
1189 fc
= tc
->packet_header_fc
;
1191 case CTF_SCOPE_PACKET_CONTEXT
:
1192 fc
= sc
->packet_context_fc
;
1194 case CTF_SCOPE_EVENT_HEADER
:
1195 fc
= sc
->event_header_fc
;
1197 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1198 fc
= sc
->event_common_context_fc
;
1200 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1201 fc
= ec
->spec_context_fc
;
1203 case CTF_SCOPE_EVENT_PAYLOAD
:
1204 fc
= ec
->payload_fc
;
1212 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1213 int64_t child_index
=
1214 ctf_field_path_borrow_index_by_index(field_path
, i
);
1215 struct ctf_field_class
*child_fc
=
1216 ctf_field_class_compound_borrow_field_class_by_index(
1218 BT_ASSERT(child_fc
);
1227 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1230 void ctf_field_class_bit_array_copy_content(
1231 struct ctf_field_class_bit_array
*dst_fc
,
1232 struct ctf_field_class_bit_array
*src_fc
)
1236 dst_fc
->byte_order
= src_fc
->byte_order
;
1237 dst_fc
->size
= src_fc
->size
;
1241 void ctf_field_class_int_copy_content(
1242 struct ctf_field_class_int
*dst_fc
,
1243 struct ctf_field_class_int
*src_fc
)
1245 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1246 dst_fc
->meaning
= src_fc
->meaning
;
1247 dst_fc
->is_signed
= src_fc
->is_signed
;
1248 dst_fc
->disp_base
= src_fc
->disp_base
;
1249 dst_fc
->encoding
= src_fc
->encoding
;
1250 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1251 dst_fc
->storing_index
= src_fc
->storing_index
;
1255 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1256 struct ctf_field_class_int
*fc
)
1258 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1261 ctf_field_class_int_copy_content(copy_fc
, fc
);
1266 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1267 struct ctf_field_class_enum
*fc
)
1269 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1273 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1275 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1278 struct ctf_field_class_enum_mapping
*mapping
=
1279 &g_array_index(fc
->mappings
,
1280 struct ctf_field_class_enum_mapping
, i
);
1282 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1283 struct ctf_range
*range
=
1284 &g_array_index(mapping
->ranges
,
1285 struct ctf_range
, range_i
);
1287 ctf_field_class_enum_map_range(copy_fc
,
1288 mapping
->label
->str
, range
->lower
.u
,
1297 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1298 struct ctf_field_class_float
*fc
)
1300 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1303 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1308 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1309 struct ctf_field_class_string
*fc
)
1311 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1318 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1319 struct ctf_field_class_struct
*fc
)
1321 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1326 for (i
= 0; i
< fc
->members
->len
; i
++) {
1327 struct ctf_named_field_class
*named_fc
=
1328 &g_array_index(fc
->members
,
1329 struct ctf_named_field_class
, i
);
1331 ctf_field_class_struct_append_member(copy_fc
,
1332 named_fc
->name
->str
,
1333 ctf_field_class_copy(named_fc
->fc
));
1340 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1341 struct ctf_field_path
*src_fp
)
1347 dst_fp
->root
= src_fp
->root
;
1348 ctf_field_path_clear(dst_fp
);
1350 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1351 int64_t index
= ctf_field_path_borrow_index_by_index(
1354 ctf_field_path_append_index(dst_fp
, index
);
1359 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1360 struct ctf_field_class_variant
*fc
)
1362 struct ctf_field_class_variant
*copy_fc
=
1363 ctf_field_class_variant_create();
1368 for (i
= 0; i
< fc
->options
->len
; i
++) {
1369 struct ctf_named_field_class
*named_fc
=
1370 &g_array_index(fc
->options
,
1371 struct ctf_named_field_class
, i
);
1373 ctf_field_class_variant_append_option(copy_fc
,
1374 named_fc
->name
->str
,
1375 ctf_field_class_copy(named_fc
->fc
));
1378 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1379 struct ctf_field_class_variant_range
*range
=
1380 &g_array_index(fc
->ranges
,
1381 struct ctf_field_class_variant_range
, i
);
1383 g_array_append_val(copy_fc
->ranges
, *range
);
1386 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1387 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1388 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1393 void ctf_field_class_array_base_copy_content(
1394 struct ctf_field_class_array_base
*dst_fc
,
1395 struct ctf_field_class_array_base
*src_fc
)
1399 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1400 dst_fc
->is_text
= src_fc
->is_text
;
1404 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1405 struct ctf_field_class_array
*fc
)
1407 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1410 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1411 copy_fc
->length
= fc
->length
;
1416 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1417 struct ctf_field_class_sequence
*fc
)
1419 struct ctf_field_class_sequence
*copy_fc
=
1420 ctf_field_class_sequence_create();
1423 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1424 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1425 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1426 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1431 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1433 struct ctf_field_class
*copy_fc
= NULL
;
1440 * Translation should not have happened yet.
1442 BT_ASSERT(!fc
->ir_fc
);
1445 case CTF_FIELD_CLASS_TYPE_INT
:
1446 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1448 case CTF_FIELD_CLASS_TYPE_ENUM
:
1449 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1451 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1452 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1454 case CTF_FIELD_CLASS_TYPE_STRING
:
1455 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1457 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1458 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1460 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1461 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1463 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1464 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1466 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1467 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1473 copy_fc
->type
= fc
->type
;
1474 copy_fc
->alignment
= fc
->alignment
;
1475 copy_fc
->in_ir
= fc
->in_ir
;
1482 struct ctf_event_class
*ctf_event_class_create(void)
1484 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1487 ec
->name
= g_string_new(NULL
);
1488 BT_ASSERT(ec
->name
);
1489 ec
->emf_uri
= g_string_new(NULL
);
1490 BT_ASSERT(ec
->emf_uri
);
1491 ec
->is_log_level_set
= false;
1496 void ctf_event_class_set_log_level(struct ctf_event_class
*ec
,
1497 enum bt_event_class_log_level log_level
)
1500 ec
->log_level
= log_level
;
1501 ec
->is_log_level_set
= true;
1505 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1512 g_string_free(ec
->name
, TRUE
);
1516 g_string_free(ec
->emf_uri
, TRUE
);
1519 ctf_field_class_destroy(ec
->spec_context_fc
);
1520 ctf_field_class_destroy(ec
->payload_fc
);
1525 struct ctf_stream_class
*ctf_stream_class_create(void)
1527 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1530 sc
->event_classes
= g_ptr_array_new_with_free_func(
1531 (GDestroyNotify
) ctf_event_class_destroy
);
1532 BT_ASSERT(sc
->event_classes
);
1533 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1535 BT_ASSERT(sc
->event_classes_by_id
);
1540 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1546 if (sc
->event_classes
) {
1547 g_ptr_array_free(sc
->event_classes
, TRUE
);
1550 if (sc
->event_classes_by_id
) {
1551 g_hash_table_destroy(sc
->event_classes_by_id
);
1554 ctf_field_class_destroy(sc
->packet_context_fc
);
1555 ctf_field_class_destroy(sc
->event_header_fc
);
1556 ctf_field_class_destroy(sc
->event_common_context_fc
);
1561 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1562 struct ctf_event_class
*ec
)
1564 g_ptr_array_add(sc
->event_classes
, ec
);
1565 g_hash_table_insert(sc
->event_classes_by_id
,
1566 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1570 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1571 struct ctf_stream_class
*sc
, uint64_t type
)
1574 return g_hash_table_lookup(sc
->event_classes_by_id
,
1575 GUINT_TO_POINTER((guint
) type
));
1579 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1582 entry
->name
= g_string_new(NULL
);
1583 BT_ASSERT(entry
->name
);
1584 entry
->value
.str
= g_string_new(NULL
);
1585 BT_ASSERT(entry
->value
.str
);
1589 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1594 g_string_free(entry
->name
, TRUE
);
1597 if (entry
->value
.str
) {
1598 g_string_free(entry
->value
.str
, TRUE
);
1603 struct ctf_clock_class
*ctf_clock_class_create(void)
1605 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1608 cc
->name
= g_string_new(NULL
);
1609 BT_ASSERT(cc
->name
);
1610 cc
->description
= g_string_new(NULL
);
1611 BT_ASSERT(cc
->description
);
1616 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1623 g_string_free(cc
->name
, TRUE
);
1626 if (cc
->description
) {
1627 g_string_free(cc
->description
, TRUE
);
1630 bt_clock_class_put_ref(cc
->ir_cc
);
1635 struct ctf_trace_class
*ctf_trace_class_create(void)
1637 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1640 tc
->default_byte_order
= CTF_BYTE_ORDER_UNKNOWN
;
1641 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1642 (GDestroyNotify
) ctf_clock_class_destroy
);
1643 BT_ASSERT(tc
->clock_classes
);
1644 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1645 (GDestroyNotify
) ctf_stream_class_destroy
);
1646 BT_ASSERT(tc
->stream_classes
);
1647 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1648 sizeof(struct ctf_trace_class_env_entry
));
1653 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1659 ctf_field_class_destroy(tc
->packet_header_fc
);
1661 if (tc
->clock_classes
) {
1662 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1665 if (tc
->stream_classes
) {
1666 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1669 if (tc
->env_entries
) {
1672 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1673 struct ctf_trace_class_env_entry
*entry
=
1674 &g_array_index(tc
->env_entries
,
1675 struct ctf_trace_class_env_entry
, i
);
1677 _ctf_trace_class_env_entry_fini(entry
);
1680 g_array_free(tc
->env_entries
, TRUE
);
1687 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1688 const char *name
, enum ctf_trace_class_env_entry_type type
,
1689 const char *str_value
, int64_t i_value
)
1691 struct ctf_trace_class_env_entry
*entry
;
1695 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1697 entry
= &g_array_index(tc
->env_entries
,
1698 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1700 _ctf_trace_class_env_entry_init(entry
);
1701 g_string_assign(entry
->name
, name
);
1704 g_string_assign(entry
->value
.str
, str_value
);
1707 entry
->value
.i
= i_value
;
1711 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1712 struct ctf_trace_class
*tc
, uint64_t id
)
1715 struct ctf_stream_class
*ret_sc
= NULL
;
1719 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1720 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1733 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1734 struct ctf_trace_class
*tc
, const char *name
)
1737 struct ctf_clock_class
*ret_cc
= NULL
;
1742 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1743 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1745 BT_ASSERT(cc
->name
);
1746 if (strcmp(cc
->name
->str
, name
) == 0) {
1757 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1758 struct ctf_trace_class
*tc
, uint64_t index
)
1761 BT_ASSERT(index
< tc
->env_entries
->len
);
1762 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1767 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1768 struct ctf_trace_class
*tc
, const char *name
)
1770 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1776 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1777 struct ctf_trace_class_env_entry
*env_entry
=
1778 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1780 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1781 ret_entry
= env_entry
;
1790 #endif /* _CTF_META_H */