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