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