5 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
18 #include <babeltrace2/babeltrace.h>
19 #include "common/common.h"
20 #include "common/uuid.h"
21 #include "common/assert.h"
26 enum ctf_field_class_type
{
27 CTF_FIELD_CLASS_TYPE_INT
,
28 CTF_FIELD_CLASS_TYPE_ENUM
,
29 CTF_FIELD_CLASS_TYPE_FLOAT
,
30 CTF_FIELD_CLASS_TYPE_STRING
,
31 CTF_FIELD_CLASS_TYPE_STRUCT
,
32 CTF_FIELD_CLASS_TYPE_ARRAY
,
33 CTF_FIELD_CLASS_TYPE_SEQUENCE
,
34 CTF_FIELD_CLASS_TYPE_VARIANT
,
37 enum ctf_field_class_meaning
{
38 CTF_FIELD_CLASS_MEANING_NONE
,
39 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
,
40 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
,
41 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
,
42 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
,
43 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
,
44 CTF_FIELD_CLASS_MEANING_MAGIC
,
45 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
,
46 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
,
47 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
,
48 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
,
49 CTF_FIELD_CLASS_MEANING_UUID
,
53 CTF_BYTE_ORDER_UNKNOWN
,
54 CTF_BYTE_ORDER_DEFAULT
,
55 CTF_BYTE_ORDER_LITTLE
,
65 CTF_SCOPE_PACKET_UNKNOWN
= -1,
66 CTF_SCOPE_PACKET_HEADER
= 0,
67 CTF_SCOPE_PACKET_CONTEXT
,
68 CTF_SCOPE_EVENT_HEADER
,
69 CTF_SCOPE_EVENT_COMMON_CONTEXT
,
70 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
,
71 CTF_SCOPE_EVENT_PAYLOAD
,
74 struct ctf_clock_class
{
79 int64_t offset_seconds
;
80 uint64_t offset_cycles
;
85 /* Weak, set during translation */
86 bt_clock_class
*ir_cc
;
89 struct ctf_field_class
{
90 enum ctf_field_class_type type
;
91 unsigned int alignment
;
95 /* Weak, set during translation. NULL if `in_ir` is false below. */
96 bt_field_class
*ir_fc
;
99 struct ctf_field_class_bit_array
{
100 struct ctf_field_class base
;
101 enum ctf_byte_order byte_order
;
105 struct ctf_field_class_int
{
106 struct ctf_field_class_bit_array base
;
107 enum ctf_field_class_meaning meaning
;
109 bt_field_class_integer_preferred_display_base disp_base
;
110 enum ctf_encoding encoding
;
111 int64_t storing_index
;
114 struct ctf_clock_class
*mapped_clock_class
;
129 struct ctf_field_class_enum_mapping
{
132 /* Array of `struct ctf_range` */
136 struct ctf_field_class_enum
{
137 struct ctf_field_class_int base
;
139 /* Array of `struct ctf_field_class_enum_mapping` */
143 struct ctf_field_class_float
{
144 struct ctf_field_class_bit_array base
;
147 struct ctf_field_class_string
{
148 struct ctf_field_class base
;
149 enum ctf_encoding encoding
;
152 struct ctf_named_field_class
{
153 /* Original name which can include a leading `_` */
156 /* Name as translated to trace IR (leading `_` removed) */
160 struct ctf_field_class
*fc
;
163 struct ctf_field_class_struct
{
164 struct ctf_field_class base
;
166 /* Array of `struct ctf_named_field_class` */
170 struct ctf_field_path
{
173 /* Array of `int64_t` */
177 struct ctf_field_class_variant_range
{
178 struct ctf_range range
;
179 uint64_t option_index
;
182 struct ctf_field_class_variant
{
183 struct ctf_field_class base
;
185 struct ctf_field_path tag_path
;
186 uint64_t stored_tag_index
;
188 /* Array of `struct ctf_named_field_class` */
191 /* Array of `struct ctf_field_class_variant_range` */
195 struct ctf_field_class_enum
*tag_fc
;
198 struct ctf_field_class_array_base
{
199 struct ctf_field_class base
;
200 struct ctf_field_class
*elem_fc
;
204 struct ctf_field_class_array
{
205 struct ctf_field_class_array_base base
;
206 enum ctf_field_class_meaning meaning
;
210 struct ctf_field_class_sequence
{
211 struct ctf_field_class_array_base base
;
213 struct ctf_field_path length_path
;
214 uint64_t stored_length_index
;
217 struct ctf_field_class_int
*length_fc
;
220 struct ctf_event_class
{
224 bt_event_class_log_level log_level
;
226 bool is_log_level_set
;
229 struct ctf_field_class
*spec_context_fc
;
232 struct ctf_field_class
*payload_fc
;
234 /* Weak, set during translation */
235 bt_event_class
*ir_ec
;
238 struct ctf_stream_class
{
241 bool packets_have_ts_begin
;
242 bool packets_have_ts_end
;
243 bool has_discarded_events
;
244 bool has_discarded_packets
;
245 bool discarded_events_have_default_cs
;
246 bool discarded_packets_have_default_cs
;
249 struct ctf_field_class
*packet_context_fc
;
252 struct ctf_field_class
*event_header_fc
;
255 struct ctf_field_class
*event_common_context_fc
;
257 /* Array of `struct ctf_event_class *`, owned by this */
258 GPtrArray
*event_classes
;
261 * Hash table mapping event class IDs to `struct ctf_event_class *`,
264 GHashTable
*event_classes_by_id
;
267 struct ctf_clock_class
*default_clock_class
;
269 /* Weak, set during translation */
270 bt_stream_class
*ir_sc
;
273 enum ctf_trace_class_env_entry_type
{
274 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
275 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
278 struct ctf_trace_class_env_entry
{
279 enum ctf_trace_class_env_entry_type type
;
288 struct ctf_trace_class
{
293 enum ctf_byte_order default_byte_order
;
296 struct ctf_field_class
*packet_header_fc
;
298 uint64_t stored_value_count
;
300 /* Array of `struct ctf_clock_class *` (owned by this) */
301 GPtrArray
*clock_classes
;
303 /* Array of `struct ctf_stream_class *` */
304 GPtrArray
*stream_classes
;
306 /* Array of `struct ctf_trace_class_env_entry` */
311 /* Weak, set during translation */
312 bt_trace_class
*ir_tc
;
316 bool lttng_event_after_packet
;
321 void ctf_field_class_destroy(struct ctf_field_class
*fc
);
324 void _ctf_field_class_init(struct ctf_field_class
*fc
,
325 enum ctf_field_class_type type
, unsigned int alignment
)
329 fc
->alignment
= alignment
;
334 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array
*fc
,
335 enum ctf_field_class_type type
)
337 _ctf_field_class_init((void *) fc
, type
, 1);
341 void _ctf_field_class_int_init(struct ctf_field_class_int
*fc
,
342 enum ctf_field_class_type type
)
344 _ctf_field_class_bit_array_init((void *) fc
, type
);
345 fc
->meaning
= CTF_FIELD_CLASS_MEANING_NONE
;
346 fc
->storing_index
= -1;
350 void ctf_field_path_init(struct ctf_field_path
*field_path
)
352 BT_ASSERT(field_path
);
353 field_path
->path
= g_array_new(FALSE
, TRUE
, sizeof(int64_t));
354 BT_ASSERT(field_path
->path
);
358 void ctf_field_path_fini(struct ctf_field_path
*field_path
)
360 BT_ASSERT(field_path
);
362 if (field_path
->path
) {
363 g_array_free(field_path
->path
, TRUE
);
368 void _ctf_named_field_class_init(struct ctf_named_field_class
*named_fc
)
371 named_fc
->name
= g_string_new(NULL
);
372 BT_ASSERT(named_fc
->name
);
373 named_fc
->orig_name
= g_string_new(NULL
);
374 BT_ASSERT(named_fc
->orig_name
);
378 void _ctf_named_field_class_fini(struct ctf_named_field_class
*named_fc
)
382 if (named_fc
->name
) {
383 g_string_free(named_fc
->name
, TRUE
);
386 if (named_fc
->orig_name
) {
387 g_string_free(named_fc
->orig_name
, TRUE
);
390 ctf_field_class_destroy(named_fc
->fc
);
394 void _ctf_field_class_enum_mapping_init(
395 struct ctf_field_class_enum_mapping
*mapping
)
398 mapping
->label
= g_string_new(NULL
);
399 BT_ASSERT(mapping
->label
);
400 mapping
->ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct ctf_range
));
401 BT_ASSERT(mapping
->ranges
);
405 void _ctf_field_class_enum_mapping_fini(
406 struct ctf_field_class_enum_mapping
*mapping
)
410 if (mapping
->label
) {
411 g_string_free(mapping
->label
, TRUE
);
414 if (mapping
->ranges
) {
415 g_array_free(mapping
->ranges
, TRUE
);
420 struct ctf_field_class_int
*ctf_field_class_int_create(void)
422 struct ctf_field_class_int
*fc
= g_new0(struct ctf_field_class_int
, 1);
425 _ctf_field_class_int_init(fc
, CTF_FIELD_CLASS_TYPE_INT
);
430 struct ctf_field_class_float
*ctf_field_class_float_create(void)
432 struct ctf_field_class_float
*fc
=
433 g_new0(struct ctf_field_class_float
, 1);
436 _ctf_field_class_bit_array_init((void *) fc
, CTF_FIELD_CLASS_TYPE_FLOAT
);
441 struct ctf_field_class_string
*ctf_field_class_string_create(void)
443 struct ctf_field_class_string
*fc
=
444 g_new0(struct ctf_field_class_string
, 1);
447 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRING
, 8);
452 struct ctf_field_class_enum
*ctf_field_class_enum_create(void)
454 struct ctf_field_class_enum
*fc
= g_new0(struct ctf_field_class_enum
, 1);
457 _ctf_field_class_int_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ENUM
);
458 fc
->mappings
= g_array_new(FALSE
, TRUE
,
459 sizeof(struct ctf_field_class_enum_mapping
));
460 BT_ASSERT(fc
->mappings
);
465 struct ctf_field_class_struct
*ctf_field_class_struct_create(void)
467 struct ctf_field_class_struct
*fc
=
468 g_new0(struct ctf_field_class_struct
, 1);
471 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_STRUCT
, 1);
472 fc
->members
= g_array_new(FALSE
, TRUE
,
473 sizeof(struct ctf_named_field_class
));
474 BT_ASSERT(fc
->members
);
475 fc
->base
.is_compound
= true;
480 struct ctf_field_class_variant
*ctf_field_class_variant_create(void)
482 struct ctf_field_class_variant
*fc
=
483 g_new0(struct ctf_field_class_variant
, 1);
486 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_VARIANT
, 1);
487 fc
->options
= g_array_new(FALSE
, TRUE
,
488 sizeof(struct ctf_named_field_class
));
489 BT_ASSERT(fc
->options
);
490 fc
->ranges
= g_array_new(FALSE
, TRUE
,
491 sizeof(struct ctf_field_class_variant_range
));
492 BT_ASSERT(fc
->ranges
);
493 fc
->tag_ref
= g_string_new(NULL
);
494 BT_ASSERT(fc
->tag_ref
);
495 ctf_field_path_init(&fc
->tag_path
);
496 fc
->base
.is_compound
= true;
501 struct ctf_field_class_array
*ctf_field_class_array_create(void)
503 struct ctf_field_class_array
*fc
=
504 g_new0(struct ctf_field_class_array
, 1);
507 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_ARRAY
, 1);
508 fc
->base
.base
.is_compound
= true;
513 struct ctf_field_class_sequence
*ctf_field_class_sequence_create(void)
515 struct ctf_field_class_sequence
*fc
=
516 g_new0(struct ctf_field_class_sequence
, 1);
519 _ctf_field_class_init((void *) fc
, CTF_FIELD_CLASS_TYPE_SEQUENCE
, 1);
520 fc
->length_ref
= g_string_new(NULL
);
521 BT_ASSERT(fc
->length_ref
);
522 ctf_field_path_init(&fc
->length_path
);
523 fc
->base
.base
.is_compound
= true;
528 void _ctf_field_class_int_destroy(struct ctf_field_class_int
*fc
)
535 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum
*fc
)
542 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
543 struct ctf_field_class_enum_mapping
*mapping
=
544 &g_array_index(fc
->mappings
,
545 struct ctf_field_class_enum_mapping
, i
);
547 _ctf_field_class_enum_mapping_fini(mapping
);
550 g_array_free(fc
->mappings
, TRUE
);
557 void _ctf_field_class_float_destroy(struct ctf_field_class_float
*fc
)
564 void _ctf_field_class_string_destroy(struct ctf_field_class_string
*fc
)
571 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct
*fc
)
578 for (i
= 0; i
< fc
->members
->len
; i
++) {
579 struct ctf_named_field_class
*named_fc
=
580 &g_array_index(fc
->members
,
581 struct ctf_named_field_class
, i
);
583 _ctf_named_field_class_fini(named_fc
);
586 g_array_free(fc
->members
, TRUE
);
593 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base
*fc
)
596 ctf_field_class_destroy(fc
->elem_fc
);
600 void _ctf_field_class_array_destroy(struct ctf_field_class_array
*fc
)
603 _ctf_field_class_array_base_fini((void *) fc
);
608 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence
*fc
)
611 _ctf_field_class_array_base_fini((void *) fc
);
613 if (fc
->length_ref
) {
614 g_string_free(fc
->length_ref
, TRUE
);
617 ctf_field_path_fini(&fc
->length_path
);
622 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant
*fc
)
629 for (i
= 0; i
< fc
->options
->len
; i
++) {
630 struct ctf_named_field_class
*named_fc
=
631 &g_array_index(fc
->options
,
632 struct ctf_named_field_class
, i
);
634 _ctf_named_field_class_fini(named_fc
);
637 g_array_free(fc
->options
, TRUE
);
641 g_array_free(fc
->ranges
, TRUE
);
645 g_string_free(fc
->tag_ref
, TRUE
);
648 ctf_field_path_fini(&fc
->tag_path
);
653 void ctf_field_class_destroy(struct ctf_field_class
*fc
)
660 case CTF_FIELD_CLASS_TYPE_INT
:
661 _ctf_field_class_int_destroy((void *) fc
);
663 case CTF_FIELD_CLASS_TYPE_ENUM
:
664 _ctf_field_class_enum_destroy((void *) fc
);
666 case CTF_FIELD_CLASS_TYPE_FLOAT
:
667 _ctf_field_class_float_destroy((void *) fc
);
669 case CTF_FIELD_CLASS_TYPE_STRING
:
670 _ctf_field_class_string_destroy((void *) fc
);
672 case CTF_FIELD_CLASS_TYPE_STRUCT
:
673 _ctf_field_class_struct_destroy((void *) fc
);
675 case CTF_FIELD_CLASS_TYPE_ARRAY
:
676 _ctf_field_class_array_destroy((void *) fc
);
678 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
679 _ctf_field_class_sequence_destroy((void *) fc
);
681 case CTF_FIELD_CLASS_TYPE_VARIANT
:
682 _ctf_field_class_variant_destroy((void *) fc
);
690 struct ctf_range
*ctf_field_class_enum_mapping_borrow_range_by_index(
691 struct ctf_field_class_enum_mapping
*mapping
, uint64_t index
)
694 BT_ASSERT(index
< mapping
->ranges
->len
);
695 return &g_array_index(mapping
->ranges
, struct ctf_range
, index
);
699 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_index(
700 struct ctf_field_class_enum
*fc
, uint64_t index
)
703 BT_ASSERT(index
< fc
->mappings
->len
);
704 return &g_array_index(fc
->mappings
, struct ctf_field_class_enum_mapping
,
709 struct ctf_field_class_enum_mapping
*ctf_field_class_enum_borrow_mapping_by_label(
710 struct ctf_field_class_enum
*fc
, const char *label
)
712 struct ctf_field_class_enum_mapping
*ret_mapping
= NULL
;
718 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
719 struct ctf_field_class_enum_mapping
*mapping
=
720 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
722 if (strcmp(mapping
->label
->str
, label
) == 0) {
723 ret_mapping
= mapping
;
733 void ctf_field_class_enum_map_range(struct ctf_field_class_enum
*fc
,
734 const char *label
, uint64_t u_lower
, uint64_t u_upper
)
736 struct ctf_field_class_enum_mapping
*mapping
= NULL
;
737 struct ctf_range range
= {
746 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
747 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
750 if (strcmp(mapping
->label
->str
, label
) == 0) {
755 if (i
== fc
->mappings
->len
) {
760 g_array_set_size(fc
->mappings
, fc
->mappings
->len
+ 1);
761 mapping
= ctf_field_class_enum_borrow_mapping_by_index(
762 fc
, fc
->mappings
->len
- 1);
763 _ctf_field_class_enum_mapping_init(mapping
);
764 g_string_assign(mapping
->label
, label
);
767 g_array_append_val(mapping
->ranges
, range
);
771 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_index(
772 struct ctf_field_class_struct
*fc
, uint64_t index
)
775 BT_ASSERT(index
< fc
->members
->len
);
776 return &g_array_index(fc
->members
, struct ctf_named_field_class
,
781 struct ctf_named_field_class
*ctf_field_class_struct_borrow_member_by_name(
782 struct ctf_field_class_struct
*fc
, const char *name
)
785 struct ctf_named_field_class
*ret_named_fc
= NULL
;
790 for (i
= 0; i
< fc
->members
->len
; i
++) {
791 struct ctf_named_field_class
*named_fc
=
792 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
794 if (strcmp(name
, named_fc
->name
->str
) == 0) {
795 ret_named_fc
= named_fc
;
805 struct ctf_field_class
*ctf_field_class_struct_borrow_member_field_class_by_name(
806 struct ctf_field_class_struct
*struct_fc
, const char *name
)
808 struct ctf_named_field_class
*named_fc
= NULL
;
809 struct ctf_field_class
*fc
= NULL
;
815 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
827 struct ctf_field_class_int
*
828 ctf_field_class_struct_borrow_member_int_field_class_by_name(
829 struct ctf_field_class_struct
*struct_fc
, const char *name
)
831 struct ctf_field_class_int
*int_fc
= NULL
;
834 ctf_field_class_struct_borrow_member_field_class_by_name(
840 if (int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
&&
841 int_fc
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
851 void _ctf_named_field_class_unescape_orig_name(
852 struct ctf_named_field_class
*named_fc
)
854 const char *name
= named_fc
->orig_name
->str
;
856 if (name
[0] == '_') {
860 g_string_assign(named_fc
->name
, name
);
864 void ctf_field_class_struct_append_member(struct ctf_field_class_struct
*fc
,
865 const char *orig_name
, struct ctf_field_class
*member_fc
)
867 struct ctf_named_field_class
*named_fc
;
870 BT_ASSERT(orig_name
);
871 g_array_set_size(fc
->members
, fc
->members
->len
+ 1);
873 named_fc
= &g_array_index(fc
->members
, struct ctf_named_field_class
,
874 fc
->members
->len
- 1);
875 _ctf_named_field_class_init(named_fc
);
876 g_string_assign(named_fc
->orig_name
, orig_name
);
877 _ctf_named_field_class_unescape_orig_name(named_fc
);
878 named_fc
->fc
= member_fc
;
880 if (member_fc
->alignment
> fc
->base
.alignment
) {
881 fc
->base
.alignment
= member_fc
->alignment
;
886 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_index(
887 struct ctf_field_class_variant
*fc
, uint64_t index
)
890 BT_ASSERT(index
< fc
->options
->len
);
891 return &g_array_index(fc
->options
, struct ctf_named_field_class
,
896 struct ctf_named_field_class
*ctf_field_class_variant_borrow_option_by_name(
897 struct ctf_field_class_variant
*fc
, const char *name
)
900 struct ctf_named_field_class
*ret_named_fc
= NULL
;
905 for (i
= 0; i
< fc
->options
->len
; i
++) {
906 struct ctf_named_field_class
*named_fc
=
907 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
909 if (strcmp(name
, named_fc
->name
->str
) == 0) {
910 ret_named_fc
= named_fc
;
920 struct ctf_field_class_variant_range
*
921 ctf_field_class_variant_borrow_range_by_index(
922 struct ctf_field_class_variant
*fc
, uint64_t index
)
925 BT_ASSERT(index
< fc
->ranges
->len
);
926 return &g_array_index(fc
->ranges
, struct ctf_field_class_variant_range
,
931 void ctf_field_class_variant_append_option(struct ctf_field_class_variant
*fc
,
932 const char *orig_name
, struct ctf_field_class
*option_fc
)
934 struct ctf_named_field_class
*named_fc
;
937 BT_ASSERT(orig_name
);
938 g_array_set_size(fc
->options
, fc
->options
->len
+ 1);
940 named_fc
= &g_array_index(fc
->options
, struct ctf_named_field_class
,
941 fc
->options
->len
- 1);
942 _ctf_named_field_class_init(named_fc
);
943 g_string_assign(named_fc
->orig_name
, orig_name
);
944 _ctf_named_field_class_unescape_orig_name(named_fc
);
945 named_fc
->fc
= option_fc
;
949 void ctf_field_class_variant_set_tag_field_class(
950 struct ctf_field_class_variant
*fc
,
951 struct ctf_field_class_enum
*tag_fc
)
959 for (option_i
= 0; option_i
< fc
->options
->len
; option_i
++) {
961 struct ctf_named_field_class
*named_fc
=
962 ctf_field_class_variant_borrow_option_by_index(
964 struct ctf_field_class_enum_mapping
*mapping
;
966 mapping
= ctf_field_class_enum_borrow_mapping_by_label(
967 tag_fc
, named_fc
->orig_name
->str
);
972 for (range_i
= 0; range_i
< mapping
->ranges
->len
;
974 struct ctf_range
*range
=
975 ctf_field_class_enum_mapping_borrow_range_by_index(
977 struct ctf_field_class_variant_range var_range
;
979 var_range
.range
= *range
;
980 var_range
.option_index
= option_i
;
981 g_array_append_val(fc
->ranges
, var_range
);
987 struct ctf_field_class
*ctf_field_class_compound_borrow_field_class_by_index(
988 struct ctf_field_class
*comp_fc
, uint64_t index
)
990 struct ctf_field_class
*fc
= NULL
;
992 switch (comp_fc
->type
) {
993 case CTF_FIELD_CLASS_TYPE_STRUCT
:
995 struct ctf_named_field_class
*named_fc
=
996 ctf_field_class_struct_borrow_member_by_index(
997 (void *) comp_fc
, index
);
1003 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1005 struct ctf_named_field_class
*named_fc
=
1006 ctf_field_class_variant_borrow_option_by_index(
1007 (void *) comp_fc
, index
);
1009 BT_ASSERT(named_fc
);
1013 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1014 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1016 struct ctf_field_class_array_base
*array_fc
= (void *) comp_fc
;
1018 fc
= array_fc
->elem_fc
;
1029 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class
*fc
)
1031 uint64_t field_count
;
1034 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1036 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1038 field_count
= struct_fc
->members
->len
;
1041 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1043 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1045 field_count
= var_fc
->options
->len
;
1048 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1049 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1051 * Array and sequence types always contain a single
1052 * member (the element type).
1064 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1065 struct ctf_field_class
*fc
, const char *orig_name
)
1067 int64_t ret_index
= -1;
1071 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1073 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
1075 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
1076 struct ctf_named_field_class
*named_fc
=
1077 ctf_field_class_struct_borrow_member_by_index(
1080 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1081 ret_index
= (int64_t) i
;
1088 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1090 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1092 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
1093 struct ctf_named_field_class
*named_fc
=
1094 ctf_field_class_variant_borrow_option_by_index(
1097 if (strcmp(orig_name
, named_fc
->orig_name
->str
) == 0) {
1098 ret_index
= (int64_t) i
;
1114 void ctf_field_path_append_index(struct ctf_field_path
*fp
, int64_t index
)
1117 g_array_append_val(fp
->path
, index
);
1121 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path
*fp
,
1125 BT_ASSERT(index
< fp
->path
->len
);
1126 return g_array_index(fp
->path
, int64_t, index
);
1130 void ctf_field_path_clear(struct ctf_field_path
*fp
)
1133 g_array_set_size(fp
->path
, 0);
1137 const char *ctf_scope_string(enum ctf_scope scope
)
1140 case CTF_SCOPE_PACKET_HEADER
:
1141 return "PACKET_HEADER";
1142 case CTF_SCOPE_PACKET_CONTEXT
:
1143 return "PACKET_CONTEXT";
1144 case CTF_SCOPE_EVENT_HEADER
:
1145 return "EVENT_HEADER";
1146 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1147 return "EVENT_COMMON_CONTEXT";
1148 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1149 return "EVENT_SPECIFIC_CONTEXT";
1150 case CTF_SCOPE_EVENT_PAYLOAD
:
1151 return "EVENT_PAYLOAD";
1158 GString
*ctf_field_path_string(struct ctf_field_path
*path
)
1160 GString
*str
= g_string_new(NULL
);
1169 g_string_append_printf(str
, "[%s", ctf_scope_string(path
->root
));
1171 for (i
= 0; i
< path
->path
->len
; i
++) {
1172 g_string_append_printf(str
, ", %" PRId64
,
1173 ctf_field_path_borrow_index_by_index(path
, i
));
1176 g_string_append(str
, "]");
1183 struct ctf_field_class
*ctf_field_path_borrow_field_class(
1184 struct ctf_field_path
*field_path
,
1185 struct ctf_trace_class
*tc
,
1186 struct ctf_stream_class
*sc
,
1187 struct ctf_event_class
*ec
)
1190 struct ctf_field_class
*fc
;
1192 switch (field_path
->root
) {
1193 case CTF_SCOPE_PACKET_HEADER
:
1194 fc
= tc
->packet_header_fc
;
1196 case CTF_SCOPE_PACKET_CONTEXT
:
1197 fc
= sc
->packet_context_fc
;
1199 case CTF_SCOPE_EVENT_HEADER
:
1200 fc
= sc
->event_header_fc
;
1202 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
1203 fc
= sc
->event_common_context_fc
;
1205 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
1206 fc
= ec
->spec_context_fc
;
1208 case CTF_SCOPE_EVENT_PAYLOAD
:
1209 fc
= ec
->payload_fc
;
1217 for (i
= 0; i
< field_path
->path
->len
; i
++) {
1218 int64_t child_index
=
1219 ctf_field_path_borrow_index_by_index(field_path
, i
);
1220 struct ctf_field_class
*child_fc
=
1221 ctf_field_class_compound_borrow_field_class_by_index(
1223 BT_ASSERT(child_fc
);
1232 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
);
1235 void ctf_field_class_bit_array_copy_content(
1236 struct ctf_field_class_bit_array
*dst_fc
,
1237 struct ctf_field_class_bit_array
*src_fc
)
1241 dst_fc
->byte_order
= src_fc
->byte_order
;
1242 dst_fc
->size
= src_fc
->size
;
1246 void ctf_field_class_int_copy_content(
1247 struct ctf_field_class_int
*dst_fc
,
1248 struct ctf_field_class_int
*src_fc
)
1250 ctf_field_class_bit_array_copy_content((void *) dst_fc
, (void *) src_fc
);
1251 dst_fc
->meaning
= src_fc
->meaning
;
1252 dst_fc
->is_signed
= src_fc
->is_signed
;
1253 dst_fc
->disp_base
= src_fc
->disp_base
;
1254 dst_fc
->encoding
= src_fc
->encoding
;
1255 dst_fc
->mapped_clock_class
= src_fc
->mapped_clock_class
;
1256 dst_fc
->storing_index
= src_fc
->storing_index
;
1260 struct ctf_field_class_int
*_ctf_field_class_int_copy(
1261 struct ctf_field_class_int
*fc
)
1263 struct ctf_field_class_int
*copy_fc
= ctf_field_class_int_create();
1266 ctf_field_class_int_copy_content(copy_fc
, fc
);
1271 struct ctf_field_class_enum
*_ctf_field_class_enum_copy(
1272 struct ctf_field_class_enum
*fc
)
1274 struct ctf_field_class_enum
*copy_fc
= ctf_field_class_enum_create();
1278 ctf_field_class_int_copy_content((void *) copy_fc
, (void *) fc
);
1280 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
1283 struct ctf_field_class_enum_mapping
*mapping
=
1284 &g_array_index(fc
->mappings
,
1285 struct ctf_field_class_enum_mapping
, i
);
1287 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
1288 struct ctf_range
*range
=
1289 &g_array_index(mapping
->ranges
,
1290 struct ctf_range
, range_i
);
1292 ctf_field_class_enum_map_range(copy_fc
,
1293 mapping
->label
->str
, range
->lower
.u
,
1302 struct ctf_field_class_float
*_ctf_field_class_float_copy(
1303 struct ctf_field_class_float
*fc
)
1305 struct ctf_field_class_float
*copy_fc
= ctf_field_class_float_create();
1308 ctf_field_class_bit_array_copy_content((void *) copy_fc
, (void *) fc
);
1313 struct ctf_field_class_string
*_ctf_field_class_string_copy(
1314 struct ctf_field_class_string
*fc
)
1316 struct ctf_field_class_string
*copy_fc
= ctf_field_class_string_create();
1323 struct ctf_field_class_struct
*_ctf_field_class_struct_copy(
1324 struct ctf_field_class_struct
*fc
)
1326 struct ctf_field_class_struct
*copy_fc
= ctf_field_class_struct_create();
1331 for (i
= 0; i
< fc
->members
->len
; i
++) {
1332 struct ctf_named_field_class
*named_fc
=
1333 &g_array_index(fc
->members
,
1334 struct ctf_named_field_class
, i
);
1336 ctf_field_class_struct_append_member(copy_fc
,
1337 named_fc
->name
->str
,
1338 ctf_field_class_copy(named_fc
->fc
));
1345 void ctf_field_path_copy_content(struct ctf_field_path
*dst_fp
,
1346 struct ctf_field_path
*src_fp
)
1352 dst_fp
->root
= src_fp
->root
;
1353 ctf_field_path_clear(dst_fp
);
1355 for (i
= 0; i
< src_fp
->path
->len
; i
++) {
1356 int64_t index
= ctf_field_path_borrow_index_by_index(
1359 ctf_field_path_append_index(dst_fp
, index
);
1364 struct ctf_field_class_variant
*_ctf_field_class_variant_copy(
1365 struct ctf_field_class_variant
*fc
)
1367 struct ctf_field_class_variant
*copy_fc
=
1368 ctf_field_class_variant_create();
1373 for (i
= 0; i
< fc
->options
->len
; i
++) {
1374 struct ctf_named_field_class
*named_fc
=
1375 &g_array_index(fc
->options
,
1376 struct ctf_named_field_class
, i
);
1378 ctf_field_class_variant_append_option(copy_fc
,
1379 named_fc
->name
->str
,
1380 ctf_field_class_copy(named_fc
->fc
));
1383 for (i
= 0; i
< fc
->ranges
->len
; i
++) {
1384 struct ctf_field_class_variant_range
*range
=
1385 &g_array_index(fc
->ranges
,
1386 struct ctf_field_class_variant_range
, i
);
1388 g_array_append_val(copy_fc
->ranges
, *range
);
1391 ctf_field_path_copy_content(©_fc
->tag_path
, &fc
->tag_path
);
1392 g_string_assign(copy_fc
->tag_ref
, fc
->tag_ref
->str
);
1393 copy_fc
->stored_tag_index
= fc
->stored_tag_index
;
1398 void ctf_field_class_array_base_copy_content(
1399 struct ctf_field_class_array_base
*dst_fc
,
1400 struct ctf_field_class_array_base
*src_fc
)
1404 dst_fc
->elem_fc
= ctf_field_class_copy(src_fc
->elem_fc
);
1405 dst_fc
->is_text
= src_fc
->is_text
;
1409 struct ctf_field_class_array
*_ctf_field_class_array_copy(
1410 struct ctf_field_class_array
*fc
)
1412 struct ctf_field_class_array
*copy_fc
= ctf_field_class_array_create();
1415 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1416 copy_fc
->length
= fc
->length
;
1421 struct ctf_field_class_sequence
*_ctf_field_class_sequence_copy(
1422 struct ctf_field_class_sequence
*fc
)
1424 struct ctf_field_class_sequence
*copy_fc
=
1425 ctf_field_class_sequence_create();
1428 ctf_field_class_array_base_copy_content((void *) copy_fc
, (void *) fc
);
1429 ctf_field_path_copy_content(©_fc
->length_path
, &fc
->length_path
);
1430 g_string_assign(copy_fc
->length_ref
, fc
->length_ref
->str
);
1431 copy_fc
->stored_length_index
= fc
->stored_length_index
;
1436 struct ctf_field_class
*ctf_field_class_copy(struct ctf_field_class
*fc
)
1438 struct ctf_field_class
*copy_fc
= NULL
;
1445 * Translation should not have happened yet.
1447 BT_ASSERT(!fc
->ir_fc
);
1450 case CTF_FIELD_CLASS_TYPE_INT
:
1451 copy_fc
= (void *) _ctf_field_class_int_copy((void *) fc
);
1453 case CTF_FIELD_CLASS_TYPE_ENUM
:
1454 copy_fc
= (void *) _ctf_field_class_enum_copy((void *) fc
);
1456 case CTF_FIELD_CLASS_TYPE_FLOAT
:
1457 copy_fc
= (void *) _ctf_field_class_float_copy((void *) fc
);
1459 case CTF_FIELD_CLASS_TYPE_STRING
:
1460 copy_fc
= (void *) _ctf_field_class_string_copy((void *) fc
);
1462 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1463 copy_fc
= (void *) _ctf_field_class_struct_copy((void *) fc
);
1465 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1466 copy_fc
= (void *) _ctf_field_class_array_copy((void *) fc
);
1468 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1469 copy_fc
= (void *) _ctf_field_class_sequence_copy((void *) fc
);
1471 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1472 copy_fc
= (void *) _ctf_field_class_variant_copy((void *) fc
);
1478 copy_fc
->type
= fc
->type
;
1479 copy_fc
->alignment
= fc
->alignment
;
1480 copy_fc
->in_ir
= fc
->in_ir
;
1487 struct ctf_event_class
*ctf_event_class_create(void)
1489 struct ctf_event_class
*ec
= g_new0(struct ctf_event_class
, 1);
1492 ec
->name
= g_string_new(NULL
);
1493 BT_ASSERT(ec
->name
);
1494 ec
->emf_uri
= g_string_new(NULL
);
1495 BT_ASSERT(ec
->emf_uri
);
1496 ec
->is_log_level_set
= false;
1501 void ctf_event_class_set_log_level(struct ctf_event_class
*ec
,
1502 enum bt_event_class_log_level log_level
)
1505 ec
->log_level
= log_level
;
1506 ec
->is_log_level_set
= true;
1510 void ctf_event_class_destroy(struct ctf_event_class
*ec
)
1517 g_string_free(ec
->name
, TRUE
);
1521 g_string_free(ec
->emf_uri
, TRUE
);
1524 ctf_field_class_destroy(ec
->spec_context_fc
);
1525 ctf_field_class_destroy(ec
->payload_fc
);
1530 struct ctf_stream_class
*ctf_stream_class_create(void)
1532 struct ctf_stream_class
*sc
= g_new0(struct ctf_stream_class
, 1);
1535 sc
->event_classes
= g_ptr_array_new_with_free_func(
1536 (GDestroyNotify
) ctf_event_class_destroy
);
1537 BT_ASSERT(sc
->event_classes
);
1538 sc
->event_classes_by_id
= g_hash_table_new(g_direct_hash
,
1540 BT_ASSERT(sc
->event_classes_by_id
);
1545 void ctf_stream_class_destroy(struct ctf_stream_class
*sc
)
1551 if (sc
->event_classes
) {
1552 g_ptr_array_free(sc
->event_classes
, TRUE
);
1555 if (sc
->event_classes_by_id
) {
1556 g_hash_table_destroy(sc
->event_classes_by_id
);
1559 ctf_field_class_destroy(sc
->packet_context_fc
);
1560 ctf_field_class_destroy(sc
->event_header_fc
);
1561 ctf_field_class_destroy(sc
->event_common_context_fc
);
1566 void ctf_stream_class_append_event_class(struct ctf_stream_class
*sc
,
1567 struct ctf_event_class
*ec
)
1569 g_ptr_array_add(sc
->event_classes
, ec
);
1570 g_hash_table_insert(sc
->event_classes_by_id
,
1571 GUINT_TO_POINTER((guint
) ec
->id
), ec
);
1575 struct ctf_event_class
*ctf_stream_class_borrow_event_class_by_id(
1576 struct ctf_stream_class
*sc
, uint64_t type
)
1579 return g_hash_table_lookup(sc
->event_classes_by_id
,
1580 GUINT_TO_POINTER((guint
) type
));
1584 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry
*entry
)
1587 entry
->name
= g_string_new(NULL
);
1588 BT_ASSERT(entry
->name
);
1589 entry
->value
.str
= g_string_new(NULL
);
1590 BT_ASSERT(entry
->value
.str
);
1594 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry
*entry
)
1599 g_string_free(entry
->name
, TRUE
);
1602 if (entry
->value
.str
) {
1603 g_string_free(entry
->value
.str
, TRUE
);
1608 struct ctf_clock_class
*ctf_clock_class_create(void)
1610 struct ctf_clock_class
*cc
= g_new0(struct ctf_clock_class
, 1);
1613 cc
->name
= g_string_new(NULL
);
1614 BT_ASSERT(cc
->name
);
1615 cc
->description
= g_string_new(NULL
);
1616 BT_ASSERT(cc
->description
);
1621 void ctf_clock_class_destroy(struct ctf_clock_class
*cc
)
1628 g_string_free(cc
->name
, TRUE
);
1631 if (cc
->description
) {
1632 g_string_free(cc
->description
, TRUE
);
1635 bt_clock_class_put_ref(cc
->ir_cc
);
1640 struct ctf_trace_class
*ctf_trace_class_create(void)
1642 struct ctf_trace_class
*tc
= g_new0(struct ctf_trace_class
, 1);
1645 tc
->default_byte_order
= CTF_BYTE_ORDER_UNKNOWN
;
1646 tc
->clock_classes
= g_ptr_array_new_with_free_func(
1647 (GDestroyNotify
) ctf_clock_class_destroy
);
1648 BT_ASSERT(tc
->clock_classes
);
1649 tc
->stream_classes
= g_ptr_array_new_with_free_func(
1650 (GDestroyNotify
) ctf_stream_class_destroy
);
1651 BT_ASSERT(tc
->stream_classes
);
1652 tc
->env_entries
= g_array_new(FALSE
, TRUE
,
1653 sizeof(struct ctf_trace_class_env_entry
));
1658 void ctf_trace_class_destroy(struct ctf_trace_class
*tc
)
1664 ctf_field_class_destroy(tc
->packet_header_fc
);
1666 if (tc
->clock_classes
) {
1667 g_ptr_array_free(tc
->clock_classes
, TRUE
);
1670 if (tc
->stream_classes
) {
1671 g_ptr_array_free(tc
->stream_classes
, TRUE
);
1674 if (tc
->env_entries
) {
1677 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1678 struct ctf_trace_class_env_entry
*entry
=
1679 &g_array_index(tc
->env_entries
,
1680 struct ctf_trace_class_env_entry
, i
);
1682 _ctf_trace_class_env_entry_fini(entry
);
1685 g_array_free(tc
->env_entries
, TRUE
);
1692 void ctf_trace_class_append_env_entry(struct ctf_trace_class
*tc
,
1693 const char *name
, enum ctf_trace_class_env_entry_type type
,
1694 const char *str_value
, int64_t i_value
)
1696 struct ctf_trace_class_env_entry
*entry
;
1700 g_array_set_size(tc
->env_entries
, tc
->env_entries
->len
+ 1);
1702 entry
= &g_array_index(tc
->env_entries
,
1703 struct ctf_trace_class_env_entry
, tc
->env_entries
->len
- 1);
1705 _ctf_trace_class_env_entry_init(entry
);
1706 g_string_assign(entry
->name
, name
);
1709 g_string_assign(entry
->value
.str
, str_value
);
1712 entry
->value
.i
= i_value
;
1716 struct ctf_stream_class
*ctf_trace_class_borrow_stream_class_by_id(
1717 struct ctf_trace_class
*tc
, uint64_t id
)
1720 struct ctf_stream_class
*ret_sc
= NULL
;
1724 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1725 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1738 struct ctf_clock_class
*ctf_trace_class_borrow_clock_class_by_name(
1739 struct ctf_trace_class
*tc
, const char *name
)
1742 struct ctf_clock_class
*ret_cc
= NULL
;
1747 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
1748 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
1750 BT_ASSERT(cc
->name
);
1751 if (strcmp(cc
->name
->str
, name
) == 0) {
1762 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_index(
1763 struct ctf_trace_class
*tc
, uint64_t index
)
1766 BT_ASSERT(index
< tc
->env_entries
->len
);
1767 return &g_array_index(tc
->env_entries
, struct ctf_trace_class_env_entry
,
1772 struct ctf_trace_class_env_entry
*ctf_trace_class_borrow_env_entry_by_name(
1773 struct ctf_trace_class
*tc
, const char *name
)
1775 struct ctf_trace_class_env_entry
*ret_entry
= NULL
;
1781 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
1782 struct ctf_trace_class_env_entry
*env_entry
=
1783 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
1785 if (strcmp(env_entry
->name
->str
, name
) == 0) {
1786 ret_entry
= env_entry
;
1795 #endif /* _CTF_META_H */