ea6b9260fb47396980c936cff2fe4f1b04113f7e
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #ifndef _CTF_META_H
8 #define _CTF_META_H
9
10 #include <babeltrace2/babeltrace.h>
11 #include "common/common.h"
12 #include "common/uuid.h"
13 #include "common/assert.h"
14 #include <glib.h>
15 #include <stdbool.h>
16 #include <stdint.h>
17 #include <string.h>
18
19 enum ctf_field_class_type {
20 CTF_FIELD_CLASS_TYPE_INT,
21 CTF_FIELD_CLASS_TYPE_ENUM,
22 CTF_FIELD_CLASS_TYPE_FLOAT,
23 CTF_FIELD_CLASS_TYPE_STRING,
24 CTF_FIELD_CLASS_TYPE_STRUCT,
25 CTF_FIELD_CLASS_TYPE_ARRAY,
26 CTF_FIELD_CLASS_TYPE_SEQUENCE,
27 CTF_FIELD_CLASS_TYPE_VARIANT,
28 };
29
30 enum ctf_field_class_meaning {
31 CTF_FIELD_CLASS_MEANING_NONE,
32 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME,
33 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME,
34 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID,
35 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID,
36 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID,
37 CTF_FIELD_CLASS_MEANING_MAGIC,
38 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT,
39 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT,
40 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE,
41 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE,
42 CTF_FIELD_CLASS_MEANING_UUID,
43 };
44
45 enum ctf_byte_order {
46 CTF_BYTE_ORDER_UNKNOWN,
47 CTF_BYTE_ORDER_DEFAULT,
48 CTF_BYTE_ORDER_LITTLE,
49 CTF_BYTE_ORDER_BIG,
50 };
51
52 enum ctf_encoding {
53 CTF_ENCODING_NONE,
54 CTF_ENCODING_UTF8,
55 };
56
57 enum ctf_scope {
58 CTF_SCOPE_PACKET_UNKNOWN = -1,
59 CTF_SCOPE_PACKET_HEADER = 0,
60 CTF_SCOPE_PACKET_CONTEXT,
61 CTF_SCOPE_EVENT_HEADER,
62 CTF_SCOPE_EVENT_COMMON_CONTEXT,
63 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT,
64 CTF_SCOPE_EVENT_PAYLOAD,
65 };
66
67 struct ctf_clock_class {
68 GString *name;
69 GString *description;
70 uint64_t frequency;
71 uint64_t precision;
72 int64_t offset_seconds;
73 uint64_t offset_cycles;
74 bt_uuid_t uuid;
75 bool has_uuid;
76 bool is_absolute;
77
78 /* Weak, set during translation */
79 bt_clock_class *ir_cc;
80 };
81
82 struct ctf_field_class {
83 enum ctf_field_class_type type;
84 unsigned int alignment;
85 bool is_compound;
86 bool in_ir;
87
88 /* Weak, set during translation. NULL if `in_ir` is false below. */
89 bt_field_class *ir_fc;
90 };
91
92 struct ctf_field_class_bit_array {
93 struct ctf_field_class base;
94 enum ctf_byte_order byte_order;
95 unsigned int size;
96 };
97
98 struct ctf_field_class_int {
99 struct ctf_field_class_bit_array base;
100 enum ctf_field_class_meaning meaning;
101 bool is_signed;
102 bt_field_class_integer_preferred_display_base disp_base;
103 enum ctf_encoding encoding;
104 int64_t storing_index;
105
106 /* Weak */
107 struct ctf_clock_class *mapped_clock_class;
108 };
109
110 struct ctf_range {
111 union {
112 uint64_t u;
113 int64_t i;
114 } lower;
115
116 union {
117 uint64_t u;
118 int64_t i;
119 } upper;
120 };
121
122 struct ctf_field_class_enum_mapping {
123 GString *label;
124
125 /* Array of `struct ctf_range` */
126 GArray *ranges;
127 };
128
129 struct ctf_field_class_enum {
130 struct ctf_field_class_int base;
131
132 /* Array of `struct ctf_field_class_enum_mapping` */
133 GArray *mappings;
134 };
135
136 struct ctf_field_class_float {
137 struct ctf_field_class_bit_array base;
138 };
139
140 struct ctf_field_class_string {
141 struct ctf_field_class base;
142 enum ctf_encoding encoding;
143 };
144
145 struct ctf_named_field_class {
146 /* Original name which can include a leading `_` */
147 GString *orig_name;
148
149 /* Name as translated to trace IR (leading `_` removed) */
150 GString *name;
151
152 /* Owned by this */
153 struct ctf_field_class *fc;
154 };
155
156 struct ctf_field_class_struct {
157 struct ctf_field_class base;
158
159 /* Array of `struct ctf_named_field_class` */
160 GArray *members;
161 };
162
163 struct ctf_field_path {
164 enum ctf_scope root;
165
166 /* Array of `int64_t` */
167 GArray *path;
168 };
169
170 struct ctf_field_class_variant_range {
171 struct ctf_range range;
172 uint64_t option_index;
173 };
174
175 struct ctf_field_class_variant {
176 struct ctf_field_class base;
177 GString *tag_ref;
178 struct ctf_field_path tag_path;
179 uint64_t stored_tag_index;
180
181 /* Array of `struct ctf_named_field_class` */
182 GArray *options;
183
184 /* Array of `struct ctf_field_class_variant_range` */
185 GArray *ranges;
186
187 /* Weak */
188 struct ctf_field_class_enum *tag_fc;
189 };
190
191 struct ctf_field_class_array_base {
192 struct ctf_field_class base;
193 struct ctf_field_class *elem_fc;
194 bool is_text;
195 };
196
197 struct ctf_field_class_array {
198 struct ctf_field_class_array_base base;
199 enum ctf_field_class_meaning meaning;
200 uint64_t length;
201 };
202
203 struct ctf_field_class_sequence {
204 struct ctf_field_class_array_base base;
205 GString *length_ref;
206 struct ctf_field_path length_path;
207 uint64_t stored_length_index;
208
209 /* Weak */
210 struct ctf_field_class_int *length_fc;
211 };
212
213 struct ctf_event_class {
214 GString *name;
215 uint64_t id;
216 GString *emf_uri;
217 bt_event_class_log_level log_level;
218 bool is_translated;
219 bool is_log_level_set;
220
221 /* Owned by this */
222 struct ctf_field_class *spec_context_fc;
223
224 /* Owned by this */
225 struct ctf_field_class *payload_fc;
226
227 /* Weak, set during translation */
228 bt_event_class *ir_ec;
229 };
230
231 struct ctf_stream_class {
232 uint64_t id;
233 bool is_translated;
234 bool packets_have_ts_begin;
235 bool packets_have_ts_end;
236 bool has_discarded_events;
237 bool has_discarded_packets;
238 bool discarded_events_have_default_cs;
239 bool discarded_packets_have_default_cs;
240
241 /* Owned by this */
242 struct ctf_field_class *packet_context_fc;
243
244 /* Owned by this */
245 struct ctf_field_class *event_header_fc;
246
247 /* Owned by this */
248 struct ctf_field_class *event_common_context_fc;
249
250 /* Array of `struct ctf_event_class *`, owned by this */
251 GPtrArray *event_classes;
252
253 /*
254 * Hash table mapping event class IDs to `struct ctf_event_class *`,
255 * weak.
256 */
257 GHashTable *event_classes_by_id;
258
259 /* Weak */
260 struct ctf_clock_class *default_clock_class;
261
262 /* Weak, set during translation */
263 bt_stream_class *ir_sc;
264 };
265
266 enum ctf_trace_class_env_entry_type {
267 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
268 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
269 };
270
271 struct ctf_trace_class_env_entry {
272 enum ctf_trace_class_env_entry_type type;
273 GString *name;
274
275 struct {
276 int64_t i;
277 GString *str;
278 } value;
279 };
280
281 struct ctf_trace_class {
282 unsigned int major;
283 unsigned int minor;
284 bt_uuid_t uuid;
285 bool is_uuid_set;
286 enum ctf_byte_order default_byte_order;
287
288 /* Owned by this */
289 struct ctf_field_class *packet_header_fc;
290
291 uint64_t stored_value_count;
292
293 /* Array of `struct ctf_clock_class *` (owned by this) */
294 GPtrArray *clock_classes;
295
296 /* Array of `struct ctf_stream_class *` */
297 GPtrArray *stream_classes;
298
299 /* Array of `struct ctf_trace_class_env_entry` */
300 GArray *env_entries;
301
302 bool is_translated;
303
304 /* Weak, set during translation */
305 bt_trace_class *ir_tc;
306
307 struct {
308 bool lttng_crash;
309 bool lttng_event_after_packet;
310 bool barectf_event_before_packet;
311 } quirks;
312 };
313
314 static inline
315 void ctf_field_class_destroy(struct ctf_field_class *fc);
316
317 static inline
318 void _ctf_field_class_init(struct ctf_field_class *fc,
319 enum ctf_field_class_type type, unsigned int alignment)
320 {
321 BT_ASSERT(fc);
322 fc->type = type;
323 fc->alignment = alignment;
324 fc->in_ir = false;
325 }
326
327 static inline
328 void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
329 enum ctf_field_class_type type)
330 {
331 _ctf_field_class_init((void *) fc, type, 1);
332 }
333
334 static inline
335 void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
336 enum ctf_field_class_type type)
337 {
338 _ctf_field_class_bit_array_init((void *) fc, type);
339 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
340 fc->storing_index = -1;
341 }
342
343 static inline
344 void ctf_field_path_init(struct ctf_field_path *field_path)
345 {
346 BT_ASSERT(field_path);
347 field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t));
348 BT_ASSERT(field_path->path);
349 }
350
351 static inline
352 void ctf_field_path_fini(struct ctf_field_path *field_path)
353 {
354 BT_ASSERT(field_path);
355
356 if (field_path->path) {
357 g_array_free(field_path->path, TRUE);
358 }
359 }
360
361 static inline
362 void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
363 {
364 BT_ASSERT(named_fc);
365 named_fc->name = g_string_new(NULL);
366 BT_ASSERT(named_fc->name);
367 named_fc->orig_name = g_string_new(NULL);
368 BT_ASSERT(named_fc->orig_name);
369 }
370
371 static inline
372 void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
373 {
374 BT_ASSERT(named_fc);
375
376 if (named_fc->name) {
377 g_string_free(named_fc->name, TRUE);
378 }
379
380 if (named_fc->orig_name) {
381 g_string_free(named_fc->orig_name, TRUE);
382 }
383
384 ctf_field_class_destroy(named_fc->fc);
385 }
386
387 static inline
388 void _ctf_field_class_enum_mapping_init(
389 struct ctf_field_class_enum_mapping *mapping)
390 {
391 BT_ASSERT(mapping);
392 mapping->label = g_string_new(NULL);
393 BT_ASSERT(mapping->label);
394 mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range));
395 BT_ASSERT(mapping->ranges);
396 }
397
398 static inline
399 void _ctf_field_class_enum_mapping_fini(
400 struct ctf_field_class_enum_mapping *mapping)
401 {
402 BT_ASSERT(mapping);
403
404 if (mapping->label) {
405 g_string_free(mapping->label, TRUE);
406 }
407
408 if (mapping->ranges) {
409 g_array_free(mapping->ranges, TRUE);
410 }
411 }
412
413 static inline
414 struct ctf_field_class_int *ctf_field_class_int_create(void)
415 {
416 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
417
418 BT_ASSERT(fc);
419 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
420 return fc;
421 }
422
423 static inline
424 struct ctf_field_class_float *ctf_field_class_float_create(void)
425 {
426 struct ctf_field_class_float *fc =
427 g_new0(struct ctf_field_class_float, 1);
428
429 BT_ASSERT(fc);
430 _ctf_field_class_bit_array_init((void *) fc, CTF_FIELD_CLASS_TYPE_FLOAT);
431 return fc;
432 }
433
434 static inline
435 struct ctf_field_class_string *ctf_field_class_string_create(void)
436 {
437 struct ctf_field_class_string *fc =
438 g_new0(struct ctf_field_class_string, 1);
439
440 BT_ASSERT(fc);
441 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRING, 8);
442 return fc;
443 }
444
445 static inline
446 struct ctf_field_class_enum *ctf_field_class_enum_create(void)
447 {
448 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
449
450 BT_ASSERT(fc);
451 _ctf_field_class_int_init((void *) fc, CTF_FIELD_CLASS_TYPE_ENUM);
452 fc->mappings = g_array_new(FALSE, TRUE,
453 sizeof(struct ctf_field_class_enum_mapping));
454 BT_ASSERT(fc->mappings);
455 return fc;
456 }
457
458 static inline
459 struct ctf_field_class_struct *ctf_field_class_struct_create(void)
460 {
461 struct ctf_field_class_struct *fc =
462 g_new0(struct ctf_field_class_struct, 1);
463
464 BT_ASSERT(fc);
465 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
466 fc->members = g_array_new(FALSE, TRUE,
467 sizeof(struct ctf_named_field_class));
468 BT_ASSERT(fc->members);
469 fc->base.is_compound = true;
470 return fc;
471 }
472
473 static inline
474 struct ctf_field_class_variant *ctf_field_class_variant_create(void)
475 {
476 struct ctf_field_class_variant *fc =
477 g_new0(struct ctf_field_class_variant, 1);
478
479 BT_ASSERT(fc);
480 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
481 fc->options = g_array_new(FALSE, TRUE,
482 sizeof(struct ctf_named_field_class));
483 BT_ASSERT(fc->options);
484 fc->ranges = g_array_new(FALSE, TRUE,
485 sizeof(struct ctf_field_class_variant_range));
486 BT_ASSERT(fc->ranges);
487 fc->tag_ref = g_string_new(NULL);
488 BT_ASSERT(fc->tag_ref);
489 ctf_field_path_init(&fc->tag_path);
490 fc->base.is_compound = true;
491 return fc;
492 }
493
494 static inline
495 struct ctf_field_class_array *ctf_field_class_array_create(void)
496 {
497 struct ctf_field_class_array *fc =
498 g_new0(struct ctf_field_class_array, 1);
499
500 BT_ASSERT(fc);
501 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
502 fc->base.base.is_compound = true;
503 return fc;
504 }
505
506 static inline
507 struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
508 {
509 struct ctf_field_class_sequence *fc =
510 g_new0(struct ctf_field_class_sequence, 1);
511
512 BT_ASSERT(fc);
513 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
514 fc->length_ref = g_string_new(NULL);
515 BT_ASSERT(fc->length_ref);
516 ctf_field_path_init(&fc->length_path);
517 fc->base.base.is_compound = true;
518 return fc;
519 }
520
521 static inline
522 void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
523 {
524 BT_ASSERT(fc);
525 g_free(fc);
526 }
527
528 static inline
529 void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
530 {
531 BT_ASSERT(fc);
532
533 if (fc->mappings) {
534 uint64_t i;
535
536 for (i = 0; i < fc->mappings->len; i++) {
537 struct ctf_field_class_enum_mapping *mapping =
538 &g_array_index(fc->mappings,
539 struct ctf_field_class_enum_mapping, i);
540
541 _ctf_field_class_enum_mapping_fini(mapping);
542 }
543
544 g_array_free(fc->mappings, TRUE);
545 }
546
547 g_free(fc);
548 }
549
550 static inline
551 void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
552 {
553 BT_ASSERT(fc);
554 g_free(fc);
555 }
556
557 static inline
558 void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
559 {
560 BT_ASSERT(fc);
561 g_free(fc);
562 }
563
564 static inline
565 void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
566 {
567 BT_ASSERT(fc);
568
569 if (fc->members) {
570 uint64_t i;
571
572 for (i = 0; i < fc->members->len; i++) {
573 struct ctf_named_field_class *named_fc =
574 &g_array_index(fc->members,
575 struct ctf_named_field_class, i);
576
577 _ctf_named_field_class_fini(named_fc);
578 }
579
580 g_array_free(fc->members, TRUE);
581 }
582
583 g_free(fc);
584 }
585
586 static inline
587 void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
588 {
589 BT_ASSERT(fc);
590 ctf_field_class_destroy(fc->elem_fc);
591 }
592
593 static inline
594 void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
595 {
596 BT_ASSERT(fc);
597 _ctf_field_class_array_base_fini((void *) fc);
598 g_free(fc);
599 }
600
601 static inline
602 void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
603 {
604 BT_ASSERT(fc);
605 _ctf_field_class_array_base_fini((void *) fc);
606
607 if (fc->length_ref) {
608 g_string_free(fc->length_ref, TRUE);
609 }
610
611 ctf_field_path_fini(&fc->length_path);
612 g_free(fc);
613 }
614
615 static inline
616 void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
617 {
618 BT_ASSERT(fc);
619
620 if (fc->options) {
621 uint64_t i;
622
623 for (i = 0; i < fc->options->len; i++) {
624 struct ctf_named_field_class *named_fc =
625 &g_array_index(fc->options,
626 struct ctf_named_field_class, i);
627
628 _ctf_named_field_class_fini(named_fc);
629 }
630
631 g_array_free(fc->options, TRUE);
632 }
633
634 if (fc->ranges) {
635 g_array_free(fc->ranges, TRUE);
636 }
637
638 if (fc->tag_ref) {
639 g_string_free(fc->tag_ref, TRUE);
640 }
641
642 ctf_field_path_fini(&fc->tag_path);
643 g_free(fc);
644 }
645
646 static inline
647 void ctf_field_class_destroy(struct ctf_field_class *fc)
648 {
649 if (!fc) {
650 return;
651 }
652
653 switch (fc->type) {
654 case CTF_FIELD_CLASS_TYPE_INT:
655 _ctf_field_class_int_destroy((void *) fc);
656 break;
657 case CTF_FIELD_CLASS_TYPE_ENUM:
658 _ctf_field_class_enum_destroy((void *) fc);
659 break;
660 case CTF_FIELD_CLASS_TYPE_FLOAT:
661 _ctf_field_class_float_destroy((void *) fc);
662 break;
663 case CTF_FIELD_CLASS_TYPE_STRING:
664 _ctf_field_class_string_destroy((void *) fc);
665 break;
666 case CTF_FIELD_CLASS_TYPE_STRUCT:
667 _ctf_field_class_struct_destroy((void *) fc);
668 break;
669 case CTF_FIELD_CLASS_TYPE_ARRAY:
670 _ctf_field_class_array_destroy((void *) fc);
671 break;
672 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
673 _ctf_field_class_sequence_destroy((void *) fc);
674 break;
675 case CTF_FIELD_CLASS_TYPE_VARIANT:
676 _ctf_field_class_variant_destroy((void *) fc);
677 break;
678 default:
679 bt_common_abort();
680 }
681 }
682
683 static inline
684 struct ctf_range *ctf_field_class_enum_mapping_borrow_range_by_index(
685 struct ctf_field_class_enum_mapping *mapping, uint64_t index)
686 {
687 BT_ASSERT_DBG(mapping);
688 BT_ASSERT_DBG(index < mapping->ranges->len);
689 return &g_array_index(mapping->ranges, struct ctf_range, index);
690 }
691
692 static inline
693 struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_index(
694 struct ctf_field_class_enum *fc, uint64_t index)
695 {
696 BT_ASSERT_DBG(fc);
697 BT_ASSERT_DBG(index < fc->mappings->len);
698 return &g_array_index(fc->mappings, struct ctf_field_class_enum_mapping,
699 index);
700 }
701
702 static inline
703 struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_label(
704 struct ctf_field_class_enum *fc, const char *label)
705 {
706 struct ctf_field_class_enum_mapping *ret_mapping = NULL;
707 uint64_t i;
708
709 BT_ASSERT_DBG(fc);
710 BT_ASSERT_DBG(label);
711
712 for (i = 0; i < fc->mappings->len; i++) {
713 struct ctf_field_class_enum_mapping *mapping =
714 ctf_field_class_enum_borrow_mapping_by_index(fc, i);
715
716 if (strcmp(mapping->label->str, label) == 0) {
717 ret_mapping = mapping;
718 goto end;
719 }
720 }
721
722 end:
723 return ret_mapping;
724 }
725
726 static inline
727 void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc,
728 const char *label, uint64_t u_lower, uint64_t u_upper)
729 {
730 struct ctf_field_class_enum_mapping *mapping = NULL;
731 struct ctf_range range = {
732 .lower.u = u_lower,
733 .upper.u = u_upper,
734 };
735 uint64_t i;
736
737 BT_ASSERT(fc);
738 BT_ASSERT(label);
739
740 for (i = 0; i < fc->mappings->len; i++) {
741 mapping = ctf_field_class_enum_borrow_mapping_by_index(
742 fc, i);
743
744 if (strcmp(mapping->label->str, label) == 0) {
745 break;
746 }
747 }
748
749 if (i == fc->mappings->len) {
750 mapping = NULL;
751 }
752
753 if (!mapping) {
754 g_array_set_size(fc->mappings, fc->mappings->len + 1);
755 mapping = ctf_field_class_enum_borrow_mapping_by_index(
756 fc, fc->mappings->len - 1);
757 _ctf_field_class_enum_mapping_init(mapping);
758 g_string_assign(mapping->label, label);
759 }
760
761 g_array_append_val(mapping->ranges, range);
762 }
763
764 static inline
765 struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_index(
766 struct ctf_field_class_struct *fc, uint64_t index)
767 {
768 BT_ASSERT_DBG(fc);
769 BT_ASSERT_DBG(index < fc->members->len);
770 return &g_array_index(fc->members, struct ctf_named_field_class,
771 index);
772 }
773
774 static inline
775 struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_name(
776 struct ctf_field_class_struct *fc, const char *name)
777 {
778 uint64_t i;
779 struct ctf_named_field_class *ret_named_fc = NULL;
780
781 BT_ASSERT_DBG(fc);
782 BT_ASSERT_DBG(name);
783
784 for (i = 0; i < fc->members->len; i++) {
785 struct ctf_named_field_class *named_fc =
786 ctf_field_class_struct_borrow_member_by_index(fc, i);
787
788 if (strcmp(name, named_fc->name->str) == 0) {
789 ret_named_fc = named_fc;
790 goto end;
791 }
792 }
793
794 end:
795 return ret_named_fc;
796 }
797
798 static inline
799 struct ctf_field_class *ctf_field_class_struct_borrow_member_field_class_by_name(
800 struct ctf_field_class_struct *struct_fc, const char *name)
801 {
802 struct ctf_named_field_class *named_fc = NULL;
803 struct ctf_field_class *fc = NULL;
804
805 if (!struct_fc) {
806 goto end;
807 }
808
809 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
810 if (!named_fc) {
811 goto end;
812 }
813
814 fc = named_fc->fc;
815
816 end:
817 return fc;
818 }
819
820 static inline
821 struct ctf_field_class_int *
822 ctf_field_class_struct_borrow_member_int_field_class_by_name(
823 struct ctf_field_class_struct *struct_fc, const char *name)
824 {
825 struct ctf_field_class_int *int_fc = NULL;
826
827 int_fc = (void *)
828 ctf_field_class_struct_borrow_member_field_class_by_name(
829 struct_fc, name);
830 if (!int_fc) {
831 goto end;
832 }
833
834 if (int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_INT &&
835 int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_ENUM) {
836 int_fc = NULL;
837 goto end;
838 }
839
840 end:
841 return int_fc;
842 }
843
844 static inline
845 void _ctf_named_field_class_unescape_orig_name(
846 struct ctf_named_field_class *named_fc)
847 {
848 const char *name = named_fc->orig_name->str;
849
850 if (name[0] == '_') {
851 name++;
852 }
853
854 g_string_assign(named_fc->name, name);
855 }
856
857 static inline
858 void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
859 const char *orig_name, struct ctf_field_class *member_fc)
860 {
861 struct ctf_named_field_class *named_fc;
862
863 BT_ASSERT(fc);
864 BT_ASSERT(orig_name);
865 g_array_set_size(fc->members, fc->members->len + 1);
866
867 named_fc = &g_array_index(fc->members, struct ctf_named_field_class,
868 fc->members->len - 1);
869 _ctf_named_field_class_init(named_fc);
870 g_string_assign(named_fc->orig_name, orig_name);
871 _ctf_named_field_class_unescape_orig_name(named_fc);
872 named_fc->fc = member_fc;
873
874 if (member_fc->alignment > fc->base.alignment) {
875 fc->base.alignment = member_fc->alignment;
876 }
877 }
878
879 static inline
880 struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_index(
881 struct ctf_field_class_variant *fc, uint64_t index)
882 {
883 BT_ASSERT_DBG(fc);
884 BT_ASSERT_DBG(index < fc->options->len);
885 return &g_array_index(fc->options, struct ctf_named_field_class,
886 index);
887 }
888
889 static inline
890 struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_name(
891 struct ctf_field_class_variant *fc, const char *name)
892 {
893 uint64_t i;
894 struct ctf_named_field_class *ret_named_fc = NULL;
895
896 BT_ASSERT_DBG(fc);
897 BT_ASSERT_DBG(name);
898
899 for (i = 0; i < fc->options->len; i++) {
900 struct ctf_named_field_class *named_fc =
901 ctf_field_class_variant_borrow_option_by_index(fc, i);
902
903 if (strcmp(name, named_fc->name->str) == 0) {
904 ret_named_fc = named_fc;
905 goto end;
906 }
907 }
908
909 end:
910 return ret_named_fc;
911 }
912
913 static inline
914 struct ctf_field_class_variant_range *
915 ctf_field_class_variant_borrow_range_by_index(
916 struct ctf_field_class_variant *fc, uint64_t index)
917 {
918 BT_ASSERT_DBG(fc);
919 BT_ASSERT_DBG(index < fc->ranges->len);
920 return &g_array_index(fc->ranges, struct ctf_field_class_variant_range,
921 index);
922 }
923
924 static inline
925 void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
926 const char *orig_name, struct ctf_field_class *option_fc)
927 {
928 struct ctf_named_field_class *named_fc;
929
930 BT_ASSERT(fc);
931 BT_ASSERT(orig_name);
932 g_array_set_size(fc->options, fc->options->len + 1);
933
934 named_fc = &g_array_index(fc->options, struct ctf_named_field_class,
935 fc->options->len - 1);
936 _ctf_named_field_class_init(named_fc);
937 g_string_assign(named_fc->orig_name, orig_name);
938 _ctf_named_field_class_unescape_orig_name(named_fc);
939 named_fc->fc = option_fc;
940 }
941
942 static inline
943 void ctf_field_class_variant_set_tag_field_class(
944 struct ctf_field_class_variant *fc,
945 struct ctf_field_class_enum *tag_fc)
946 {
947 uint64_t option_i;
948
949 BT_ASSERT(fc);
950 BT_ASSERT(tag_fc);
951 fc->tag_fc = tag_fc;
952
953 for (option_i = 0; option_i < fc->options->len; option_i++) {
954 uint64_t range_i;
955 struct ctf_named_field_class *named_fc =
956 ctf_field_class_variant_borrow_option_by_index(
957 fc, option_i);
958 struct ctf_field_class_enum_mapping *mapping;
959
960 mapping = ctf_field_class_enum_borrow_mapping_by_label(
961 tag_fc, named_fc->orig_name->str);
962 if (!mapping) {
963 continue;
964 }
965
966 for (range_i = 0; range_i < mapping->ranges->len;
967 range_i++) {
968 struct ctf_range *range =
969 ctf_field_class_enum_mapping_borrow_range_by_index(
970 mapping, range_i);
971 struct ctf_field_class_variant_range var_range;
972
973 var_range.range = *range;
974 var_range.option_index = option_i;
975 g_array_append_val(fc->ranges, var_range);
976 }
977 }
978 }
979
980 static inline
981 struct ctf_field_class *ctf_field_class_compound_borrow_field_class_by_index(
982 struct ctf_field_class *comp_fc, uint64_t index)
983 {
984 struct ctf_field_class *fc = NULL;
985
986 switch (comp_fc->type) {
987 case CTF_FIELD_CLASS_TYPE_STRUCT:
988 {
989 struct ctf_named_field_class *named_fc =
990 ctf_field_class_struct_borrow_member_by_index(
991 (void *) comp_fc, index);
992
993 BT_ASSERT_DBG(named_fc);
994 fc = named_fc->fc;
995 break;
996 }
997 case CTF_FIELD_CLASS_TYPE_VARIANT:
998 {
999 struct ctf_named_field_class *named_fc =
1000 ctf_field_class_variant_borrow_option_by_index(
1001 (void *) comp_fc, index);
1002
1003 BT_ASSERT_DBG(named_fc);
1004 fc = named_fc->fc;
1005 break;
1006 }
1007 case CTF_FIELD_CLASS_TYPE_ARRAY:
1008 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1009 {
1010 struct ctf_field_class_array_base *array_fc = (void *) comp_fc;
1011
1012 fc = array_fc->elem_fc;
1013 break;
1014 }
1015 default:
1016 break;
1017 }
1018
1019 return fc;
1020 }
1021
1022 static inline
1023 uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
1024 {
1025 uint64_t field_count;
1026
1027 switch (fc->type) {
1028 case CTF_FIELD_CLASS_TYPE_STRUCT:
1029 {
1030 struct ctf_field_class_struct *struct_fc = (void *) fc;
1031
1032 field_count = struct_fc->members->len;
1033 break;
1034 }
1035 case CTF_FIELD_CLASS_TYPE_VARIANT:
1036 {
1037 struct ctf_field_class_variant *var_fc = (void *) fc;
1038
1039 field_count = var_fc->options->len;
1040 break;
1041 }
1042 case CTF_FIELD_CLASS_TYPE_ARRAY:
1043 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1044 /*
1045 * Array and sequence types always contain a single
1046 * member (the element type).
1047 */
1048 field_count = 1;
1049 break;
1050 default:
1051 bt_common_abort();
1052 }
1053
1054 return field_count;
1055 }
1056
1057 static inline
1058 int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1059 struct ctf_field_class *fc, const char *orig_name)
1060 {
1061 int64_t ret_index = -1;
1062 uint64_t i;
1063
1064 switch (fc->type) {
1065 case CTF_FIELD_CLASS_TYPE_STRUCT:
1066 {
1067 struct ctf_field_class_struct *struct_fc = (void *) fc;
1068
1069 for (i = 0; i < struct_fc->members->len; i++) {
1070 struct ctf_named_field_class *named_fc =
1071 ctf_field_class_struct_borrow_member_by_index(
1072 struct_fc, i);
1073
1074 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1075 ret_index = (int64_t) i;
1076 goto end;
1077 }
1078 }
1079
1080 break;
1081 }
1082 case CTF_FIELD_CLASS_TYPE_VARIANT:
1083 {
1084 struct ctf_field_class_variant *var_fc = (void *) fc;
1085
1086 for (i = 0; i < var_fc->options->len; i++) {
1087 struct ctf_named_field_class *named_fc =
1088 ctf_field_class_variant_borrow_option_by_index(
1089 var_fc, i);
1090
1091 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1092 ret_index = (int64_t) i;
1093 goto end;
1094 }
1095 }
1096
1097 break;
1098 }
1099 default:
1100 break;
1101 }
1102
1103 end:
1104 return ret_index;
1105 }
1106
1107 static inline
1108 void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index)
1109 {
1110 BT_ASSERT(fp);
1111 g_array_append_val(fp->path, index);
1112 }
1113
1114 static inline
1115 int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path *fp,
1116 uint64_t index)
1117 {
1118 BT_ASSERT_DBG(fp);
1119 BT_ASSERT_DBG(index < fp->path->len);
1120 return g_array_index(fp->path, int64_t, index);
1121 }
1122
1123 static inline
1124 void ctf_field_path_clear(struct ctf_field_path *fp)
1125 {
1126 BT_ASSERT(fp);
1127 g_array_set_size(fp->path, 0);
1128 }
1129
1130 static inline
1131 const char *ctf_scope_string(enum ctf_scope scope)
1132 {
1133 switch (scope) {
1134 case CTF_SCOPE_PACKET_HEADER:
1135 return "PACKET_HEADER";
1136 case CTF_SCOPE_PACKET_CONTEXT:
1137 return "PACKET_CONTEXT";
1138 case CTF_SCOPE_EVENT_HEADER:
1139 return "EVENT_HEADER";
1140 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1141 return "EVENT_COMMON_CONTEXT";
1142 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1143 return "EVENT_SPECIFIC_CONTEXT";
1144 case CTF_SCOPE_EVENT_PAYLOAD:
1145 return "EVENT_PAYLOAD";
1146 default:
1147 bt_common_abort();
1148 }
1149 }
1150
1151 static inline
1152 GString *ctf_field_path_string(struct ctf_field_path *path)
1153 {
1154 GString *str = g_string_new(NULL);
1155 uint64_t i;
1156
1157 BT_ASSERT(path);
1158
1159 if (!str) {
1160 goto end;
1161 }
1162
1163 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
1164
1165 for (i = 0; i < path->path->len; i++) {
1166 g_string_append_printf(str, ", %" PRId64,
1167 ctf_field_path_borrow_index_by_index(path, i));
1168 }
1169
1170 g_string_append(str, "]");
1171
1172 end:
1173 return str;
1174 }
1175
1176 static inline
1177 struct ctf_field_class *ctf_field_path_borrow_field_class(
1178 struct ctf_field_path *field_path,
1179 struct ctf_trace_class *tc,
1180 struct ctf_stream_class *sc,
1181 struct ctf_event_class *ec)
1182 {
1183 uint64_t i;
1184 struct ctf_field_class *fc;
1185
1186 switch (field_path->root) {
1187 case CTF_SCOPE_PACKET_HEADER:
1188 fc = tc->packet_header_fc;
1189 break;
1190 case CTF_SCOPE_PACKET_CONTEXT:
1191 fc = sc->packet_context_fc;
1192 break;
1193 case CTF_SCOPE_EVENT_HEADER:
1194 fc = sc->event_header_fc;
1195 break;
1196 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1197 fc = sc->event_common_context_fc;
1198 break;
1199 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1200 fc = ec->spec_context_fc;
1201 break;
1202 case CTF_SCOPE_EVENT_PAYLOAD:
1203 fc = ec->payload_fc;
1204 break;
1205 default:
1206 bt_common_abort();
1207 }
1208
1209 BT_ASSERT_DBG(fc);
1210
1211 for (i = 0; i < field_path->path->len; i++) {
1212 int64_t child_index =
1213 ctf_field_path_borrow_index_by_index(field_path, i);
1214 struct ctf_field_class *child_fc =
1215 ctf_field_class_compound_borrow_field_class_by_index(
1216 fc, child_index);
1217 BT_ASSERT_DBG(child_fc);
1218 fc = child_fc;
1219 }
1220
1221 BT_ASSERT_DBG(fc);
1222 return fc;
1223 }
1224
1225 static inline
1226 struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
1227
1228 static inline
1229 void ctf_field_class_bit_array_copy_content(
1230 struct ctf_field_class_bit_array *dst_fc,
1231 struct ctf_field_class_bit_array *src_fc)
1232 {
1233 BT_ASSERT(dst_fc);
1234 BT_ASSERT(src_fc);
1235 dst_fc->byte_order = src_fc->byte_order;
1236 dst_fc->size = src_fc->size;
1237 }
1238
1239 static inline
1240 void ctf_field_class_int_copy_content(
1241 struct ctf_field_class_int *dst_fc,
1242 struct ctf_field_class_int *src_fc)
1243 {
1244 ctf_field_class_bit_array_copy_content((void *) dst_fc, (void *) src_fc);
1245 dst_fc->meaning = src_fc->meaning;
1246 dst_fc->is_signed = src_fc->is_signed;
1247 dst_fc->disp_base = src_fc->disp_base;
1248 dst_fc->encoding = src_fc->encoding;
1249 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
1250 dst_fc->storing_index = src_fc->storing_index;
1251 }
1252
1253 static inline
1254 struct ctf_field_class_int *_ctf_field_class_int_copy(
1255 struct ctf_field_class_int *fc)
1256 {
1257 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
1258
1259 BT_ASSERT(copy_fc);
1260 ctf_field_class_int_copy_content(copy_fc, fc);
1261 return copy_fc;
1262 }
1263
1264 static inline
1265 struct ctf_field_class_enum *_ctf_field_class_enum_copy(
1266 struct ctf_field_class_enum *fc)
1267 {
1268 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
1269 uint64_t i;
1270
1271 BT_ASSERT(copy_fc);
1272 ctf_field_class_int_copy_content((void *) copy_fc, (void *) fc);
1273
1274 for (i = 0; i < fc->mappings->len; i++) {
1275 uint64_t range_i;
1276
1277 struct ctf_field_class_enum_mapping *mapping =
1278 &g_array_index(fc->mappings,
1279 struct ctf_field_class_enum_mapping, i);
1280
1281 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1282 struct ctf_range *range =
1283 &g_array_index(mapping->ranges,
1284 struct ctf_range, range_i);
1285
1286 ctf_field_class_enum_map_range(copy_fc,
1287 mapping->label->str, range->lower.u,
1288 range->upper.u);
1289 }
1290 }
1291
1292 return copy_fc;
1293 }
1294
1295 static inline
1296 struct ctf_field_class_float *_ctf_field_class_float_copy(
1297 struct ctf_field_class_float *fc)
1298 {
1299 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
1300
1301 BT_ASSERT(copy_fc);
1302 ctf_field_class_bit_array_copy_content((void *) copy_fc, (void *) fc);
1303 return copy_fc;
1304 }
1305
1306 static inline
1307 struct ctf_field_class_string *_ctf_field_class_string_copy(
1308 struct ctf_field_class_string *fc)
1309 {
1310 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
1311
1312 BT_ASSERT(copy_fc);
1313 return copy_fc;
1314 }
1315
1316 static inline
1317 struct ctf_field_class_struct *_ctf_field_class_struct_copy(
1318 struct ctf_field_class_struct *fc)
1319 {
1320 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
1321 uint64_t i;
1322
1323 BT_ASSERT(copy_fc);
1324
1325 for (i = 0; i < fc->members->len; i++) {
1326 struct ctf_named_field_class *named_fc =
1327 &g_array_index(fc->members,
1328 struct ctf_named_field_class, i);
1329
1330 ctf_field_class_struct_append_member(copy_fc,
1331 named_fc->name->str,
1332 ctf_field_class_copy(named_fc->fc));
1333 }
1334
1335 return copy_fc;
1336 }
1337
1338 static inline
1339 void ctf_field_path_copy_content(struct ctf_field_path *dst_fp,
1340 struct ctf_field_path *src_fp)
1341 {
1342 uint64_t i;
1343
1344 BT_ASSERT(dst_fp);
1345 BT_ASSERT(src_fp);
1346 dst_fp->root = src_fp->root;
1347 ctf_field_path_clear(dst_fp);
1348
1349 for (i = 0; i < src_fp->path->len; i++) {
1350 int64_t index = ctf_field_path_borrow_index_by_index(
1351 src_fp, i);
1352
1353 ctf_field_path_append_index(dst_fp, index);
1354 }
1355 }
1356
1357 static inline
1358 struct ctf_field_class_variant *_ctf_field_class_variant_copy(
1359 struct ctf_field_class_variant *fc)
1360 {
1361 struct ctf_field_class_variant *copy_fc =
1362 ctf_field_class_variant_create();
1363 uint64_t i;
1364
1365 BT_ASSERT(copy_fc);
1366
1367 for (i = 0; i < fc->options->len; i++) {
1368 struct ctf_named_field_class *named_fc =
1369 &g_array_index(fc->options,
1370 struct ctf_named_field_class, i);
1371
1372 ctf_field_class_variant_append_option(copy_fc,
1373 named_fc->name->str,
1374 ctf_field_class_copy(named_fc->fc));
1375 }
1376
1377 for (i = 0; i < fc->ranges->len; i++) {
1378 struct ctf_field_class_variant_range *range =
1379 &g_array_index(fc->ranges,
1380 struct ctf_field_class_variant_range, i);
1381
1382 g_array_append_val(copy_fc->ranges, *range);
1383 }
1384
1385 ctf_field_path_copy_content(&copy_fc->tag_path, &fc->tag_path);
1386 g_string_assign(copy_fc->tag_ref, fc->tag_ref->str);
1387 copy_fc->stored_tag_index = fc->stored_tag_index;
1388 return copy_fc;
1389 }
1390
1391 static inline
1392 void ctf_field_class_array_base_copy_content(
1393 struct ctf_field_class_array_base *dst_fc,
1394 struct ctf_field_class_array_base *src_fc)
1395 {
1396 BT_ASSERT(dst_fc);
1397 BT_ASSERT(src_fc);
1398 dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc);
1399 dst_fc->is_text = src_fc->is_text;
1400 }
1401
1402 static inline
1403 struct ctf_field_class_array *_ctf_field_class_array_copy(
1404 struct ctf_field_class_array *fc)
1405 {
1406 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
1407
1408 BT_ASSERT(copy_fc);
1409 ctf_field_class_array_base_copy_content((void *) copy_fc, (void *) fc);
1410 copy_fc->length = fc->length;
1411 return copy_fc;
1412 }
1413
1414 static inline
1415 struct ctf_field_class_sequence *_ctf_field_class_sequence_copy(
1416 struct ctf_field_class_sequence *fc)
1417 {
1418 struct ctf_field_class_sequence *copy_fc =
1419 ctf_field_class_sequence_create();
1420
1421 BT_ASSERT(copy_fc);
1422 ctf_field_class_array_base_copy_content((void *) copy_fc, (void *) fc);
1423 ctf_field_path_copy_content(&copy_fc->length_path, &fc->length_path);
1424 g_string_assign(copy_fc->length_ref, fc->length_ref->str);
1425 copy_fc->stored_length_index = fc->stored_length_index;
1426 return copy_fc;
1427 }
1428
1429 static inline
1430 struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
1431 {
1432 struct ctf_field_class *copy_fc = NULL;
1433
1434 if (!fc) {
1435 goto end;
1436 }
1437
1438 /*
1439 * Translation should not have happened yet.
1440 */
1441 BT_ASSERT(!fc->ir_fc);
1442
1443 switch (fc->type) {
1444 case CTF_FIELD_CLASS_TYPE_INT:
1445 copy_fc = (void *) _ctf_field_class_int_copy((void *) fc);
1446 break;
1447 case CTF_FIELD_CLASS_TYPE_ENUM:
1448 copy_fc = (void *) _ctf_field_class_enum_copy((void *) fc);
1449 break;
1450 case CTF_FIELD_CLASS_TYPE_FLOAT:
1451 copy_fc = (void *) _ctf_field_class_float_copy((void *) fc);
1452 break;
1453 case CTF_FIELD_CLASS_TYPE_STRING:
1454 copy_fc = (void *) _ctf_field_class_string_copy((void *) fc);
1455 break;
1456 case CTF_FIELD_CLASS_TYPE_STRUCT:
1457 copy_fc = (void *) _ctf_field_class_struct_copy((void *) fc);
1458 break;
1459 case CTF_FIELD_CLASS_TYPE_ARRAY:
1460 copy_fc = (void *) _ctf_field_class_array_copy((void *) fc);
1461 break;
1462 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1463 copy_fc = (void *) _ctf_field_class_sequence_copy((void *) fc);
1464 break;
1465 case CTF_FIELD_CLASS_TYPE_VARIANT:
1466 copy_fc = (void *) _ctf_field_class_variant_copy((void *) fc);
1467 break;
1468 default:
1469 bt_common_abort();
1470 }
1471
1472 copy_fc->type = fc->type;
1473 copy_fc->alignment = fc->alignment;
1474 copy_fc->in_ir = fc->in_ir;
1475
1476 end:
1477 return copy_fc;
1478 }
1479
1480 static inline
1481 struct ctf_event_class *ctf_event_class_create(void)
1482 {
1483 struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1);
1484
1485 BT_ASSERT(ec);
1486 ec->name = g_string_new(NULL);
1487 BT_ASSERT(ec->name);
1488 ec->emf_uri = g_string_new(NULL);
1489 BT_ASSERT(ec->emf_uri);
1490 ec->is_log_level_set = false;
1491 return ec;
1492 }
1493
1494 static inline
1495 void ctf_event_class_set_log_level(struct ctf_event_class *ec,
1496 enum bt_event_class_log_level log_level)
1497 {
1498 BT_ASSERT(ec);
1499 ec->log_level = log_level;
1500 ec->is_log_level_set = true;
1501 }
1502
1503 static inline
1504 void ctf_event_class_destroy(struct ctf_event_class *ec)
1505 {
1506 if (!ec) {
1507 return;
1508 }
1509
1510 if (ec->name) {
1511 g_string_free(ec->name, TRUE);
1512 }
1513
1514 if (ec->emf_uri) {
1515 g_string_free(ec->emf_uri, TRUE);
1516 }
1517
1518 ctf_field_class_destroy(ec->spec_context_fc);
1519 ctf_field_class_destroy(ec->payload_fc);
1520 g_free(ec);
1521 }
1522
1523 static inline
1524 struct ctf_stream_class *ctf_stream_class_create(void)
1525 {
1526 struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1);
1527
1528 BT_ASSERT(sc);
1529 sc->event_classes = g_ptr_array_new_with_free_func(
1530 (GDestroyNotify) ctf_event_class_destroy);
1531 BT_ASSERT(sc->event_classes);
1532 sc->event_classes_by_id = g_hash_table_new(g_direct_hash,
1533 g_direct_equal);
1534 BT_ASSERT(sc->event_classes_by_id);
1535 return sc;
1536 }
1537
1538 static inline
1539 void ctf_stream_class_destroy(struct ctf_stream_class *sc)
1540 {
1541 if (!sc) {
1542 return;
1543 }
1544
1545 if (sc->event_classes) {
1546 g_ptr_array_free(sc->event_classes, TRUE);
1547 }
1548
1549 if (sc->event_classes_by_id) {
1550 g_hash_table_destroy(sc->event_classes_by_id);
1551 }
1552
1553 ctf_field_class_destroy(sc->packet_context_fc);
1554 ctf_field_class_destroy(sc->event_header_fc);
1555 ctf_field_class_destroy(sc->event_common_context_fc);
1556 g_free(sc);
1557 }
1558
1559 static inline
1560 void ctf_stream_class_append_event_class(struct ctf_stream_class *sc,
1561 struct ctf_event_class *ec)
1562 {
1563 g_ptr_array_add(sc->event_classes, ec);
1564 g_hash_table_insert(sc->event_classes_by_id,
1565 GUINT_TO_POINTER((guint) ec->id), ec);
1566 }
1567
1568 static inline
1569 struct ctf_event_class *ctf_stream_class_borrow_event_class_by_id(
1570 struct ctf_stream_class *sc, uint64_t type)
1571 {
1572 BT_ASSERT_DBG(sc);
1573 return g_hash_table_lookup(sc->event_classes_by_id,
1574 GUINT_TO_POINTER((guint) type));
1575 }
1576
1577 static inline
1578 void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry)
1579 {
1580 BT_ASSERT(entry);
1581 entry->name = g_string_new(NULL);
1582 BT_ASSERT(entry->name);
1583 entry->value.str = g_string_new(NULL);
1584 BT_ASSERT(entry->value.str);
1585 }
1586
1587 static inline
1588 void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry)
1589 {
1590 BT_ASSERT(entry);
1591
1592 if (entry->name) {
1593 g_string_free(entry->name, TRUE);
1594 }
1595
1596 if (entry->value.str) {
1597 g_string_free(entry->value.str, TRUE);
1598 }
1599 }
1600
1601 static inline
1602 struct ctf_clock_class *ctf_clock_class_create(void)
1603 {
1604 struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1);
1605
1606 BT_ASSERT(cc);
1607 cc->name = g_string_new(NULL);
1608 BT_ASSERT(cc->name);
1609 cc->description = g_string_new(NULL);
1610 BT_ASSERT(cc->description);
1611 return cc;
1612 }
1613
1614 static inline
1615 void ctf_clock_class_destroy(struct ctf_clock_class *cc)
1616 {
1617 if (!cc) {
1618 return;
1619 }
1620
1621 if (cc->name) {
1622 g_string_free(cc->name, TRUE);
1623 }
1624
1625 if (cc->description) {
1626 g_string_free(cc->description, TRUE);
1627 }
1628
1629 bt_clock_class_put_ref(cc->ir_cc);
1630 g_free(cc);
1631 }
1632
1633 static inline
1634 struct ctf_trace_class *ctf_trace_class_create(void)
1635 {
1636 struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1);
1637
1638 BT_ASSERT(tc);
1639 tc->default_byte_order = CTF_BYTE_ORDER_UNKNOWN;
1640 tc->clock_classes = g_ptr_array_new_with_free_func(
1641 (GDestroyNotify) ctf_clock_class_destroy);
1642 BT_ASSERT(tc->clock_classes);
1643 tc->stream_classes = g_ptr_array_new_with_free_func(
1644 (GDestroyNotify) ctf_stream_class_destroy);
1645 BT_ASSERT(tc->stream_classes);
1646 tc->env_entries = g_array_new(FALSE, TRUE,
1647 sizeof(struct ctf_trace_class_env_entry));
1648 return tc;
1649 }
1650
1651 static inline
1652 void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1653 {
1654 if (!tc) {
1655 return;
1656 }
1657
1658 ctf_field_class_destroy(tc->packet_header_fc);
1659
1660 if (tc->clock_classes) {
1661 g_ptr_array_free(tc->clock_classes, TRUE);
1662 }
1663
1664 if (tc->stream_classes) {
1665 g_ptr_array_free(tc->stream_classes, TRUE);
1666 }
1667
1668 if (tc->env_entries) {
1669 uint64_t i;
1670
1671 for (i = 0; i < tc->env_entries->len; i++) {
1672 struct ctf_trace_class_env_entry *entry =
1673 &g_array_index(tc->env_entries,
1674 struct ctf_trace_class_env_entry, i);
1675
1676 _ctf_trace_class_env_entry_fini(entry);
1677 }
1678
1679 g_array_free(tc->env_entries, TRUE);
1680 }
1681
1682 g_free(tc);
1683 }
1684
1685 static inline
1686 void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc,
1687 const char *name, enum ctf_trace_class_env_entry_type type,
1688 const char *str_value, int64_t i_value)
1689 {
1690 struct ctf_trace_class_env_entry *entry;
1691
1692 BT_ASSERT(tc);
1693 BT_ASSERT(name);
1694 g_array_set_size(tc->env_entries, tc->env_entries->len + 1);
1695
1696 entry = &g_array_index(tc->env_entries,
1697 struct ctf_trace_class_env_entry, tc->env_entries->len - 1);
1698 entry->type = type;
1699 _ctf_trace_class_env_entry_init(entry);
1700 g_string_assign(entry->name, name);
1701
1702 if (str_value) {
1703 g_string_assign(entry->value.str, str_value);
1704 }
1705
1706 entry->value.i = i_value;
1707 }
1708
1709 static inline
1710 struct ctf_stream_class *ctf_trace_class_borrow_stream_class_by_id(
1711 struct ctf_trace_class *tc, uint64_t id)
1712 {
1713 uint64_t i;
1714 struct ctf_stream_class *ret_sc = NULL;
1715
1716 BT_ASSERT_DBG(tc);
1717
1718 for (i = 0; i < tc->stream_classes->len; i++) {
1719 struct ctf_stream_class *sc = tc->stream_classes->pdata[i];
1720
1721 if (sc->id == id) {
1722 ret_sc = sc;
1723 goto end;
1724 }
1725 }
1726
1727 end:
1728 return ret_sc;
1729 }
1730
1731 static inline
1732 struct ctf_clock_class *ctf_trace_class_borrow_clock_class_by_name(
1733 struct ctf_trace_class *tc, const char *name)
1734 {
1735 uint64_t i;
1736 struct ctf_clock_class *ret_cc = NULL;
1737
1738 BT_ASSERT_DBG(tc);
1739 BT_ASSERT_DBG(name);
1740
1741 for (i = 0; i < tc->clock_classes->len; i++) {
1742 struct ctf_clock_class *cc = tc->clock_classes->pdata[i];
1743
1744 BT_ASSERT_DBG(cc->name);
1745 if (strcmp(cc->name->str, name) == 0) {
1746 ret_cc = cc;
1747 goto end;
1748 }
1749 }
1750
1751 end:
1752 return ret_cc;
1753 }
1754
1755 static inline
1756 struct ctf_trace_class_env_entry *ctf_trace_class_borrow_env_entry_by_index(
1757 struct ctf_trace_class *tc, uint64_t index)
1758 {
1759 BT_ASSERT_DBG(tc);
1760 BT_ASSERT_DBG(index < tc->env_entries->len);
1761 return &g_array_index(tc->env_entries, struct ctf_trace_class_env_entry,
1762 index);
1763 }
1764
1765 static inline
1766 struct ctf_trace_class_env_entry *ctf_trace_class_borrow_env_entry_by_name(
1767 struct ctf_trace_class *tc, const char *name)
1768 {
1769 struct ctf_trace_class_env_entry *ret_entry = NULL;
1770 uint64_t i;
1771
1772 BT_ASSERT_DBG(tc);
1773 BT_ASSERT_DBG(name);
1774
1775 for (i = 0; i < tc->env_entries->len; i++) {
1776 struct ctf_trace_class_env_entry *env_entry =
1777 ctf_trace_class_borrow_env_entry_by_index(tc, i);
1778
1779 if (strcmp(env_entry->name->str, name) == 0) {
1780 ret_entry = env_entry;
1781 goto end;
1782 }
1783 }
1784
1785 end:
1786 return ret_entry;
1787 }
1788
1789 #endif /* _CTF_META_H */
This page took 0.101863 seconds and 4 git commands to generate.