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