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